ifstream readfile (string("Level/Level1.txt").c_str());
for (unsigned int i=0;i< /* ? */ ;i++)
{
Level.push_back(EmptyString);
readfile>>Level;
}
readfile.close();
linecount in fstream
I want to code a little level parser for my game, so i thought dynamic file length would be something nice :) . So I've started with a vector<string> but now i need to know how often my loop that creats another string has to run. Does someone know how to get the lines?
Atm it looks like this:
First of all,
ifstream readfile (string("Level/Level1.txt").c_str());
is nonsense..
ifstream readfile ("Level/Level1.txt");
is faster, smaller and safer, and does the same job.
Second, you want to use something like
while (!EndOfFile(file)) do ....
I don't know how it's done with streams though..
also, you might be interested in this
ifstream readfile (string("Level/Level1.txt").c_str());
is nonsense..
ifstream readfile ("Level/Level1.txt");
is faster, smaller and safer, and does the same job.
Second, you want to use something like
while (!EndOfFile(file)) do ....
I don't know how it's done with streams though..
also, you might be interested in this
Quote:Original post by philipptr
So I've started with a vector<string> but now i need to know how often my loop that creats another string has to run.
The C++ iostreams have state flags that can tell you things like the end of file, iostreams are also implicitly convertible to bool so you can do a quick status check of a stream using the idiomatic:
while(io_stream) or while(!io_stream)if(io_stream) or if(!io_stream)
Quote:Original post by philipptr
Does someone know how to get the lines?
For std::basic_string there is a free-function in namespace std called std::getline in header string and can be used with any iostream type not just file streams. Remember it is not a member function of any stream it is a free-function.
thanks for the first hint ;)
I searched a bit for the EndOfFile thing and found that ifstream has a member eof() so I just use for (unsigned int i=0;!readfile.eof();i++) and it works.
So thanks you've helped quite a lot.
EDIT: thanks snk_kid that tought me some more stuff :)
I searched a bit for the EndOfFile thing and found that ifstream has a member eof() so I just use for (unsigned int i=0;!readfile.eof();i++) and it works.
So thanks you've helped quite a lot.
EDIT: thanks snk_kid that tought me some more stuff :)
I forget to mention that std::getline returns the reference to a stream that was passed in so you can do:
std:string line;while(std::getline(in_stream, line)) { // do some processing with the line // ... Level.push_back(line);}
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement