Thread: what is wrong with eof?

  1. #16
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Read the FAQ.

    The eof is only set when trying to read some more fails. If it fails, in will not modify what it is trying to input to. It will not be set when you read the last line because that last read is successful. Hence you'll get a dummy "reread" of the last line.

    The solution proposed in this thread (and FAQ) exits the loop as soon as in fails to input anything.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  2. #17
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The only end-of-file character that I'm aware of is Ctrl-Z, and that only works on Microsoft based systems, not on Linux/Unix systems, and of course, only in text-mode on the MS-based systems. The only REASON that Ctrl-Z is recognized as EOF is that in the old days of floppy disks on CP/M, the file size was counted in blocks of 128, 256 or 512 bytes. So a file that was 129, 257 or 513 bytes would have a load of "rubbish" at the end. The solution was to indicate the end of the file with a special character. Note that getting a CTRL-Z into the file may involve more than just pressing that key, particularly in a text-editor where CTRL-Z usually means "undo".

    In modern systems, this is not needed, and you shouldn't need to have any final character in the file. You may, however, need to have a newline ('\n') at the end of the last line for things to work correctly with various types of input.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #18
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > He told me that I have a space in the end of list.txt
    You need to find a better teacher then.

    Your program should be able to deal with any valid (or indeed invalid) file, not just ones which are magically formatted to make your poorly constructed program work.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #19
    Registered User
    Join Date
    May 2007
    Posts
    67
    Thanks to Daved and matsp because of their really careful and patient explain.

    for Salem
    Your program should be able to deal with any valid (or indeed invalid) file, not just ones which are magically formatted to make your poorly constructed program work.
    Your words make me little frustrated,but I know your meaning and that is what I want to do.However,I still don't know how to make my poorly program perfectly fit in any kind of input.
    So I will try,maybe it will be hard.

  5. #20
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> I still don't know how to make my poorly program perfectly fit in any kind of input.

    The example from ZuK in post #5 and repeated by me will handle a file that has any kind of extra whitespace at the end. It will also handle a file that has no whitespace at the end.

    That same code will "handle" any other kind of inpnut error by simply exiting the loop and not processing any more input. If you want to print an error message you would have to do more work, but I doubt that's what you want right now.

    So I hope you're using
    Code:
          while( in>>name>>score[0]>>score[1]>>score[2]>>score[3]>>score[4]>>score[5]
               >>score[6]>>score[7]>>score[8]>>score[9] ) {
    If you are and are still having problems, then let us know what they are. If you aren't, why not?

  6. #21
    Registered User
    Join Date
    May 2007
    Posts
    67
    thank yo Daved.I mean "perfectly" like if there is more than 10 test scores(any number beyond
    10 and you don't know the number of test,which means you can't use score[0]~score[9],of course not score[n])or somebody miss one or two exams and so those score doesn't count in the ave point.How do handle that?

  7. #22
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Yes, that would be more complicated. I missed that part of the question earlier.

    So now the question is how to mark the end of one group of scores. In the file, it will likely be marked with a newline. There are two ways I can think of to identify that newline and separate a score from a name on the next line. One is to use get() to get the character after each score. If that character is a space ' ' then you can get another score. If that character is a newline '\n' then you are done with the current student and you can move to the next one.

    The second idea is to read the entire line into a string using getline (you could read the name first if you wanted). Then use a stringstream to separate the test scores in that line. The reason this is necessary is because during normal usage of operator>> you can't tell the difference between a space and a newline, so you don't know if you are about to read in another score or the next line with a person's name. Using getline stops only at the newline, so it makes that decision for you.

    Now, whichever of those two options you choose, you would have to add a second loop. Your current loop basically loops while there is still data in the file. It loops through each student. You need a second, nested loop to loop through each score for the current student. This loop would end when a newline is found or the end of the stringstream is found. That way it can handle multiple test scores.

    I don't think you would need to store each test score individually, so you could get rid of the score array. You just need to process each score one at a time and keep track of how many you've read in.

  8. #23
    Registered User
    Join Date
    May 2007
    Posts
    67
    I don't know how I could say because you are really a nice kind guy that willing to help the beginner like me.And in fact even I already finish CS 2(this course for C++,and luckily get an A) and taking CS 3A now,I didn't learn stringstream yet and even for getline I didn't use it very well,so I will have a long way to go!
    But now I think the most serious problem for me is for some concepts like string,array etc.I know the basics but for advance use I will be in trouble,and for some problems I know how to do it but don't know how to convert it simply and easily to C++ code,so that is why I sometimes(no,always)ask many stupid questions.So how could I get through this and go into your level?Read more books and do more projects?
    I am reading "solving problems with C++ " 6 edition and "Introduction to programming with C++" by Y.Daniel Liang for my class,and I also have thinking in C++ volume 1 (2 edition) and
    C++ primer 4th edition in weekends,so maybe after I finish these books I will be better!

  9. #24
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Learning new parts of a lagnuage is a combination of reading about them and using them - if you don't do both, you won't learn them. It's like improving your car-driving skills by reading a book - but parts of the skill as a driver is "muscle memory", meaning that you teach your muscles to perform a certain manouver(sp?) quickly without too much thinking effort.

    Similarly, programming isn't all about what you've read, but also about what you know how to use, and the only way to know that you can actually use some particular technique.

    Also, whilst Deved's usage of stringstream is elegant and easy, you could of course solve the same problem using getline and manual string parsing (taking data out of the string and forming the name strings and score numbers from the characters in the string). This should be something you know how to do at a basic level. So knowing stringstream isn't strictly neccessary to correctly solve this problem - but it does make the job much easier.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program exiting
    By sql.scripter in forum C Programming
    Replies: 9
    Last Post: 01-28-2006, 08:51 PM
  2. Anything wrong with this(char to short and int)
    By hckr83 in forum C Programming
    Replies: 3
    Last Post: 12-22-2005, 06:27 PM
  3. EOF messing up my input stream?
    By Decrypt in forum C++ Programming
    Replies: 4
    Last Post: 09-30-2005, 03:00 PM
  4. Dividing and EOF
    By Tride in forum C Programming
    Replies: 7
    Last Post: 09-27-2003, 05:26 PM
  5. Strange EOF happenings
    By fatinez in forum C Programming
    Replies: 6
    Last Post: 09-23-2003, 08:37 PM