Reading Text File

This is a discussion on Reading Text File within the C Programming forums, part of the General Programming Boards category; A.Hill D.Lee W.Ang K.Ree E.Alexander B.Harvard A.Ericson I'm trying to get the above lines from the file. I want to ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    23

    Reading Text File

    A.Hill D.Lee W.Ang K.Ree
    E.Alexander B.Harvard A.Ericson

    I'm trying to get the above lines from the file. I want to read the student names of each line with one variable. I want to return the variable each time with the next student name. Here's what i have so far...but this only gives the first name and it loops infinitely.

    while (sscanf (stdntName, "%20s", stdnt))
    {
    printf("%s\n", stdnt);
    }

  2. #2
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    Well, your loop probably wants to be related to the file & deciding if it has reached the end of it or not. And you could probably use something like fgets in a loop, testing for a return or linefeed character (in order to read each line of the file into a string).
    Demonographic rhinology is not the only possible outcome, but why take the chance

  3. #3
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    You can use the fgets function to read complete lines from file
    and the strtok function to break up the line in separate names.

  4. #4
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>while (sscanf (stdntName, "%20s", stdnt))
    scanf doesn't return 0, ever :-) It returns either the number of items converted or EOF, which is negative, if an error happened. Your loop would be better like this
    Code:
    while (sscanf (stdntName, "%20s", stdnt) == 1)
    or better yet
    Code:
    while (fgets(stdntName, 20, stdnt) != 0)
    *Cela*

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    scanf doesn't return 0, ever :-) It returns either the number of items converted or EOF, which is negative, if an error happened.
    It returns 0 if the number of items converted is zero.
    The scanf function returns the value of the macro EOF if an input failure occurs before any conversion. Otherwise, the scanf function returns the number of input items assigned, which can be fewer than provided for, or even zero, in the event of an early matching failure.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. Reading Character at a time from a text file
    By Giania in forum C Programming
    Replies: 8
    Last Post: 02-25-2006, 02:17 PM
  5. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 01:59 PM

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