strings and file io

This is a discussion on strings and file io within the C++ Programming forums, part of the General Programming Boards category; hi...i'm trying to create a program that takes a text file and reads the first line. i can do it ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    84

    strings and file io

    hi...i'm trying to create a program that takes a text file and reads the first line.

    i can do it fine in using c style strings (char) but how can i do it using strings?

    so far i'm thinking something like...

    Code:
    while(!infile.eof())
    {
                    // i need a line here to recognize the end of line
         infile >> line;
         outfile << line << endl;
         cout << line << endl;
    }
    currently i'm using infile.getline(line, 100); and it works.

    thanks,
    barneygumble742

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    There is a different version of getline for strings. Instead of infile.getline(line, 100); use getline(infile, line);. Since line is a string that can resize you don't need the 100 to indicate how many characters to use.

    Also, you should put the getline for either versions inside the while statement so you can detect errors when reading from the file. Using the eof() function like that is not the best way.
    Code:
    while (getline(infile, line))
    {
     ...
    }

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >while(!infile.eof())
    That's a logic error. The eofbit isn't set until after you've tried and failed to read from the stream, so unless you bend over backward to fix it, you'll process the last line of the file twice.

    >currently i'm using infile.getline(line, 100); and it works.
    Then getline(infile, line) should work wonders. You can even put it in the loop condition and everything will "just work", thus solving your actual question, and the logic error that I just described:
    Code:
    string line;
    
    while ( getline ( infile, line ) ) {
      // Process line
    }
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    84
    thanks. you're right...it does work wonders.

  5. #5
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    Quote Originally Posted by Prelude
    >while(!infile.eof())
    That's a logic error. The eofbit isn't set until after you've tried and failed to read from the stream, so unless you bend over backward to fix it, you'll process the last line of the file twice.
    if the last line ends with \n yes. otherwise the getline will reach the end of the file and set EOF

  6. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    84
    another question:

    lets say i have a text file:
    one 2 three 4 five
    six 7 eight 9 ten

    how can i input this into a class with private objects first, second, third, fourth, fifth?

    i just thought of using arrays (or possibly vectors) to take in the first line and then go line by line. is it possible to use ifstream to take in the first line, then separate that line into pieces (objects) and then increment to the next line?

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Overload the stream extraction operator for input streams (>>).

    Code:
    #include <string>
    #include <fstream>
    #include <iostream>
    #include <vector>
    using namespace std;
    
    class object
    {
        string str1, str2, str3;
        int i1, i2;
    public:
        friend istream& operator>>(istream&,object&);
    };
    
    istream& operator>>(istream& is,object& obj)
    {
        is >> obj.str1 >> obj.i1 >> obj.str2 >> obj.i2 >> obj.str3;
        return is;
    }
    
    int main()
    {
        ifstream input("file.txt");
        object temp;
        vector<object> objVect;
    
        if( input.is_open() )
        {
            // Push copies of objects onto vector as long as there is data in file
            while( input >> temp )
                objVect.push_back(temp);
        }
    
        // Do stuff with vector of objects
    
        return 0;
    }
    Assuming each line in the file always consists of a string/int/string/int/string otherwise you may have to consider checking the stream for errors.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    >if the last line ends with \n yes. otherwise the getline will reach the end of the file and set EOF

    EOF should terminate the input by getline() just like finding a terminating char. The difference may well be that EOF will also set the fail bit, but that should be after the input has already been achieved. If the fail bit is set because EOF has been found, then to use the stream for additional input later in the program you would need to clear the stream by calling clear().
    You're only born perfect.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 04:53 AM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 01:36 PM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 04:44 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 11:12 PM

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