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.