目标:创建一个在 .txt 文件中转换的简单代码:
每个'I','E','A','S'和'O'分别变成'1','3','4','5'和'0'。
一开始我做得很简单,但它不能处理多行文本。所以我试着把短语和台词当作一个矩阵来处理,但我仍然无法让它发挥作用。这是代码:
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *arquivo;
char frase[100][100];
int i = 0;
int j = 0;
//adress of the initial text file
arquivo = fopen("C:\\Users\\xand\\Desktop\\ex1.txt", "r");
//transfering every line of the file into a matrix
while (!feof(arquivo))
{
fgets(frase[100][i],100, arquivo);
i++;
}
fclose(arquivo);
//converting the letters to numbers
for(j = 0; j < 100; j++)
{
while(frase[i][j] != '\0')
{
if(frase[i][j] == 'i' || frase[i][j] == 'I')
{
frase[i][j] = '1';
}
if(frase[i][j] == 'e' || frase[i][j] == 'E')
{
frase[i][j] = '3';
}
if(frase[i][j] == 'a' || frase[i][j] == 'A')
{
frase[i][j] = '4';
}
if(frase[i][j] == 's' || frase[i][j] == 'S')
{
frase[i][j] = '5';
}
if(frase[i][j] == 'o' || frase[i][j] == 'O')
{
frase[i][j] = '0';
}
i++;
}
}
arquivo = fopen("ex1 criptografado.txt", "w");
//here is where I believe to be the problem
//It doesn't even create the new file. Im not sure if using matrix is the ideal solution to fprintf a multi-lined text to a file
for(j = 0; j < 100; j++)
{
i = 0;
while(frase[i][j] != '\0')
{
fprintf(arquivo, "%s", frase[i][j]);
i++;
}
fprintf(arquivo, "\n");
}
fclose(arquivo);
return 0;
}
代码可以编译,但在我尝试运行它时会崩溃。谁能帮我解决这个问题?