转换.txt文件的文本

时间:2013-06-27 19:09:26

标签: c file matrix printf

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

代码编译,但在我尝试运行它时崩溃了。任何人都可以帮我解决这个问题吗?

2 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分:0)

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

修改

通过@ctn查看答案 - 他的代码是很好的例子。