Thread: a proper read loop

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    9

    a proper read loop

    I've been reading characters from text files but I have been using a loop that uses
    Code:
    	while(!feof(filename))
    to read into whatever I want it to read into. However, I know this is not the proper way to do it.

    I'm supposed to be using the return code of whatever I am using to read from the file, right?

    So I am using fscanf and reading 1 character at a time....how do I loop with fscanf? I guess my question is what does fscanf return when it reaches the end of a file. The code below is simply what my guess is at what it will be like.

    Code:
    while( (fscanf(filename, "%c", &next_char) != ???) )
    {
        work with data here
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I guess my question is what does fscanf return when it reaches the end of a file.
    fscanf returns EOF on error or at the end-of-file, otherwise it returns the number of converted items.
    Code:
    while( fscanf(filename, "%c", &next_char) == 1 )
    {
        work with data here
    }
    [edit]
    Found a nasty typo
    [/edit]

    -Prelude
    Last edited by Prelude; 02-25-2003 at 11:45 AM.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Dec 2002
    Posts
    9
    Thanks.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>I guess my question is what does fscanf return when it reaches the end of a file
    Why not use a good reference site.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Dec 2002
    Posts
    9
    I have that page bookmarked from before but I'm still at a stage where I dont understand alot of what those manual pages are really saying. Thanks though...I'm getting there.

  6. #6
    Registered User
    Join Date
    Feb 2003
    Posts
    31
    The value EOF is returned if an input failure occurs before any conversion such as an end-of-file occurs.
    That was from the refrence site Hammer told you to look at.

    So you would want to use:

    Code:
     while( (fscanf(filename, "%c", &next_char) != EOF) )

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Originally posted by GravtyKlz
    That was from the refrence site Hammer told you to look at.

    So you would want to use:
    Code:
     while( (fscanf(filename, "%c", &next_char) != EOF) )
    Also from the same site:
    Code:
    RETURN VALUES
           These functions return the number of input items assigned,
           which can be fewer than provided for, or even zero, in the
           event  of  a matching failure.  Zero indicates that, while
           there was input available, no conversions  were  assigned;
           typically  this is due to an invalid input character, such
           as an alphabetic character for  a  `%d'  conversion.   The
           value  EOF  is  returned if an input failure occurs before
           any conversion such as an end-of-file occurs. If an  error
           or end-of-file occurs after conversion has begun, the num-
           ber of conversions which were  successfully  completed  is
           returned.
    I'd do it like this.
    Code:
    #include <stdio.h> 
    int main()
    {
       const char filename[] = "test.txt";
       FILE *file = fopen(filename, "r");
       if ( file != NULL )
       {
          char ch;
          while ( fscanf(file, "%c", &ch) == 1 )
          {
             /* do stuff */
             putchar(ch);
          }
          fflush(stdout);
          fclose(file);
       }
       return 0;
    }
    Or this.
    Code:
    #include <stdio.h> 
    int main()
    {
       const char filename[] = "test.txt";
       FILE *file = fopen(filename, "r");
       if ( file != NULL )
       {
          for ( ;; )
          {
             int ch = fgetc(file);
             if( ch == EOF )
             {
                break;
             }
             /* do stuff */
             putchar(ch);
          }
          fflush(stdout);
          fclose(file);
       }
       return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  2. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  3. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  4. Proper way to read in a file
    By elaechelin in forum C++ Programming
    Replies: 2
    Last Post: 04-28-2004, 10:00 AM
  5. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM