EOF question, scanf also

This is a discussion on EOF question, scanf also within the C Programming forums, part of the General Programming Boards category; I have a piece of code that currently only reads in one line of data from a file : gcc ...

  1. #1
    newbie123
    Guest

    EOF question, scanf also

    I have a piece of code that currently only reads in one line of data from a file :

    gcc myfile.c<data.dat

    data.dat contains:

    2 8 1101100

    I'm using the scanf function to read in these values.

    I want to be able to read in more than one line of the data file; I know I need to construct some sort of loop to read it but I'm not sure how.

    Would it be a while(------ != EOF) ??

    Please help

  2. #2
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511

    Wink

    What about

    while (fscanf (infile, "%d %d %d", x , y, z) != EOF)

    anyway I assume you are using integers and the file continue to look follow same pattern. Anyway, use fscanf
    Mr. C: Author and Instructor

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Or use
    while (fscanf (infile, "%d %d %d", x , y, z) == 3)

    Or use fgets() to grab the line, and parse the data whilst its in memory.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>while (fscanf (infile, "%d %d %d", x , y, z) != EOF)
    This works fine if nothing happens in the middle of a conversion. scanf returns EOF if the end of the file is reached or if there's an error before any conversions, otherwise the number of successful conversions is returned. This is better
    Code:
    while (fscanf (infile, "%d %d %d", x , y, z) == 3)
    Now you cover for all possible oopsies that can confuse your program :-)
    *Cela*

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >while (fscanf (infile, "%d %d %d", x , y, z) == 3)
    Or even more likely:
    while (fscanf (infile, "%d %d %d", &x , &y, &z) == 3)

    I'm going to guess the prog has three ints, rather than 3 int pointers.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about 'gets' and EOF
    By Sharke in forum C Programming
    Replies: 4
    Last Post: 03-11-2009, 08:41 AM
  2. Quick Question Regarding Scanf() & Phone Numbers!
    By weaveram in forum C Programming
    Replies: 3
    Last Post: 09-20-2007, 09:58 AM
  3. Question on scanf();
    By Utopus in forum C Programming
    Replies: 4
    Last Post: 06-15-2007, 07:46 AM
  4. question about eof
    By ssjnamek in forum C++ Programming
    Replies: 17
    Last Post: 05-05-2005, 10:05 AM
  5. files won't stop being read!!!
    By jverkoey in forum C++ Programming
    Replies: 15
    Last Post: 04-10-2003, 05:28 AM

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