Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getline from File bug?

Status
Not open for further replies.

FudgeFU

Programmer
May 29, 2002
3
AU
Hey peeps. Im getting this weird error. Dont know if its visual c or me (prolly the latter)

Trying to read text in from file, simple:

mystream.getline(mystring, 100);

im printing it out to the prompt to check if everythings ok. Works except for a few lines (prolly 1% of lines) that have the first char of the line missing. The rest of the 500 lines in the file are there. Just missing the first character on some lines.

I thought it had something to do with getline being dodgy so i downloaded the service pack, but that didnt work.

Thanks in advance

Fudge
 
Most possibly there is something wrong with your code. Post your code and you will be helped out.
 
while(!myfile.eof())
{
putmarker = myfile.tellg();
myfile.getline(stringline, 100);

if (isEntry(stringline))
{
myfile.seekp(putmarker);
myfile << &quot;[Entry_&quot;;
myfile << setw(5) << setfill('0') << currentindex;
myfile << &quot;]&quot; << endl;
currentindex++;
}
}
 
Although I am not sure, It seems like your seekp() changes the file pointer and you don't handle it back properly. I don't know whether you have moved the pointer to the next line or not after you put something to the current line of your file.
 
Oh Ok,

I thought that seekp/tellp and seekg/tellg were two different values. One for putting, one for getting. I checked to see if I played around with the get pointer, because thats where its going wrong. But im not playing around with the get, so I assumed it should be getting the entire line.

I was also thinking perhaps the problem stems from the getline function grabbing everything till it hits a delimeter. perhaps one of the delimeters (besides \n newline) confuses the function into grabbing more than the delimeter character. ie. grabbing the delimeter char PLUS another character.
 
If you debug your program, you can see your &quot;getline&quot; reads the string starting from where you finished writing your string back to the file. That means your write process does change the read pointer.
I think you can check if you still get the problem if you omit those write statements.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top