fstream ?

This is a discussion on fstream ? within the C++ Programming forums, part of the General Programming Boards category; hrmm i was wondering if someone could jsut write a very simple loop, that reads a line at a time ...

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    91

    fstream ?

    hrmm i was wondering if someone could jsut write a very simple loop, that reads a line at a time from a file.

    something like



    Code:
    istream& Message::read(istream& stream){
    
       while( end of file )
       {
           getline(....?...);
           /* read an int on next line of file? */
           getline(....?...);
       }     
       return stream;
    }

  2. #2
    Registered User
    Join Date
    Jul 2004
    Posts
    91
    ok this is what is happennig

    i have..
    Code:
    while(msg.read(inFile)!=0)
                            msg.print(cout) << endl;
    and the function read is ..

    Code:
    istream& Message::read(istream& stream){
            getline(stream, animalName);
            getline(stream, animalType);
            stream >> age;
            return stream;
    }
    and in my file i'll have
    Jack
    Dog
    3
    Missy
    Cat
    6

    but my output only outputs
    Jack is a Dog and he is 3 years old.


    How come it did not read the Cat?

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,765
    > stream >> age;
    This will leave a newline on the stream
    Which will really upset your attempt at getline() in the next call to read

    It's best to stick to getline to read all lines, then use something else to extract bits of information from a line, if that is what you want to do
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Jul 2004
    Posts
    91
    thanks

  5. #5
    Registered User
    Join Date
    Jul 2004
    Posts
    91
    hrm now that i've change all into getline statements..
    it reads the stuff from the file, but the printout is a bit weird..

    in my file i'll have
    Jack
    Dog
    3
    Missy
    Cat
    6

    but my output only outputs
    Jack is a Dog and is 3 years old.
    Missy is a Cat and is 6 years old.
    Cat and is 6 years old.

    last line???

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,765
    Post the latest code
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Jul 2004
    Posts
    91
    i've been fiddling around with it, and i've got it working...
    not too sure what was going on..
    thanks anyway

  8. #8
    Registered User
    Join Date
    Jul 2004
    Posts
    101
    >>not too sure what was going on..
    Then how are you sure that it is fixed? Bugs do not disappear, and messing with your code randomly is not a good way to try removing them. When you have a bug, find out exactly where it is coming from and what is causing it before you change the code in any way. Now you have code that is suspect because the original bug may be hidden and you may have introduced more bugs by fiddling with the code.

    The best way to regain confidence in your program is to perform regression testing on older versions. The goal is to verify that what worked correctly before continues to work correctly and what was broken before works correctly now.

  9. #9
    Registered User
    Join Date
    Jul 2004
    Posts
    60
    paperbox005>>while( end of file )
    Shouldn't this be while(eof())
    Child who knows C++
    Using Borland C/C++ Compiler 5.5 (Command Line Version)

  10. #10
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    I don't know of any eof function so I'm assuming you meant feof. Given that:
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351

  11. #11
    Registered User
    Join Date
    Jul 2004
    Posts
    60
    Thantos, here is a breif explanation of the eof function: http://mathbits.com/MathBits/CompSci/Files/End.htm
    Quote Originally Posted by the website I posted Above
    End of File Function
    So, just how much data is in that file? The contents of an input file may not be precisely known. Usually the general format of the file and the type of data needed to receive the input are known. The amount of data stored in the file is often indefinite. So, do we spend our time counting data in a text file, or do we let the computer deal with the amount of data? Of course, we let the computer do the counting. C++ provides a special function, eof( ), that returns nonzero (meaning TRUE) when there are no more data to be read from an input file stream, and zero (meaning FALSE) otherwise.

    Rules for using end-of-file (eof( )):
    1. Always test for the end-of-file condition before processing data read from an input file stream.
    a. use a priming input statement before the loop
    b. use an input statement at the bottom of the loop
    2. Use a while loop for getting data from an input file stream. A for loop is desirable only when you know the exact number of data items in the file.
    I'm surprised an experienced programmer, like you Thantos, didn't know this and a noob did.
    Last edited by C++Child; 07-31-2004 at 08:27 PM.
    Child who knows C++
    Using Borland C/C++ Compiler 5.5 (Command Line Version)

  12. #12
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Maybe because the function is a member function and not a function by itself like you had it.
    Last edited by Thantos; 07-31-2004 at 10:07 PM.

  13. #13
    Registered User
    Join Date
    Jul 2004
    Posts
    60
    You said that wasn't a function at all! You said >>Did you mean feof()?
    Child who knows C++
    Using Borland C/C++ Compiler 5.5 (Command Line Version)

  14. #14
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,810
    Is it me or is c++ child one of the most rudest person's. He comes in with such an attitude. Don't insult thantos he could own you anyday of the week and don't even think about questioning his knowlege.
    Last edited by prog-bman; 08-01-2004 at 10:21 AM.
    Woop?

  15. #15
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    I actually think he's a reincarnation of C+++C_forever.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with fstream, one variabile for write and read
    By Smjert in forum C++ Programming
    Replies: 3
    Last Post: 02-03-2009, 10:19 PM
  2. Fstream. I/O
    By kevinawad in forum C++ Programming
    Replies: 2
    Last Post: 07-07-2008, 10:19 AM
  3. ARGH! fstream errors
    By OttoDestruct in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2004, 11:37 PM
  4. Are fstream and ofstream incompatible?
    By johnnyd in forum C++ Programming
    Replies: 5
    Last Post: 03-19-2003, 12:21 PM
  5. Problems with fstreams.
    By mosdef in forum C++ Programming
    Replies: 7
    Last Post: 06-19-2002, 04:36 PM

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