-2

In my .txt file I have follow data.

Name: name
Score: 0.00%
Name: name
Score: 0.00%
Name: name
Score:6.67%

and I tried using this code

    if (userAnswer1 == 3){
        printf("Test History\n\n");
        FILE * fPointer;
        fPointer = fopen("history.txt", "r");
        char singleLine[50];
        while(!feof(fPointer)) {
            fgets(singleLine, 50, fPointer);
            puts(singleLine);
            int i;
            for(i=0; i>=0; i++){
                if(i%2==0){
                    printf("\n");
                }
                break;
            }
        }
        fclose(fPointer);
    }

to get this output:

Name: name
Score: 0.00%

Name: name
Score: 0.00%

Name: name
Score: 6.67%

Instead of that I got below output.

Test History

Name: name

Score: 0.00%

Name: name

Score: 0.00%

Name: name

Score: 6.67%

Score: 6.67%

Pratik Gadoya
  • 1,420
  • 1
  • 16
  • 27
andre2012
  • 1
  • 3

4 Answers4

1

Two things: The puts function writes a newline; And the fgets functions may include the newline in the buffer.

That means when you print singleLine first there is a newline (because fgets put it there), then puts writes its own newline.

Either remove the newline that fgets writes into the buffer, or don't use puts.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
1
while(!feof(fPointer)){
    fgets(singleLine, 50, fPointer);
    puts(singleLine);
    int i;
    for(i=0; i>=0; i++){
        if(i%2==0){
            printf("\n");
        }
        break;
    }
}

There are a few things to note:

  1. fgets() reads until a newline, and includes it to the buffer. So on the following puts() call, you've already output a newline after that, and puts() even adds a newline on its own.

  2. Consider this code:

    int i;
    for(i=0; i>=0; i++){
        if(i%2==0){
            printf("\n");
        }
        break;
    }
    

    You're actually printing one newline, then break the loop. So it makes no difference from a plain

    printf("\n");
    

Counting both things above, you're actually printing 3 newlines every while loop: One is what fgets() writes to the buffer, one from puts(), one manually.

To fix them, you need to do two jobs:

  1. Strip the newline from whatever fgets() gives you:

    singleLine[strlen(singleLine) - 1] = '\0';
    
  2. Make the loop work better:

    int i = 0; // Must initialize!
    while (condition) {
        other_statements;
    
        i++;
        if (i%2==0) {
            printf("\n");
        }
        // Do nothing if 'i' is odd
    }
    
  3. Stop using while (!feof()). Check the return value of fgets instead:

    char *ret = fgets( ... );
    if (ret == NULL)
        break;
    
iBug
  • 35,554
  • 7
  • 89
  • 134
  • Ok, so i tried this, but it will not print anything now except empty lines. I guess singleLine[strlen(singleLine) - 1] = '\0'; seem to remove it all – andre2012 Dec 12 '17 at 13:55
  • So i came a little closer to a solution, int i=0; while(!feof(fPointer)){ fgets(singleLine, 50, fPointer); printf("%s", singleLine); i++; if(i%2==0){ printf("\n"); } this seem to work except that it prints out the last score again; "Score: 6.67%" – andre2012 Dec 12 '17 at 14:11
  • @andre2012 Don't use `while(!feof())`. Check the return value of `fgets` instead. – iBug Dec 12 '17 at 14:16
  • thanks for the help, didnt really understand but checket tutorial on youtube and got it right, will post code in a reply. – andre2012 Dec 14 '17 at 15:00
  • @ander2012 Which part don't you understand? – iBug Dec 14 '17 at 16:22
  • kinda new to programming, so barely anything with checking return value of fgets, i know it can return NULL or something else. I guess that if it returns NULL it is supposed to stop checking in the file – andre2012 Dec 14 '17 at 16:34
0
    printf("Test History\n\n");

    FILE *pToFile = fopen("history.txt", "r");

    char input[50];
    int i=0;
    while(fgets(input, 50, pToFile)){
        printf("%s", input);
        i++;
        if(i%2==0){
            printf("\n");
        }
    }
    fclose(pToFile);
andre2012
  • 1
  • 3
-2

the trouble is with your logic - you have the 'if' inside the 'while' so it loops and counts on each 'while' (line from your file).

I'm not c expert, so no code, but it seems you get the commands. The logic I would use is to look a the text in the line and add the '\n' from that.

i.e.

if singleline (contains - however you do that in c) "%" 

add the \n

Apps-n-Add-Ons
  • 2,026
  • 1
  • 17
  • 28