用C读取和写入文件没有给出正确的输出

时间:2017-02-10 19:43:32

标签: c

我有一个输入文件,格式如下:

  

C G F 0.89402944 I -628131597   C U F 0.8987373 C F C N F 0.046165943 C K I -898862630 F 0.20252013 I -1074797352 F 0.11681563   F 0.88717264 F 0.7676326   I -817826750

目标是创建3个新文件。一个文件将包含字符,另一个文件将包含整数,第三个文件将包含浮点数。

我应该先读取第一个字符来读取输入文件。这个角色会告诉我下一个字符串是什么。因此,例如,第一个字符是C,这意味着下一个字符序列将是Char。然后我读了下一个字符,它是一个F,这意味着下一个字符序列将是一个浮点数,依此类推。

#include<stdio.h>

int main(int arg, char * argv[]){
FILE * intsFile = fopen("ints.txt", "w");
FILE * charsFile = fopen("chars.txt", "w");
FILE * floatsFile = fopen("floats.txt", "w");

FILE * inputFile = fopen("input.txt", "r");

char characters;
int integers;
float floats;

char flag;

while(fscanf(inputFile, "%c", &flag) != EOF){
    if (flag == 'C'){
        fscanf(inputFile, "%c", &characters);
        fprintf(charsFile, "%c ", characters);
    }
    if (flag == 'F'){
        fscanf(inputFile, "%i", &integers);
        fscanf(inputFile, "%i ", integers);
    }
    if (flag == 'I'){
        fscanf(inputFile, "%f", &floats);
        fscanf(inputFile, "%f ", floats);
    }
}

return 0;
}

然而,我的输出很奇怪。我的chars.txt文件中没有任何内容,我的ints文件中没有任何内容,并且我的浮动文件包含重复值。怎么回事?

2 个答案:

答案 0 :(得分:1)

你的循环有点不对,flag == F正在读整数,整数和浮点读也试图写入输入文件。你的主要问题是字符和数字之间的空格,可以通过scanf&#34;来解决。 %C&#34;等如下图所示。 这是完整的工作代码。

#include<stdio.h>

int main(int arg, char * argv[]){
    FILE * intsFile = fopen("ints.txt", "w");
    FILE * charsFile = fopen("chars.txt", "w");
    FILE * floatsFile = fopen("floats.txt", "w");

    FILE * inputFile = fopen("input.txt", "r");

    char characters;
    int integers;
    float floats;

    char space;

    char flag;

    while (fscanf(inputFile, "%c ", &flag) != EOF){
        if (flag == 'C'){

            fscanf(inputFile, "%c", &characters);
            fprintf(charsFile, "%c ", characters);

        }
        if (flag == 'F'){
            fscanf(inputFile, "%f", &floats);
            fprintf(floatsFile, "%f ", floats);

        }
        if (flag == 'I'){

            fscanf(inputFile, "%i", &integers);
            fprintf(intsFile, "%i", integers);

        }
        fscanf(inputFile, "%c", &space);
    }

    return 0;
}

答案 1 :(得分:1)

您可以使用以下代码修复它:

#include<stdio.h>

int main(int arg, char * argv[]){
    FILE * intsFile = fopen("ints.txt", "wt");
    FILE * charsFile = fopen("chars.txt", "wt");
    FILE * floatsFile = fopen("floats.txt", "wt");

    FILE * inputFile = fopen("input.txt", "r");

    char characters;
    int integers;
    float floats;

    char flag;

    while(fscanf(inputFile, "%c", &flag) != EOF){
        if (flag == 'C'){
            fscanf(inputFile, " %c", &characters); //fix
            fprintf(charsFile, "%c ", characters);
        }
        if (flag == 'F'){
            fscanf(inputFile, "%f", &floats);//fix. wrong order with integer
            fprintf(floatsFile, "%f ", floats);//fix
        }
        if (flag == 'I'){
            fscanf(inputFile, "%i", &integers);//fix. wrong order with float
            fprintf(intsFile, "%i ", integers);//fix
        }
    }

    fclose(intsFile);//fixes
    fclose(charsFile);
    fclose(floatsFile);
    fclose(inputFile);

    return 0;
}