I'm trying to read characters from file but i receive a segmentation fault when i have variables declared outside main. If i delete them and try to read from file, everything works smooth. I am using dev c++ 5.7.1 with mingw 4.8.1 the portable version.
I've isolated the problem to just having a few variables declared and the file reading code. This piece of code comes from a bigger app that i am trying to develop(i am learning game development on my own).
#include <iostream>
#include <fstream>
bool running = true;
bool musicPlaying = true;
bool read = false;
int levelNumber;
int barWidth = 40;
int barHeight = 10;
float timeElapsed;
int main(int argc, char** argv) {
std::ifstream file;
file.open("test.txt");
std::cout<<file.is_open()<<std::endl;
char c;
while(!file.eof()){
file.get(c);
std::cout<<c;
}
return 0;
}
I get the output: 1, the file is open. But no character is read from file. Debug information
Why is this happening?
As per your answer to my comment, declaring bool read
overrides read
function which is used by the fread
.
Either rename it or move it to another namespace or class.
None of my compilers could reproduce the same issue. And it's very strange behavior because at least the declaration of 'read' should be the same as the original one for a proper override. Maybe your stdlib is calling read in a different way.