0

Hi guys i am wirting a c programme here and i am trying to print two lines from a text file at a time the problem is that when on the last line if the lines are odd, 3 lines 5 lines. It will print the last line twice.I cant find the comparison of if function for thsi particualr question. is it a bool? I am trying ferror(file) currently

    FILE *file;

    printf("Hello!\n");
    printf("Please input file name(without.txt):\n");

    scanf("%s", input);
    strcat(input,".txt");
    file = fopen( input , "r" );

    while(!(feof(file)))
    {

        for(i=0; i<2; i++){
        **if(feof(file)==ferror(file))**
        {
            printf("File ended");
                return 0;
        }
        else
        {
            fgets(return_char, 200, file);
            printf("%s\n", return_char);
        }
    }

    }

Example of input :

i have a cat

i have a dog

i have a cow

Expected output :

first round

i have a cat

i have a dog

second round :

i have a cow

Current output : first round :

i have a cat

i have a dog

second round :

i have a cow

i have a cow

user3809384
  • 111
  • 1
  • 4
  • 10
  • 2
    “while( !feof( file ) )” is always wrong.http://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong – ani627 Sep 03 '14 at 11:07
  • hi there thanks for replying. i have changed and am using what you have suggested, it still consist of the same error and the first letter is missing. output now is have a cat i have a dog second output have a cow have a cow – user3809384 Sep 03 '14 at 11:17
  • Stop using `feof` and `ferror` entirely. Loop until `fgets` fails. – M.M Sep 03 '14 at 11:22

2 Answers2

2

The behaviour you observe is typical of foef, which checks for end-of-file after the fact.

It is usually better to use the return values of the file reading functions (fgets, fgetc, fread etc.) instead of feof. Your loop will then look like this:

while (1) {
    for(i=0; i<2; i++) {
        if (fgets(return_char, 200, file) == NULL) {
            printf("File ended");
            return 0;
        }            
        printf("%s", return_char);            
    }
    printf("--\n");
}

Or, without explicit loop over i and outer infinite loop framework:

i = 0;
while (fgets(return_char, 200, file)) {
    printf("%s", return_char);            
    if (i == 1) printf("--\n");
    i = 1 - i;
}

printf("File ended");
return 0;
M Oehm
  • 28,726
  • 3
  • 31
  • 42
  • `while (fgets(return_char, 200, file) != NULL) {` or `while (fgets(return_char, 200, file)) {` – wildplasser Sep 03 '14 at 11:29
  • @wildplasser: Oh, of course: the second example has the condition wrong (and I prefer your second syntax). I'll fix, thanks. – M Oehm Sep 03 '14 at 11:35
  • @MOehm the only problem left is that i will be prompting user for an input for the second round of loop which i can't be using while(1) – user3809384 Sep 03 '14 at 11:41
  • @user3809384: I don't understand what you want, I'm afraid. Do you want to prompt the user interactively for the second string if the file has an odd number of lines? – M Oehm Sep 03 '14 at 11:51
  • Nope. For the first two lines it will appear automatically, for the following 2 user will be prompted to continue or exit. The .txt that will be read are given off 3 lines. When user chooses to continue, it returns the third line twice. i have tried many different loops it either works giving me all 3 lines altogether or 4 lines with repeated 3rd line all this while. @MOehm – user3809384 Sep 03 '14 at 11:58
  • @user3809384: But that is totally unrelated to the original `feof` question and has something to do with reading user input with `scanf` or some such, no? – M Oehm Sep 03 '14 at 12:10
0

Try this code-

#include<stdio.h>
#include<string.h>
int main()
{
        int i;
        FILE *file;
        char input[20],return_char[50],ch;
        printf("Hello!\n");
        printf("Please input file name(without.txt):\n");

        scanf("%s", input);
        strcat(input,".txt");
        file = fopen( input , "r" );
        i=2;
        while(fgets(return_char, 200, file))
        {
                if(i==0){ 
                        printf("Do you want to continue(y/n)\n");
                        scanf(" %c",&ch);
                        if(ch == 'y'){
                                printf("%s\n", return_char); 
                                i=1; // making i=1 to read further line.
                        }
                        else{
                                fclose(file);
                                return 0;
                        }
                }
                else{
                        printf("%s\n", return_char);
                        i--;
                }
        }
}

In this program when i=0 the return_char will have the third line of the file. So i am making i=1 again to read next line and so on.

Sathish
  • 3,740
  • 1
  • 17
  • 28