Thread: reading text from a file

  1. #1
    Patent Pending GSLR's Avatar
    Join Date
    Sep 2001
    Posts
    134

    reading text from a file

    Hi me again

    ok so i got a program to read a line of text from a file using fgets()function.

    So my next problem is 'how do i tell the program to move on to the next line of text in the file' so that it can be read ?

    Thanx for the help .
    And To All Those Opposed, WELL !!!
    >Deleted< " Looks like a serial no."

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    60
    fgets() atumatically reads the next line
    use something like
    Code:
    while (fgets(..) != NULL) {
    ...
    }
    Last edited by Lau; 05-13-2003 at 08:32 AM.
    "Shallow men believe in luck. Strong men believe in cause and effect."
    -Ralph Waldo Emerson

  3. #3
    Open to suggestions Brighteyes's Avatar
    Join Date
    Mar 2003
    Posts
    204
    >So my next problem is 'how do i tell the program to move on to the next line of text in the file' so that it can be read ?
    Don't think in terms of 'lines', think of a file as a long string of characters. You read one character and automatically move ahead to the next one, it just so happens that one character means a line break ('\n').
    p.s. What the alphabet would look like without q and r.

  4. #4
    Patent Pending GSLR's Avatar
    Join Date
    Sep 2001
    Posts
    134
    Hi

    Thanx for the reply , but what im actually looking to do is read 1 line of text , display it on the screen , press a key and then the next line of text will be displayed and so on until the EOF is reached.

    mmm i wonder if im going about this the wrong way.
    And To All Those Opposed, WELL !!!
    >Deleted< " Looks like a serial no."

  5. #5
    Open to suggestions Brighteyes's Avatar
    Join Date
    Mar 2003
    Posts
    204
    Something like this I suppose:
    Code:
    int main(void)
    {
        /* Open the file */
    
        while (fgets(line, sizeof (line), file))
        {
            fputs(line, stdout);
    
            printf("Continue? (y/n)");
            fflush(stdout);
    
            if (tolower(getchar()) != 'y')
                break;
        }
    
        return 0;
    }
    p.s. What the alphabet would look like without q and r.

  6. #6
    Patent Pending GSLR's Avatar
    Join Date
    Sep 2001
    Posts
    134
    Thanx a lot Brighteyes

    you were a big help.
    And To All Those Opposed, WELL !!!
    >Deleted< " Looks like a serial no."

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, 03:17 PM
  5. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 02:59 PM