简体   繁体   中英

Can Not Read .raw File

raw file that i have placed in the same folder as that of the code but fopen() is not opening the file instead it is giving the file error. here is the code

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); 
}

the code is giving me the value of pFile as null.

I am making some assumptions here, but I think the issue is that the .raw file needs to be in the same directory as you are running the executable from, not the same directory as the code is in.

The OS doesn't know anything about the layout of how your source project is constructed, all it knows is it has been asked to open a file called test.raw , and given no other information, assumes it is in the current directory.

As comments have stated, you should check errno (use perror to print it) to establish the failure reason. You can also check by trying to create a file at the same point in the code, called say HEREIAM.TXT and then search for that file, to find where the open call is actually looking for files.

As suggested in comments use perror to check the error

#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); 
   }

}

I see:

"No such file or directory"

in my case if I have no test.raw file in working directory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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