简体   繁体   中英

Checking to see if a bmp file exists in C++

I create a file, a bmp file, and store it in a certain directory. What I want to check is that it is there after (so whether the creation was successful). I have

FILE *pfile;
pfile = fopen("C:\Users\me\Test-Outputs\Capture Output\test.bmp", "r");

if(pfile != NULL)
    worked!
else 
    didnt work!!

But it's not working. It says the file does not exist even though it does. Anyone know where I'm going wrong?

Use double backslashes. The single backslash is an escape char in C and C-like languages.

You have to escape the backslashes with another backslash. Try

pfile = fopen("C:\\Users\\me\\Test-Outputs\\Capture Output\\test.bmp", "r");

Using forward slashes should also work.

fopen行替换为:

pfile = fopen("C:\\Users\\me\\Test-Outputs\\Capture Output\\test.bmp", "r");  

正如电影导演所说,请使用以下内容:

pfile = fopen("C:\\Users\\me\\Test-Outputs\\Capture Output\\test.bmp", "r");

I think that the better way to do that is use the "access" function.

#include <unistd.h>
int access(const char *pathname, int mode);

It returns zero on success.

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