2

I want to check a file is present in C drive or not..? can any one tell me how ?

Update:

I got errors, I am using VC++ 2008

#include "stdafx.h" #include <stdio.h> int main(int argc, _TCHAR argv[]) { FILE * f = fopen("C:\\Program Files (x86)\\flower.jpeg"); if (f == NULL) { file_exists = FALSE: } else { file_exists = TRUE; fclose(f); } return 0; } 

Update 2

When trying to cut and paste code from the linked example below:

#include "stdafx.h" #include <windows.h> #include "Shlwapi.h" int tmain(int argc, _TCHAR argv[]) { // Valid file path name (file is there). TCHAR buffer_1[ ] = _T("C:\\TEST\\file.txt"); TCHAR *lpStr1; lpStr1 = buffer_1; // Return value from "PathFileExists". int retval; // Search for the presence of a file with a true result. retval = PathFileExists(lpStr1); return 0; } 

I am getting this error:

files.obj : error LNK2019: unresolved external symbol __imp__PathFileExistsW@4 referenced in function _wmain 
0

4 Answers 4

3

It's not the code's problem, this is a link error as it said. you should add this to your code which tell the linker to use "shlwapi.lib" library.

#pragma comment( lib, "shlwapi.lib") 

In my case, it solve the problem. In windows ,there is not _stat thing. But luckily, you don't need to open it to check its validity, the PathFileExists is the exact function you need.

Sign up to request clarification or add additional context in comments.

Comments

2

Given you mention C drive, I'm assuming you can use the Windows API, if so PathFileExists(LPCTSTR) will do you

5 Comments

#include "stdafx.h" #include <windows.h> #include "Shlwapi.h" int _tmain(int argc, _TCHAR* argv[]) { // Valid file path name (file is there). TCHAR buffer_1[ ] =_T("C:\\TEST\\file.txt"); TCHAR *lpStr1; lpStr1 = buffer_1; // Return value from "PathFileExists". int retval; // Search for the presence of a file with a true result. retval = PathFileExists(lpStr1); return 0; } I am getting this error 1>files.obj : error LNK2019: unresolved external symbol __imp__PathFileExistsW@4 referenced in function _wmain 1>C:\Users\user1\Desktop\files\Debug\files.exe :
Are you linking to Shlwapi.lib? Project Property Pages > Linker > Input > Additional Dependencies
Yes, i did add Shlwapi.lib to the project
Are you sure you've added it in the right place, and for all configurations? If you're building in debug and only add it to the release configuration then it won't work. I've just built a sample app, got the error without the lib being linked and fixed it by linking.
[These functions are available through Windows XP Service Pack 2 (SP2) and Windows Server 2003. They might be altered or unavailable in subsequent versions of Windows.]
0

Sure - just try opening it and see if it fails:

#include <stdio.h> int file_exists = 0; FILE * f = fopen("C:\\foo.dat", "rb"); if (f != NULL) { file_exists = 1; fclose(f); } 

Comments

0

You don't have to open it - just get its status using stat or _stat

#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> struct _stat buffer; int status; ... status = _stat("mod1", &buffer); 

There are also Windows API functions to give you more control

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.