fgetc question

This is a discussion on fgetc question within the C Programming forums, part of the General Programming Boards category; If I use fgetc to read one character at a time in a file, how to I move to the ...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    31

    fgetc question

    If I use fgetc to read one character at a time in a file, how to I move to the next line after I encounter a new line character and loop until I hit end of file? thanks for any help.
    'The bigger they are, the harder they fall' ~Yang

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    C input is a stream. That means that each character is treated equally when you use fgetc. So you move to the next line simply by removing a newline character from the stream. Any special treatment of lines is done in the processing of your loop:
    Code:
    int c, lines = 0;
    while ( ( c = fgetc ( fin ) ) != EOF ) {
      if ( c == '\n' )
        lines++;
    }
    printf ( "Approximate # of lines: %d\n", lines );
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    15
    Thats just silly. you dont have new lines you dont actually no you are on a new line if you want to count the new lines then you will use \n but you dont actually move to the next line

    EOF is the end off the file or you can look for a minus 1 '-1'

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >but you dont actually move to the next line
    How don't you? You end processing of the last line and begin processing the next one by taking the line separator off of the stream. You're trying to be too literal and in this case that breeds confusion.

    >or you can look for a minus 1 '-1'
    Or not. EOF is not guaranteed to be -1.
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    15
    because its not actually a NEW LINE is it. its just another char.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >because its not actually a NEW LINE is it. its just another char.
    *sigh*
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Mar 2004
    Posts
    15
    this doesnt give the best advice does it makes it even more confusing.

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >this doesnt give the best advice does it makes it even more confusing.
    I have a sound I'd like you to hear: *plonk*
    My best code is written with the delete key.

  9. #9
    Registered User
    Join Date
    Mar 2004
    Posts
    15
    Plonk LOL

    all im saying is you need to explain it better by what you have written it looks like you are physically moving to the next line that isnt true is it. i know what your saying you do but the other dude isnt going to have a clue.

  10. #10
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    Originally posted by liams7
    this doesnt give the best advice does it makes it even more confusing.
    Sorry liams7, Prelude is correct. It is you that is confusing the matter. You may wish to study further. Check out some tutorials.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  11. #11
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    Originally posted by liams7
    i know what your saying you do but the other dude isnt going to have a clue.
    Is the dude a friend of yours and asked you what Prelude meant? If not, how do you know the dude didn't understand? I don't see the dude posting for further explanation so I'd say he understood.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  12. #12
    Registered User
    Join Date
    Mar 2004
    Posts
    15
    this is so rubbish

  13. #13
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >by what you have written it looks like you are physically moving to the next line
    It depends on what you mean by "physically moving". The 'file pointer' moves forward by one character. The character it moves over is '\n', which most programs treat as a line terminator. The next character to be read is logically the first character of the next line.

    >that isnt true is it
    Explain to me how I'm wrong then.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  2. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  3. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  4. Question about linked lists.
    By cheeisme123 in forum C++ Programming
    Replies: 6
    Last Post: 02-25-2003, 12:36 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

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