Reputation: 75
At the moment I'm trying to get to create a race track that has all of the textures on the ground loaded via a textfile. An example is this:
0, 0, 1, 1, 1, 1, 0, 0
0, 1, 0, 0, 0, 0, 1, 0
0, 0, 1, 1, 1, 1, 0, 0
The ground itself is simply a grid.
At the moment the code I have to get information from text file is as follows:
string line;
ifstream myfile ("track1.txt");
if (myfile.is_open())
{
while ( !myfile.eof() )
{
getline( myfile, line, ',');
if (line == '0')
{
const wstring textureFileName=TEXT("crate.jpg");
}
myfile>>line;
}
myfile.close();
}
When I build this code, how ever I get the following error:
binary '==' : no operator found which takes a right-hand operand of type
Then if I change the double equals sign to a single I get the following error:
error C2451: conditional expression of type 'std::basic_string<_Elem,_Traits,_Ax>'is illegal 'char' (or there is no acceptable conversion)
Is there something I'm missing that is causing the error?
Sorry for my lack of proper detail.
Thank you chaosTechnician. Will the val work on multiple lines in the text file? Like this
0 0 1 1 1 1 0 0
0 1 0 0 0 0 1 0
1 0 0 0 0 0 0 1
0 1 0 0 0 0 1 0
0 0 1 1 1 1 0 0
I mean, is there anything else I need to type to get the computer to realise the line has ended?
Upvotes: 0
Views: 317
Reputation: 368
If you're concerned about keeping relative spatial data (i.e., rows/columns of each 'tile') intact and you're limiting yourself to ascii files, you might as well just forgo any issues with delimiting or reading invalid input by switching to chars.
Let's say this is your file:
a6sgb1
dsj26s
l8h9h4
34gpoy
And then do do the following to parse it (after testing reading a line from std::getline()
through std::stringstream
, std::get()
, and just plain iterating through a std::string
from std::getline()
on my development machine, I found the first to be fastest - your mileage may vary. I'll supply all three):
stringstream
ifstream strFile("test.txt");
string line;
int row(0);
int col(0);
while(getline(strFile, line)) {
stringstream ss(line);
char ch;
while(ss >> ch) {
switch(ch) {
// your code
}
++col;
}
++row;
}
line iteration
istream lineFile("test.txt");
string line;
int row(0);
while(getline(lineFile, line)) {
int len(line.length());
for(int col = 0; col < len; ++col) {
switch(line[col]) {
// your code
}
}
++row;
}
individual char parsing
ifstream chFile("test.txt");
char ch;
int row(0);
int col(0);
while(chFile.get(ch)) {
switch(ch) {
case '\n':
++row;
break;
// your code
}
++col;
}
Someone else mentioned questions like this should probably be on stackoverflow, I agree.
Upvotes: 0
Reputation: 2542
To expand on chaosTechnician answer the ifstream does not see lines.
every time you call myfile it will read the next value irrespective of its location.
that means that it would read 0 0 0 0 and 0 0 0 0
as the same thing.
the way I use it in my code is to put different data on diffident lines for example
50 //race length
0 0 0 //first square of each lane
0 1 0 //second lane and so on
but it would function exactly the same if it was 50 0 0 0 0 1 0
By knowing what order the information will be read in you can read it into variables at load time and manipulate them as the game goes on.
Good luck with your project
edit:
I use c++ an directx 9.c so this method should work for you.
Upvotes: 0
Reputation: 4026
You're trying to compare a string to a char:
line == '0'
instead, try:
line[0] == '0'
or:
line == "0"
Upvotes: 3
Reputation: 1700
First, I strongly suggest using the code formatting in your post to make it easier to read... Also, the easiest way to see if it's written correctly would be to compile it and run it. :)
If your text file is 0,0,0,0,0,0,0,0,0,0
(everything on one line), then getline(myfile, line);
will read it all in with your first read--it's all on one line. Here's a link to how getline()
works. If you were to use a comma as the delimeter (by passing a comma as the third argument: getline(myfile, line, ',');
) it's much more likely to do what you want. But, if you want to just read each line, why put them on the same line separated with a comma when you could just put each on its own line? Is there a specific reason you're doing it that way?
Additionally, if line
is a string, if(line == 0)
won't check what you want it to check. You'd need to convert line
to a int
to compare values.
If you want to be using ifstream
and the values in the file will be integers, I'd suggest something akin to this:
int val;
ifstream myfile("track1.txt");
while(myfile >> val)
{
//Use the value in here
}
Then your file can be as simple as this:
0 0 0 0 0 0 0 0 0 0
Hope this helps.
Upvotes: 1