-
Reading a file.
Hey ! I have this simple code to read a file and retrieve its content but it's not working properly (it ouputs default values instead of in-file values) = /
Here's the content of the test file:
Code:
r
yyyyyyyyyyyyyyyy
ggggggggggggg
rrrrrrrrrrrrrrrrrrrrrr
While stepping through my code with the debugger, it seems that it jumps over the while() condition loop but I don't get why. peek() isn't supposed to return EOF since I used seekg().
Here's the function:
Code:
#include "game_load.h"
const unsigned int* Board::OpenFileContent(const char* file_path) {
int cur_pos = 0;
std::string line;
std::ifstream file(file_path);
while(file.peek() != EOF) {
std::getline(file, line);
if(line.size() > LineLength)
LineLength = line.size();
LineNb++;
}
Array = new unsigned int[LineLength * LineNb];
file.seekg(0, std::ios::beg);
while(file.peek() != EOF) {
std::getline(file, line);
for(int i = 0; i < line.size(); i++) {
switch(line[ i ]) {
case 'g':
Array[cur_pos] = B_GREEN;
break;
case 'y':
Array[cur_pos] = B_YELLOW;
break;
case 'r':
Array[cur_pos] = B_RED;
break;
case ' ':
default:
Array[cur_pos] = B_EMPTY;
}
cur_pos++;
}
while(cur_pos < (cur_pos / LineLength + 1)) {
Array[cur_pos] = B_EMPTY;
cur_pos++;
}
}
return Array;
}
And here's how I use it:
Code:
#include "game_load.h"
#include <iostream>
int main() {
Board b;
const unsigned int* dat = b.OpenFileContent("test.dat");
for(int y = 0; y < b.GetLineNb(); y++) {
for(int x = 0; x < b.GetLineLength(); x++)
std::cout << dat[y*x + x];
std::cout << std::endl;
}
}
All of this is compiled using Microsoft's Visual C++ v7.1. Is there any obvious detail I didn't think of or is there any trick I am not aware of ?
Edit: Closing the file and reopening doesn't change the results. The while() condition loop is still skipped. =/
I also called fail() and it returns true which means a 'failure extracting from (the) stream'(C++ reference) occured.
Also, calling exceptions() and catch()'ing exceptions gives me a segmentation fault which is weird...
Thanks.
-
clear() reset flag bits
check out vector no need peek()
Kuphryn
-
I already tried clear() as well. No change at all. Forgot to say that.
-
Considering what you are doing with the data later on, you might just want to read the file character by character and make your life a bit easier, instead of peeking and grabbing whole lines.
-
@Citizen: I find it easier to grab whole lines as I'm used to it. Thanks, though.
It worked when I put clear() before seekg() which is logical when I think about it. However, now it outputs a lot of junk. Stepping through the debugger tells me that line contains the right string on the getline() line... but on the for() line (which is right after), it contains a lot of junk. What the hell does that mean ?
-
I don't understand what all this peek should be good for. Just use the normal way to read from a stream line by line
Code:
while( std::getline(file, line) ) {
if(line.size() > LineLength)
LineLength = line.size();
LineNb++;
}
Kurt
-
I didn't know about that trick, thanks.
Apparently, what's messed up is how I show the content of the array. When I displayed it on a single line it displayed all the correct values. I'll have to check back my algorithm = /
Thanks all !