c++ file reading

Started by
15 comments, last by Servant of the Lord 11 years, 7 months ago
If this could help some then cool! However, since you're working in text files - let's say for instance you have:


Level TItle: The Doom Room
Models: 25
Textures: 1000
....


if you're using fstream you can do:


char input;
fin = file.open("map1.txt");

fin.get(input);
while(input != ":") {
fin.get(input);
}

fin >> MapName;

fin.get(input);
while(input !=":") {
...
}



This is just a suggestion what I used previously for my model file until I converted the file to binary. Just throwing it out there. My example is pretty straightforward and easy. I'm sure others may have preffered ways of reading a text file.
Game Engine's WIP Videos - http://www.youtube.com/sicgames88
SIC Games @ GitHub - https://github.com/SICGames?tab=repositories
Simple D2D1 Font Wrapper for D3D11 - https://github.com/SICGames/D2DFontX
Advertisement
try with something like this.

a 3.5 3.5 yay_some_text
a 3.2 4.5 yay_some_more_text


ifstream f;
f.open(file);
char dat[256];
for(;;){
if(!f){
break
}
f >> dat;
if(strcmp(dat,"a") == 0){
float x, y;
char txt[256];
f >> x >> y >> txt;
}
}

try with something like this.

a 3.5 3.5 yay_some_text
a 3.2 4.5 yay_some_more_text


ifstream f;
f.open(file);
char dat[256];
for(;;){
if(!f){
break
}
f >> dat;
if(strcmp(dat,"a") == 0){
float x, y;
char txt[256];
f >> x >> y >> txt;
}
}



That code risks causing a buffer overflow though, you could do: f >> setw(256) >> dat to ensure it doesn't happen when you read data. (or just use c++ strings)

strcmp is also potentially dangerous if dat isn't null terminated. (This is exactly why one should use the C++ string and stream objects rather than the C functions)
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!

This nags at me a bit too: http://stackoverflow...answer-5165293


Things to point out and quote from there would be:

"EDIT #2: I've found that the slow down for me was due to the set insert"

"Which gives a slowdown of a whooping 17%.
This takes into account:
automatic memory management (no buffer overflow)
automatic resources management (no risk to forget to close the file)
handling of locale"

Unless you deal with a huge text file, 17% are nothing compared to what you get in return and once you manually do the equivalent, chances are your own code would be as "slow" or even slower.
f@dzhttp://festini.device-zero.de
Guess i should replay my fix ^^.


std::vector<string> MapList; //The vector where to hold what Maps to hold
int HowManyMaps = 0; //Keaping this for later
std::string tempString;

std::fstream tempStream("./Data/Map.txt");
if(tempStream.is_open)
{
tempStream >> HowManyMaps;
tempStream.ignore(2);
for(int a = 0; a < HowManyMaps; a++)
{
tempStream >> tempString;
tempStream.ignore(1);
MapList.push_back(tempString);
}
}
tempStream.close();

//Exit
for(int x = 0; x < HowManyMaps; x++)
{
MapList [x].clear(); //clear the strings
}
MapList.clear();


only problem i got is that i can save file as "Text.gam" then open it with fstream as "Text.gam"
but if i open it with Notepad... It becomes "Text.gam.txt" file =,= so anoing

Guess i should replay my fix ^^.


std::vector<string> MapList; //The vector where to hold what Maps to hold
int HowManyMaps = 0; //Keaping this for later
std::string tempString;

std::fstream tempStream("./Data/Map.txt");
if(tempStream.is_open)
{
tempStream >> HowManyMaps;
tempStream.ignore(2);
for(int a = 0; a < HowManyMaps; a++)
{
tempStream >> tempString;
tempStream.ignore(1);
MapList.push_back(tempString);
}
}
tempStream.close();

//Exit
for(int x = 0; x < HowManyMaps; x++)
{
MapList [x].clear(); //clear the strings
}
MapList.clear();


