1

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?

  • 3
    I suggest updating the compiler. GCC 4.8.1 is very old. Also, see https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons – HolyBlackCat Nov 02 '19 at 08:27
  • As a new user here, start with the [tour] and read [ask]. Concerning your issue, please extract and provide a [mcve] as part of your question. Unused globals should have been stripped unless relevant. Further, don't post pictures if you can use text instead. Also, that `while (!eof){...}` doesn't work, just do a little research on it. – Ulrich Eckhardt Nov 02 '19 at 08:42
  • 2
    Does this answer your question? [Why is iostream::eof inside a loop condition (i.e. \`while (!stream.eof())\`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – OznOg Nov 02 '19 at 09:04
  • 1
    Your code is running well with my compiler. Try to comment out ```bool read = false; ```. it may conflict with read function. – Soheil Armin Nov 02 '19 at 09:32
  • Perhaps `while (file.get(c)) std::cout << c;`? – David C. Rankin Nov 02 '19 at 09:47
  • @SoheilArmin that would lead to a compiler "redefinition" error. And GCC 4.7.1 and up don't throw this error anymore. – JHBonarius Nov 02 '19 at 10:15
  • @SoheilArmin you were right! The read boolean did conflict with the read function. Commenting it or changing the name to read2 solves the problem. Also, i updated the compiler to 8.2.0(this was the most up to date portable version i could find) and the problem persisted. There seems to be no problem in Visual Studio. – JungleCoder Nov 04 '19 at 05:04

1 Answers1

1

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.

Soheil Armin
  • 2,145
  • 1
  • 4
  • 17