Like Tree4Likes

FEOF issues

This is a discussion on FEOF issues within the C Programming forums, part of the General Programming Boards category; Hi Guys, Yes, I know I am not suppose to use FEOF to control a while loop but i need ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    34

    FEOF issues

    Hi Guys, Yes, I know I am not suppose to use FEOF to control a while loop but i need a suggestion of how to solve my issue.
    Code:
    while (!feof(in))
    adjust(root, in);
    In the function, a line of characters are read and picked out one by one. When the line reaches \n the function stops. Comes out to check the while loop for FEOF. There are cases where there are multiple lines this the function runs again (exactly what i want it to do). But when it does reach FEOF in the file itself, the while loop doesnt stop. Any Suggestions?

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,323
    We are going to need to see more of your code.
    Fact - Beethoven wrote his first symphony in C

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    34
    Quote Originally Posted by Click_here View Post
    We are going to need to see more of your code.
    The function adjust is
    Code:
    typedef struct node{
        char data[100];
        node *left,*right;
        char* n;
    }Node, *NodePtr;
    
    void adjust(NodePtr root, FILE * in2)
    {
        char str[100];
        char * result;
        NodePtr tnp=NULL, tmp=NULL, first=NULL;
        
        
        fgets(str,100,in2);
        int i=0;
        result=strtok(str," ");
        if(result!=NULL && *result!='\n' && result!='\0' && result!="\n")
        {
            tnp=find2 (root,result);    
            first=tnp;
        }
        while (result!=NULL && *result!='\n' && result!='\0' && result!="\n")
        {
        
        i++;
        
        result = strtok(NULL," ");
        
        if (result!=NULL && *result!='\n' && result!='\0' && result!="\n")
            {
            tmp=find2(root,result);
            tnp->n=tmp->data;
            
            }
        else
            tmp->n=first->data;
        if (result!=NULL && *result!='\n' && result!='\0' && result!="\n")
        tnp=find2 (root,result);
        }
        
    }
    My Main is

    Code:
    char main ()
    {
        NodePtr root=NULL, tnp;
        char test[20]="yo",word[20];
        char *result=NULL, *c=0;
        char str[16];
        int op=0;
    
    
        FILE * in=fopen("C:\\Test\\input.txt","r");
        FILE* out=fopen("C:\\Test\\input.txt","a");
        
        while (!feof(in))
        {
            fscanf(in,"%s",str);
            NodePtr tnp=find(root,str);
            if (root==NULL) root=tnp;
            
        }
    rewind(in);
    fseek(in, 0, SEEK_SET);
    while (!feof(in))
        adjust(root, in);
    
    display(root,word);
    fclose(out);
    fclose(in);
    getch();
    }
    Display function would find a word in the Binary Tree, then prints out all the 'n' values in structure.
    Last edited by Ramkiller1; 09-30-2012 at 07:20 PM. Reason: Forgot to add some extra code

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,622
    feof() can control a loop, but only if something tries to read from in before the feof() call. So that means you have to read from in before the loop starts and before the condition is checked in the loop, or you will encounter the problem with feof(). This necessary priming of your loops is the reason why it is bad to control loops with feof().
    laserlight likes this.

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Try controlling your loop like this:
    Code:
    while (fscanf(in, "%s", str) == 1)
    {
        NodePtr tnp=find(root,str);
        if (root==NULL) root=tnp;
    }
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  6. #6
    Registered User
    Join Date
    Sep 2012
    Posts
    34
    Thanks for the help will try now

  7. #7
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by whiteflags View Post
    This necessary priming of your loops is the reason why it is bad to control loops with feof().
    Can you show a good use of feof for loop control? To me it always seems best to do it some other way.
    EDIT: Now that I think about it, you probably want to stop the input loop whether it's eof or an error, but isn't feof only true for eof? It has a counterpart, ferror() for testing the error state. They're meant to be used after a stream has stopped to help determine why it stopped.
    Last edited by oogabooga; 09-30-2012 at 07:39 PM.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  8. #8
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,323
    Quote Originally Posted by oogabooga View Post
    Can you show a good use of feof for loop control? To me it always seems best to do it some other way.
    I've used it before, but I can't find where.

    This is a good example on how to use it though feof (CRT)
    Fact - Beethoven wrote his first symphony in C

  9. #9
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,622
    Quote Originally Posted by oogabooga View Post
    Can you show a good use of feof for loop control? To me it always seems best to do it some other way.
    Code:
    puts("Type:\n");
    
    int c = getchar();
    while (!feof(stdin)) {
       putchar(c);
       c = getchar();
    }
    Like I attempted to describe. Whether this is a good use is in the eye of the beholder. I can assure people that it works properly.

  10. #10
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by Click_here View Post
    I've used it before, but I can't find where.

    This is a good example on how to use it though feof (CRT)
    Notice the specific extra test of ferror() !!!
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  11. #11
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by whiteflags View Post
    Code:
    puts("Type:\n");
    
    int c = getchar();
    while (!feof(stdin)) {
       putchar(c);
       c = getchar();
    }
    Like I attempted to describe. Whether this is a good use is in the eye of the beholder. I can assure people that it works properly.
    Clearly
    Code:
    while ((c = getchar) != EOF)
        putchar(C);
    is better. It is C, after all.

    It also ensures the loop stops on an error (since EOF is returned on eof OR any other error).
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  12. #12
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,622
    I think the only time an error is even possible is if the file isn't locked and it's being used by another program. I didn't mean to say that it was better, I was just being factual. Pointlessly factual apparently.

  13. #13
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,323
    Testing the returned value for EOF is not always the best way of determining when you have arrived at the end of a file - Think of the case of when you are reading a binary file and one of the characters is the EOF character.
    Fact - Beethoven wrote his first symphony in C

  14. #14
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,622
    There is no EOF character. EOF is purposely out of the range of characters.

  15. #15
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,323
    What if your binary file is full of integers and one of the is the EOF character?
    Fact - Beethoven wrote his first symphony in C

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. HELP: feof
    By dlf723 in forum C Programming
    Replies: 5
    Last Post: 07-23-2010, 08:49 AM
  2. feof() from FAQ
    By salvadoravi in forum C Programming
    Replies: 6
    Last Post: 01-25-2008, 12:08 PM
  3. cannot get out of while( !feof (f) )
    By SoFarAway in forum C Programming
    Replies: 2
    Last Post: 02-19-2005, 02:36 PM
  4. feof issues
    By Rare177 in forum C Programming
    Replies: 13
    Last Post: 11-23-2004, 09:11 AM
  5. feof ....EOF
    By GanglyLamb in forum C Programming
    Replies: 12
    Last Post: 12-04-2002, 11:38 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21