Thread: getline() and file I/O

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663

    getline() and file I/O

    I can't figure out a problem I'm having. The input file looks like this:

    input.txt:
    david5555555555555555
    sally6666666666666666666666

    and I'm trying to use getline() to pick out just the name from each line, and then use ignore() to discard the rest of the line, but my output file ends up looking like this:

    output.txt:
    david

    instead of:

    output.txt:
    david
    sally

    Here is the code:
    Code:
    #include <fstream>
    #include <iostream>
    using namespace std;
    
    int main()
    { 
    	char text[6];
    	char word[6];
    	ifstream inFile("C:\\TestData\\input.txt");
    	ofstream outFile("C:\\TestData\\output.txt");
    	
    	inFile.getline(text, 6);
    	cout<<"tellg(): "<<inFile.tellg()<<endl;
    	//I get tellg()=-1 here???
    	
    	inFile.ignore(1000, '\n');
    	//I thought the get pointer would be 6 here
    	//so I'm trying to discard the rest of the 
    	//char's on the first line up to an including
    	//the '\n' to set up for reading the next line 
    	//of data.
    	
    	inFile.getline(word, 6);
    	
    	outFile<<text;
    	outFile<<word;
    
    	return 0;
    }
    I also tried the same thing creating a dummy variable:

    char overflow[200];

    and doing this:

    inFile.getline(text, 6);
    inFile.geline(overflow, 200);
    inFile.getline(word, 6);

    but I got the same output. I thought with this statement:

    inFile.getline(text, 6);

    getline() was supposed to stop reading from the stream after 5 char's or until a '\n' (the default delimiter) was encountered, and therefore I needed to do something to remove the rest of the char's from the line, but it's not working the way I exepected.
    Last edited by 7stud; 04-30-2003 at 03:37 PM.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I ran your code and it worked fine. Try adding this code after the open:
    Code:
    	ifstream inFile("C:\\TestData\\input.txt");
    	if (!inFile.is_open())
    	{
    		cout << "File not found." << endl;
    		return 1;
    	}

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    I'm running it on VC++ 6, and I think the file was opened properly as evidenced by "david" in the ouput file.

    What did tellg() output for you?
    Last edited by 7stud; 04-30-2003 at 04:24 PM.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    I put in the check to see if the input file was opened properly, and I didn't get the error message.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >What did tellg() output for you?

    It outputs:
    -1 if the file does not exist
    5 if the file exists

    My output file looked like:
    davidsally

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I wonder if it's something to do with the newline char. Try adding a tellg() after the ignore():

    inFile.ignore(1000, '\n');
    cout<<"tellg(): "<<inFile.tellg()<<endl;

    I get:
    tellg(): 5
    tellg(): 23

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    I get tellg()=-1 after both.

    What compiler are you using?

  8. #8
    Registered User
    Join Date
    Feb 2003
    Posts
    162
    getting -1 here as well....VC++ as well...

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Jamsan,

    What does your ouput file look like? The same as mine?

    The code seems like it should work, and it apparently does with another compiler. I don't get it.

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I'm using a Borland compiler.

  11. #11
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Thanks swoopy.

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    7stud, there is/was a bug with the <string> version of getline. See RoD's post here:

    http://cboard.cprogramming.com/showt...hlight=getline

    However you are using char arrays.

  13. #13
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    swoopy,

    Thanks. I'm aware of that bug, and it just requires you to hit enter twice after your input. The posted problem in that thread actually had nothing to do with that bug.

    Someone suggested I try get() instead of getline(), and get() works. However, the way I understand it, the only difference between getline() and get() is that getline() removes the delimiter from the stream, but since in my example the read ends before hitting the delimiter, the functions should work the same.
    Last edited by 7stud; 05-01-2003 at 01:55 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help needed with file I/O
    By vishalbhingarka in forum C++ Programming
    Replies: 17
    Last Post: 11-30-2005, 11:20 AM
  2. Help with getline() and file reading
    By evilkillerfiggi in forum C++ Programming
    Replies: 5
    Last Post: 10-15-2005, 03:49 PM
  3. getline function to read 1 line from a text file
    By kes103 in forum C++ Programming
    Replies: 3
    Last Post: 10-21-2004, 06:21 PM
  4. problem with getline() file I/O
    By hyperion in forum C++ Programming
    Replies: 4
    Last Post: 12-08-2003, 09:34 AM
  5. getline i/o
    By kylesbigdog in forum C++ Programming
    Replies: 8
    Last Post: 04-23-2003, 08:25 AM