• Advertisement
Sign in to follow this  

problem with fstream i/o

This topic is 4734 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 having problems using std::string's with the fstream io functions. when i read the file, the string still has no data in it heres the code...
BOOL cUI :: LoadInterface()
{
	fstream uiFile("Data\\Interface\\Interface.UI",ios::in);
	string input;

	if (uiFile.is_open())
	{
		while (uiFile.eof())
		{
			uiFile.getline((char *)input.c_str(),100);

			if (input.find("//") == string::npos && input.find("") == string::npos)
			{
				// does string stuff
			}
		}

		return TRUE;
	}
	else if(!uiFile.is_open())
	{
		uiFile.close();
		return -1;
	}

	return TRUE;
}



any help would be great thx

Share this post


Link to post
Share on other sites
Advertisement

uiFile.getline((char *)input.c_str(),100);


c_str returns a const pointer to the characters in the string - it can be used for reading the string only, not writing to it. Instead of using ifstream::getline, you want to use the non-member function version of getline, which takes a string as a parameter. This has the added bonus of not needing to pass in a maximum length - the string will be resized. So, use:


getline(uiFile, input);

Share this post


Link to post
Share on other sites
i tried it but i dotnt hink it worked. i dont have my font engine up yet so i cant write to the screen so i tried to write it to a text file. When i did, it was blank. It could be because i am doin it wrong but i dunno this is wat i was doin. fwrite << input;

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
At a glance I think the problem is that you need to change:

while (uiFile.eof())

to

while (!uiFile.eof())

Share this post


Link to post
Share on other sites

while(getline(uifile,input))
{
if (input.find("//") == string::npos && input.find("") == string::npos)
{
// does string stuff
}
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement