In my project, there are multiple lines of input and I am getting a strange bug where after the first line, the ifstream cuts off characters based on how far away it is from the end of the file. (If there are 3 lines, line 1 is fine. line 2 is missing the first 2 chars from the line. Line 3 is missing 1 char from the line.)
An example of input/output:
input:
John Smith
Jane Doe
Donald Trump
output:
whole line:John Smith*
What's in the stream?:John Smith.
Next line...
whole line:Jane Doe*
What's in the stream?:ne Doe.
Next line...
whole line:Donald Trump*
What's in the stream?:onald Trump.
Code:
Input1 function:
void input1(ifstream& in, LinkedList &linList)
{
string wholeLine;
int len = in.tellg();
while(getline(in,wholeLine)){
in.seekg(len);
string name;
vector<double> xArr;
vector<double> yArr;
cout << "whole line:" << wholeLine << "." << endl;
string streamCheck;
int tempLocForCheck = in.tellg();
getline(in,streamCheck);
in.seekg(tempLocForCheck);
cout << "What's in the stream?:" << streamCheck << "." << endl;
name = getName(wholeLine); //Call to getName function
string throwAway;
getline(in,throwAway);
cout << "\t\tNext line..." << endl;
len = in.tellg();
}
}
getName function:
string getName(string inName){
stringstream in(inName);
string name = "";
bool notNumber = true;
while(in.peek() != '\n' && notNumber){
string letter(1,in.peek());
if(regex_match(letter ,regex("^[A-Za-z\\s]+$"))){
name += in.get();
}
else{
notNumber = false;
}
}
return name;
}
Of course, this code has been simplified to the point where anything that doesn't touch the ifstream has been excluded. I tested the code as is and the problem still persists.
I have the throwAway
at the bottom because in the actual use of the code, I will have extraneous data at the end of the line.
As you can see, it seems like the tellg()
is returning a different value than what the getline()
would have you expect. According to the getline()
we are at the beginning of the line. However, according to the tellg()
we are not at the beginning of the line.
I would really appreciate any help with this. I'm sorry there is a lot of code to read through. I did my best to slim it down. I have been fiddling with it for hours trying to fix it but haven't been able to. Thanks.
I don't know if it makes a difference, but I am required to use the MinGW 4.9.2 compiler.