C++ 无法读取.raw文件
我将原始文件放在与代码相同的文件夹中,但fopen()没有打开该文件,而是给出了文件错误。 这是密码C++ 无法读取.raw文件,c++,fopen,C++,Fopen,我将原始文件放在与代码相同的文件夹中,但fopen()没有打开该文件,而是给出了文件错误。 这是密码 char strInputFile[] = "test.raw"; pFile = fopen(strInputFile, "rb"); if (pFile == NULL) { fputs("File error", stderr); cout << " press any key to exit"; cout << _getch(); exi
char strInputFile[] = "test.raw";
pFile = fopen(strInputFile, "rb");
if (pFile == NULL) {
fputs("File error", stderr);
cout << " press any key to exit";
cout << _getch();
exit(1);
}
char strInputFile[]=“test.raw”;
pFile=fopen(strInputFile,“rb”);
如果(pFile==NULL){
fputs(“文件错误”,stderr);
cout我在这里做了一些假设,但我认为问题在于.raw文件需要与运行可执行文件的目录相同,而不是与代码所在的目录相同
操作系统对源项目的构造布局一无所知,只知道它被要求打开一个名为test.raw
的文件,并且没有提供任何其他信息,假设它位于当前目录中
如评论所述,您应该检查errno(使用perror打印)确定失败原因。您也可以通过尝试在代码中的同一点创建一个名为say HEREIAM.TXT的文件进行检查,然后搜索该文件,以查找open调用实际查找文件的位置。如注释中所建议,使用perror检查错误
#include <stdio.h>
#include <iostream>
using namespace std;
int main() {
char strInputFile[] = "test.raw";
FILE* pFile = fopen(strInputFile, "rb");
if (pFile == NULL) {
char buf[200] = {0};
perror(buf);
fputs("File error", stderr);
cout << " press any key to exit";
cout << getchar();
exit(1);
}
}
#包括
#包括
使用名称空间std;
int main(){
char strInputFile[]=“test.raw”;
FILE*pFile=fopen(strInputFile,“rb”);
如果(pFile==NULL){
char buf[200]={0};
佩罗尔(buf);
fputs(“文件错误”,stderr);
您知道如何编译和运行代码吗?是否尝试使用peror()
获取fopen()
失败的原因?检测到相对文件路径。正如其他人所说,当前工作目录(CWD)可能与exe(或源)目录不同例如,code::Blocks
就是这样做的。如果我创建一个名为sample
的项目,我将在sample
中有一个main.cpp
文件。如果我随后使用IDE编译和运行,它将创建一些新文件夹。(1)sample\bin
和sample\obj
。每个文件夹中都会有另一个文件夹,debug
或release
。假设发布,我在sample\bin\release\sample.exe
和sample\bin\debug\sample.exe两个文件夹中都有exe。令人困惑的是,当使用IDE启动时,CWD是sample