I'm having a problem using ifstream; I'm more used to using FILE*'s.
I want to read an entire file using an ifstream and store it in a buffer, without loosing formatting. Here's what I have so far:
That only reads one word ('Adventures') from the data file. I did try a little 'double buffering' techinique but I lost all formatting:Code:// Read (): Reads the specified data file // and returns it's contents std::string Dataf::Read (std::string fileName) { std::ifstream fd (fileName.c_str()); std::string buffer; std::string errorBuf ("Error opening data file ["); if (!fd.is_open()) { errorBuf += fileName + "], please check it exists.\n"; KillGame (errorBuf); } fd >> buffer; return buffer; }
Any help appreciated as usualCode:// Read (): Reads the specified data file // and returns it's contents std::string Dataf::Read (std::string fileName) { std::ifstream fd (fileName.c_str()); std::string buffer, buf; std::string errorBuf ("Error opening data file ["); if (!fd.is_open()) { errorBuf += fileName + "], please check it exists.\n"; KillGame (errorBuf); } while (!fd.eof()) { fd >> buf; buffer += buf + " "; } return buffer; }![]()



LinkBack URL
About LinkBacks




I used to be an adventurer like you... then I took an arrow to the knee.