Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/136.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ ferror导致访问冲突_C++ - Fatal编程技术网

C++ ferror导致访问冲突

C++ ferror导致访问冲突,c++,C++,我在调用ferror(f)时遇到访问冲突。问题是我检查f是否不是空指针。我还能够在访问冲突发生之前读取文件。它发生在来自的bzip库中(该库仅经过修改,以便可以构建,例如禁用错误并删除多个主函数) 这是我的主要任务 int main() { int e = 0; int *error = &e; FILE *f = fopen("./test", "r"); //open file if (f == NULL) { //Changed, as beforehand both check

我在调用
ferror(f)
时遇到访问冲突。问题是我检查
f
是否不是空指针。我还能够在访问冲突发生之前读取文件。它发生在来自的bzip库中(该库仅经过修改,以便可以构建,例如禁用错误并删除多个主函数)

这是我的主要任务

int main() {
int e = 0;
int *error = &e;
FILE *f = fopen("./test", "r"); //open file
if (f == NULL) {  //Changed, as beforehand both checks happend at the same time
//However the programm passes both checks
    std::cout << "f* is NULL\n";
    exit(1);
}
if (ferror(f)) {
    std::cerr << "Can't open the file " << ferror(f) << '\n';
    exit(1);
}
char *c = char[20];
fread(c, 1, 20, f); // Here we can read succesfully out of the file
std::cout << c;
BZFILE* bzfile = BZ2_bzReadOpen(error, f, 1, 0, NULL, 0); //The failing function call
}
我试着把程序移到另一台电脑上,但还是出现了同样的错误。指针不是空的;这有两个检查,我可以自己调用
ferror
,而不会造成访问冲突。

如果(f==NULL | | ferror(f))//检查f不是NULL,也没有错误
if(f == NULL || ferror(f)) // Check that f isn't NULL nor has an Error
    std::cerr << "Can't open the file " << ferror(f)  << '\n';

cerr看起来不像C.“如果(f==NULL | | feror(f))”不检查f是否为NULL:(除了所有的错误检查都在混合的位置,部分代码使用错误的语言之外,我想知道
c
是否应该是NUL Terminated您是否有
std::cerr可以尝试在调试器下运行?这是一个合理的问题,但是这不是失败的部分,它到达最后一个函数调用,并且事先检查
Exception thrown at 0x0003E5C4 in wikiParser.exe: 0xC0000005: Access violation executing location 0x0003E5C4.
if(f == NULL || ferror(f)) // Check that f isn't NULL nor has an Error
    std::cerr << "Can't open the file " << ferror(f)  << '\n';