简体   繁体   中英

Reading and writing files in C isn't giving correct output

I've got an input file, of the following format:

CGF 0.89402944 I -628131597 CUF 0.8987373 CFCNF 0.046165943 CKI -898862630 F 0.20252013 I -1074797352 F 0.11681563 F 0.88717264 F 0.7676326 I -817826750

The objective is to create 3 new files. One file will contain characters, the other will contain ints, and the third will contain floats.

I'm supposed to read the input file by first reading the first character. This character will tell me what the next string is. So, for example, the first character is C, this means that the next sequence of characters will be a Char. Then I read the next character, which is an F, which means that the next sequence of characters will be a float, and so on.

#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;
}

However, my output is weird. I get nothing in my chars.txt file, nothing in my ints file, and my floats file contains a duplicate value. Whats going on here?

Your loops were a bit wrong, flag==F was reading integers and also integer and float reads were trying to write to the input file. Your main problem was the SPACES between characters and numbers, which can be fixed with scanf being " %c" etc as shown below. Here is the complete working code.

#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;
}

You can fix it with following code:

#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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM