quick file question...
-
How do I check if a file exists or not? I have tried the following:
CString f, msg; f = __argv[i]; ifstream infile(f); if(!infile) { msg.Format("Could not open: %s",f); AfxMessageBox(msg); } else AfxMessageBox("file opened");
But if the file doesn't exist, it just makes one!!! Any input is appreciated. Thanks! Nitron _________________________________________-- message sent on 100% recycled electrons. -
How do I check if a file exists or not? I have tried the following:
CString f, msg; f = __argv[i]; ifstream infile(f); if(!infile) { msg.Format("Could not open: %s",f); AfxMessageBox(msg); } else AfxMessageBox("file opened");
But if the file doesn't exist, it just makes one!!! Any input is appreciated. Thanks! Nitron _________________________________________-- message sent on 100% recycled electrons.Check the code below if you don't mind using Win32 API:
bool FileExists(LPCTSTR szFilename)
{
WIN32_FIND_DATA findFileData;
HANDLE hFind = FindFirstFile(szFilename, &findFileData);
if (hFind == INVALID_HANDLE_VALUE)
{
return false;
}
VERIFY(FindClose(hFind));
return true;
}Tomasz Sowinski -- http://www.shooltz.com
What is "scratch" and why can everything be made from it?
-
How do I check if a file exists or not? I have tried the following:
CString f, msg; f = __argv[i]; ifstream infile(f); if(!infile) { msg.Format("Could not open: %s",f); AfxMessageBox(msg); } else AfxMessageBox("file opened");
But if the file doesn't exist, it just makes one!!! Any input is appreciated. Thanks! Nitron _________________________________________-- message sent on 100% recycled electrons.I usually use next approach
bool is_file_exist(const char * szFilePath) { DWORD Code=::GetFileAttributes(szFilePath); return (long(Code)!=-1) && (!((FILE_ATTRIBUTE_DIRECTORY) & Code)); }
-
Check the code below if you don't mind using Win32 API:
bool FileExists(LPCTSTR szFilename)
{
WIN32_FIND_DATA findFileData;
HANDLE hFind = FindFirstFile(szFilename, &findFileData);
if (hFind == INVALID_HANDLE_VALUE)
{
return false;
}
VERIFY(FindClose(hFind));
return true;
}Tomasz Sowinski -- http://www.shooltz.com
What is "scratch" and why can everything be made from it?
-
How do I check if a file exists or not? I have tried the following:
CString f, msg; f = __argv[i]; ifstream infile(f); if(!infile) { msg.Format("Could not open: %s",f); AfxMessageBox(msg); } else AfxMessageBox("file opened");
But if the file doesn't exist, it just makes one!!! Any input is appreciated. Thanks! Nitron _________________________________________-- message sent on 100% recycled electrons.As you're using MFC, I use this: CFileStatus status; if( CFile::GetStatus( "test.dat", status ) ) { // file exists } Christian We're just observing the seasonal migration from VB to VC. Most of these birds will be killed by predators or will die of hunger. Only the best will survive - Tomasz Sowinski 29-07-2002 ( on the number of newbie posters in the VC forum ) Cats, and most other animals apart from mad cows can write fully functional vb code. - Simon Walton - 6-Aug-2002
-
How do I check if a file exists or not? I have tried the following:
CString f, msg; f = __argv[i]; ifstream infile(f); if(!infile) { msg.Format("Could not open: %s",f); AfxMessageBox(msg); } else AfxMessageBox("file opened");
But if the file doesn't exist, it just makes one!!! Any input is appreciated. Thanks! Nitron _________________________________________-- message sent on 100% recycled electrons. -
How do I check if a file exists or not? I have tried the following:
CString f, msg; f = __argv[i]; ifstream infile(f); if(!infile) { msg.Format("Could not open: %s",f); AfxMessageBox(msg); } else AfxMessageBox("file opened");
But if the file doesn't exist, it just makes one!!! Any input is appreciated. Thanks! Nitron _________________________________________-- message sent on 100% recycled electrons.here's yet another way:
#include >io.h>
...
...
bool bFileExists = (_access(inFile, 0)==0);
Conservative: One who admires radicals centuries after they're dead. -- Leo C. Rosten