Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Indy

getline - how does this function really work??

This topic is 6304 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am using getline function to read in a text file. What I am trying to do is split the line into two by reading in the first part into the first array and the second part in a second array. However, what I have found is that if I do it the array index number gets doubled. My code is as follows: void LoadPartList() { fstream f; f.open("pl.txt",ios::in); while (!f.eof()) { f.getline(part[x],15,''\t''); //f.getline(description[x],30); x+=1; } ListLoad=true; f.close(); } The text file has 3994 enteries in total, but after loading in the information ''x''=7988. This is wrong and gives me an error (as expected) in Windows. Just as an experiment I tested the following in a Console Windows program and it works. I find that strange. Is there any other method I can use to read in a line and split it up and store it in separate arrays? The line unfortunately is not always the same size so counting the characters would not be suitable. Thanks Indy

Share this post


Link to post
Share on other sites
Advertisement
Your code is probably reading 14 characters, storing the data, incrementing the array index, then reading a tab, storing an empty string, incrementing the index, then repeating. If you have delimiters placed properly, then the numerical limit is only there for safety. Note that getline "extracts up to n - 1 elements", which in this case is 14. If your fields are 15 characters including the tab, then what I detailed above will almost certainly be true.

Edited by - Kylotan on July 19, 2001 12:43:23 PM

Share this post


Link to post
Share on other sites
Also in my experiences testing for while (!EOF) has never realy worked as planed... I am not 100% as for the implumentation, but the EOF is actually a special charachter (ASCII 26 I belive), similar to an ''endl'' being the two byte string \c\n for carrage return and new line. Your best bet might be first reading in the number of values to load THEN loading them within a for loop.

file >> nNumberOfValues;
for (int i = 0; i < nNumberOfValues; i++)
file >> m_Array ;

CodeSmith the Pixel Pusher
www.cs.trinity.edu/~csmith8

Share this post


Link to post
Share on other sites
eof() simply tells you if the last read operation causes an error and that error was an end-of-file rather than any other type of error. It can''t tell you if you''ve read everything yet... you have to attempt to read and fail, just like feof() in the C stdio library.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!