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.