Some critiques:

  • [font=courier new,courier,monospace]is_open[/font] is a function, not a variable. You need parenthesis after it (right now, you're not actually calling it), like so: [font=courier new,courier,monospace]is_open()[/font]
  • You should just use the function [font=courier new,courier,monospace]good()[/font] instead of [font=courier new,courier,monospace]is_open()[/font], because [font=courier new,courier,monospace]good()[/font] will check for more errors than [font=courier new,courier,monospace]is_open()[/font].
  • You don't need to manually close() the stream. It will close itself automatically when it goes out of scope. There's nothing wrong with closing it, of course, but it's not necessary.
  • You don't need to manually clear() the strings or the vector. They will automatically be cleared and free their memory when they also go out of scope.
  • What happens if you've got a corrupt file (or someone maliciously edits it) so that [font=courier new,courier,monospace]HowManyMaps[/font] is incorrect? If you're going to loop over all the elements in a vector, use the vector's [font=courier new,courier,monospace]size()[/font] member to make sure you don't go over/under bounds (but here in this case the entire iterating and clearing over the vector is unnecessary, as stated). Also, [font=courier new,courier,monospace]for(int a = 0; a < HowManyMaps; a++)[/font] should also be checking if the [font=courier new,courier,monospace]tempStream[/font] is still [font=courier new,courier,monospace]good()[/font], because it's possible you're trying to read more maps than the file has (if it's a corrupt/incorrect file), so it should be: [font=courier new,courier,monospace]for(int a = 0; a < HowManyMaps && tempStream.good(); a++)[/font]...


The hard thing about C++ is that because things might "work," but it doesn't mean they're right.


only problem i got is that i can save file as "Text.gam" then open it with fstream as "Text.gam"
but if i open it with Notepad... It becomes "Text.gam.txt" file =,= so anoing

Ditch Notepad and get Notepad++. It's infinitely better.
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
When I don't care too much about speed, I handle that type of thing in distinct re-usable functions (from a lack of deep familiarity with proper stream usage).

In your case, I might do it like this:
//Load the file into a single string.
std::string fileContents = LoadFileAsString("text.txt");


//Remove comments.
fileContents = String::RemoveComments(fileContents, "//");
fileContents = String::RemoveComments(fileContents, "/*", "*/");

//Seperate the string into lines; this also eliminates empty lines via a callback function.
StringList lines = String::Seperate(fileContents, '\n', RemoveEmptyLines);
if(lines.empty())
{
//The file (barring empty lines and comments) is empty!
//Handle the error.
}

//Get the first line.
int line = 0;
int numberOfFiles = StringToInt(lines[line++]); //You can also use lines.at() and catch the exception for malformed files.

if(lines.size() > (line + numberOfFiles))
{
//Malformed file. Not as many lines as promised by 'numberOfFiles'.
//Handle the error.
}

//Find the iterators to the range of files we want.
auto beginningOfFiles = (lines.begin() + line);
auto endOfFiles = (beginningOfFiles + numberOfFiles);

//Copy the range of elements to their own vector.
StringList files;
std::copy(beginningOfFiles, endOfFiles, files.begin());

//Continue processing the file...
line += numberOfFiles;

//...more file processing...


Most of those functions exist already exist in my code base through repeated needs, though RemoveComments() does not (I've never had to deal with multi-line comments before, but that's easy enough to solve - and once solved, becomes a permanent tool in your library, ready to use).

String::Seperate() function. Currently just takes a boolean to check for empty lines, but after writing this post I realize I need a callback function to check for non-empty but whitespace-filled lines. I also have it on my todo list (but as low priority) to make a version for if 'divider' is a single char (which would be alot faster).
StringList is just a typedef for std::vector<std::string> since I use it so often.

//Divides up a string into multiple segments seperated by 'divider', and returns each segment in a StringList.
//If any segment is empty, and if 'ignoreEmptySegments' is true, the empty segments are not added to the StringList.
StringList Seperate(const std::string &str, const std::string &divider, bool ignoreEmptySegments)
{
StringList stringList;
//Check for empty string.
if(str.empty() || divider.empty())
return stringList;
size_t start = 0;
size_t end = str.find(divider, start);
//Keep looping, as long as there are more segments.
while(end != std::string::npos)
{
std::string subString = str.substr(start, end - start);
if(subString.size() > 0 || ignoreEmptySegments == false)
{
stringList.push_back(subString);
}
start = end + 1;
end = str.find(divider, start);
}
//Get the final (or the only) segment.
std::string subString = str.substr(start, str.size() - start);
if(subString.size() > 0 || ignoreEmptySegments == false)
{
stringList.push_back(subString);
}
return stringList;
}


LoadFileAsString() function.
//Loads the entire file, newlines and all, and returns its contents as a string.
//Returns an empty string if the file doesn't exist.
std::string LoadFileAsString(const std::string &filename, ReadMode::Enum readMode)
{
//Open the file for reading.
std::ifstream file;
if(readMode == ReadMode::Binary)
file.open(filename.c_str(), std::ios_base::in | std::ios_base::binary);
else
file.open(filename.c_str(), std::ios_base::in);
if(!file)
{
Log::Message(MSG_SOURCE("FileFunctions", Log::Severity::Error)) << "Failed to load '" << Log_HighlightCyan(GetFilenameFromPath(filename)) << "' at " << Log_DisplayPath(filename)
<< "\nDo I have sufficient privileges? Does the file even exist?" << Log::FlushStream;
return "";
}
//Read the file into the stringStream.
std::ostringstream stringStream;
stringStream << file.rdbuf();
//Convert the stringStream into a regular string.
return stringStream.str();
}



Observant programmers will notice that most of my code suffers from [s]Shmuel[/s] Schlemiel the Painter algorithms. Many of the functions could be individually optimized, and the use of them together could further be optimized into a stand-alone function, like a "LoadFileAsStringList()" since I frequently go from File to String to StringList (easy enough to add).
I typically prefer clean code that works before I start optimizing code, and I don't do the actual optimizations until I find them necessary (otherwise I find myself wasting inordinate amounts of time of things that don't matter much). With file parsing, when I occasionally need files parsed fast, I load it as binary and don't bother with text parsing anyway.

This topic is closed to new replies.

Advertisement