problem with fstream i/o

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

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 on other sites
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 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 on other sites
At a glance I think the problem is that you need to change:

while (uiFile.eof())

to

while (!uiFile.eof())

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

1. 1
2. 2
Rutin
20
3. 3
4. 4
frob
13
5. 5

• 9
• 14
• 10
• 9
• 17

×