Archived

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

VC++ file handler parsing (or so)

This topic is 5311 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

Hi, I''m currently learning to program in OpenGL (and fairly new to C++) and need some sort of function to read object data. I''m using ifstream. Now, everything works fine if you read from the file in the same namespace where you load the file (IE: ifstream OpenFile("MyObject.3d")), but when you try to read from the open file from a function something goes wrong! When calling the function (and thus parsing the file handler) the first time, everthing''s ok, but after that the file cannot be read anymore. Here''s my function: int ReadIntegerFromFile(ifstream OpenFile) { // variable to store a character char CurrentASCIISign=0; // variable to store returnvalue int ReturnValue=0; // Variable to store wether or not integer in file is negative bool NumberNegative=false; // check to see if not end of file was reached if(OpenFile.eof()) { exit(0); } while(1) { OpenFile.get(CurrentASCIISign); // Read current ascii sing from file and store it in CurrentASCIISign // If the current ascii sign is space or enter, then we know we have finished determining the integer if(CurrentASCIISign=='' '' || CurrentASCIISign==''\n'' || OpenFile.eof()) { if(NumberNegative) ReturnValue*=-1; break; } // Make sure the current sign is a number (ASCII representation is 48 - 57) if(CurrentASCIISign>=48 && CurrentASCIISign<=57) { // since we are reading the numbers from left to right, we want to multiply the current value of // NumberOfPolygons by 10 each time we read a new number ReturnValue*=10; // Then add the latest number to it ReturnValue+=(CurrentASCIISign-48); } // if not else { if(CurrentASCIISign==''-'') { NumberNegative=true; } else { // exit for there is an invalid sign exit(0); } } } return ReturnValue; } So the first time the function is started everything is just fine, but the second time the OpenFile.get() function returns -52 What am I doing wrong? Should I use something else than ifstream? thanks in advance, Tomas Hi!

Share this post


Link to post
Share on other sites
Thanks, it works just fine now, altho I don''t understand why using a reference solves my problem :''( (I tried using pointers but they didn''t do the job), but thanks anyway!

Share this post


Link to post
Share on other sites