简体   繁体   English

尝试从 dev c++ mingw 4.8.1 中的文件读取时收到分段错误

[英]Receiving segmentation fault when trying to read from file in dev c++ mingw 4.8.1

I'm trying to read characters from file but i receive a segmentation fault when i have variables declared outside main.我正在尝试从文件中读取字符,但是当我在 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.我正在使用 dev c++ 5.7.1 和 mingw 4.8.1 便携版。

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.我得到 output: 1,文件已打开。 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 .根据您对我的评论的回答,声明bool read覆盖read fread使用的 function 。 Either rename it or move it to another namespace or class.重命名它或将其移动到另一个命名空间或 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.也许您的stdlib以不同的方式调用 read 。

Welcome to StackOverflow...欢迎来到 StackOverflow...

Firstly, like said your compiler is VERY old.首先,就像说你的编译器很旧。 version 4.8.1, while current G++ is version 9.2.版本 4.8.1,而当前 G++ 是版本 9.2。

Try to prevent using global variables.尽量避免使用全局变量。 It is allowed, but not nice encapsulated design.这是允许的,但不是很好的封装设计。 They are not stored in stack or normal heap, but in some obscure section in memory.它们不存储在堆栈或普通堆中,而是存储在 memory 的某个不起眼的部分中。

Anyhow, you didn't share test.txt, so I made my own containing无论如何,你没有分享 test.txt,所以我自己制作了包含

acb

And cleaned your code并清理了你的代码

#include <iostream>    
#include <fstream>    
int main() {
    std::ifstream file;
    file.open("test.txt");
    std::cout << file.is_open() << '\n';
    char c;
    while (file >> c) std::cout << c;
}

And my output is而我的 output 是

1
abc

Even though you're original code reads past the end of the file, I would really expect some output at least.即使您的原始代码读取到文件末尾,我真的希望至少有一些 output。 Compilers get updated all the time.编译器一直在更新。 Maybe you're actually running into an old bug... You REALLY should update也许您实际上遇到了一个旧错误...您真的应该更新

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM