-
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. :(
-
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;
}
-
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?
-
I put in the check to see if the input file was opened properly, and I didn't get the error message.
-
>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
-
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
-
I get tellg()=-1 after both.
What compiler are you using?
-
getting -1 here as well....VC++ as well...
-
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.
-
I'm using a Borland compiler.
-
-
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.
-
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.