0

目标:创建一个在 .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;
}

代码可以编译,但在我尝试运行它时会崩溃。谁能帮我解决这个问题?

4

2 回答 2

1
#include <stdio.h>
#include <stdlib.h>

int main()
{
    FILE *f, *g;
    int c;

    if ((f = fopen("asdf.txt", "r")) == NULL) {
        perror("fopen");
        exit(1);
    }
    if ((g = fopen("asdf1.txt", "w")) == NULL) {
        perror("fopen");
        exit(1);
    }

    while ((c = fgetc(f)) != EOF) {
        switch (c) {
        case 'i':
        case 'I':
            fputc('1', g);
            break;
        case 'e':
        case 'E':
            fputc('3', g);
            break;
        case 'a':
        case 'A':
            fputc('4', g);
            break;
        case 's':
        case 'S':
            fputc('5', g);
            break;
        case 'o':
        case 'O':
            fputc('0', g);
            break;
        default:
            fputc(c, g);
            break;
        }
    }

    fclose(f);
    fclose(g);

    return 0;
}

我已经修复了你的代码。主要是在所有未使用的行中添加 '\0',而不是在行尾打印 '\n',因为它是由 fgets 放入字符串中的,替换frase[i][j]frase[j][i],实际崩溃:fprintf(arquivo, "%c", frase[j][i])而不是fprintf(arquivo, "%s", frase[i][j]).

#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[i], 100, arquivo);
        i++;
    }
    for (; i < 100; i++) {
        frase[i][0] = '\0';
    }

    fclose(arquivo);

    //converting the letters to numbers
    for (j = 0; j < 100; j++) {
        i = 0;
        while (frase[j][i] != '\0') {
            if (frase[j][i] == 'i' || frase[j][i] == 'I') {
                frase[j][i] = '1';
            }
            if (frase[j][i] == 'e' || frase[j][i] == 'E') {
                frase[j][i] = '3';
            }
            if (frase[j][i] == 'a' || frase[j][i] == 'A') {
                frase[j][i] = '4';
            }
            if (frase[j][i] == 's' || frase[j][i] == 'S') {
                frase[j][i] = '5';
            }
            if (frase[j][i] == 'o' || frase[j][i] == 'O') {
                frase[j][i] = '0';
            }

            i++;
        }
    }

    arquivo = fopen("ex1 criptografado.txt", "w");

    for (j = 0; j < 100; j++) {
        i = 0;
        while (frase[j][i] != '\0') {
            fprintf(arquivo, "%c", frase[j][i]);
            i++;
        }
    }
    // or simpler:
    // for (j = 0; j < 100; j++)
    //     fprintf(arquivo, "%s", frase[j]);

    fclose(arquivo);

    return 0;
}
于 2013-06-27T19:26:33.600 回答
0

我认为使用矩阵的逻辑存在问题。您可以获取一大块字符串,然后将其与所有字符进行比较 - 如果它是想要的字符之一(您需要更改),则使用数字更改字符,通过将字符写入流 - 有关fputc详细信息,请参阅参考。

编辑:

请参阅@ctn 的答案——他的代码就是很好的例子。

于 2013-06-27T19:31:52.473 回答