Check if file is there or not [modified]
-
I curently have this code bool FilesClass::FilePath(char name[50]) { int buffer; int fh; if( _sopen_s( &fh, name, _O_BINARY, _SH_DENYWR, 0 ) ) { Files++; buffer=strlen(name); strncat(DllStatus,name,buffer); strncat(DllStatus,";",2); return false; } else return true; } But in some cases it fails to process the checks, and asserts with cant open file. My idea is that this function either returns true if the file is present, or false if its not, any sugestions? -- modified at 17:45 Tuesday 5th December, 2006
-
I curently have this code bool FilesClass::FilePath(char name[50]) { int buffer; int fh; if( _sopen_s( &fh, name, _O_BINARY, _SH_DENYWR, 0 ) ) { Files++; buffer=strlen(name); strncat(DllStatus,name,buffer); strncat(DllStatus,";",2); return false; } else return true; } But in some cases it fails to process the checks, and asserts with cant open file. My idea is that this function either returns true if the file is present, or false if its not, any sugestions? -- modified at 17:45 Tuesday 5th December, 2006
What do you mean fails to process the checks? How about something like this:
bool FilesClass::FilePath(const char *name)
{
int buffer;
int fh;errno\_t errno = \_sopen\_s( &fh, name, \_O\_BINARY | \_O\_RDONLY, \_SH\_DENYWR, \_S\_IREAD ); if( errno ) { if (errno == ENOENT) { Files++; buffer=strlen(name); strncat(DllStatus,name,buffer); strncat(DllStatus,";",2); } else { // \_sopen\_s() failed! } return false; } else { \_close( fh ); return true; }
}
The pmode param is required in the call to _sopen_s(). Note that if the file is already opened elsewhere without read share access FilePath() will return false.
-
What do you mean fails to process the checks? How about something like this:
bool FilesClass::FilePath(const char *name)
{
int buffer;
int fh;errno\_t errno = \_sopen\_s( &fh, name, \_O\_BINARY | \_O\_RDONLY, \_SH\_DENYWR, \_S\_IREAD ); if( errno ) { if (errno == ENOENT) { Files++; buffer=strlen(name); strncat(DllStatus,name,buffer); strncat(DllStatus,";",2); } else { // \_sopen\_s() failed! } return false; } else { \_close( fh ); return true; }
}
The pmode param is required in the call to _sopen_s(). Note that if the file is already opened elsewhere without read share access FilePath() will return false.
Ok, found my assert problem as well, i actualy check for total of 6222 files, array was to short. Thanks, your code worked better. -- modified at 18:26 Tuesday 5th December, 2006
-
I curently have this code bool FilesClass::FilePath(char name[50]) { int buffer; int fh; if( _sopen_s( &fh, name, _O_BINARY, _SH_DENYWR, 0 ) ) { Files++; buffer=strlen(name); strncat(DllStatus,name,buffer); strncat(DllStatus,";",2); return false; } else return true; } But in some cases it fails to process the checks, and asserts with cant open file. My idea is that this function either returns true if the file is present, or false if its not, any sugestions? -- modified at 17:45 Tuesday 5th December, 2006
FredrickNorge wrote:
My idea is that this function either returns true if the file is present, or false if its not, any sugestions?
_access(..., 0)
sounds a whole lot simpler.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
-
I curently have this code bool FilesClass::FilePath(char name[50]) { int buffer; int fh; if( _sopen_s( &fh, name, _O_BINARY, _SH_DENYWR, 0 ) ) { Files++; buffer=strlen(name); strncat(DllStatus,name,buffer); strncat(DllStatus,";",2); return false; } else return true; } But in some cases it fails to process the checks, and asserts with cant open file. My idea is that this function either returns true if the file is present, or false if its not, any sugestions? -- modified at 17:45 Tuesday 5th December, 2006
In additinal yo can use of
FindFirstFile
WhiteSky
-
I curently have this code bool FilesClass::FilePath(char name[50]) { int buffer; int fh; if( _sopen_s( &fh, name, _O_BINARY, _SH_DENYWR, 0 ) ) { Files++; buffer=strlen(name); strncat(DllStatus,name,buffer); strncat(DllStatus,";",2); return false; } else return true; } But in some cases it fails to process the checks, and asserts with cant open file. My idea is that this function either returns true if the file is present, or false if its not, any sugestions? -- modified at 17:45 Tuesday 5th December, 2006
FredrickNorge wrote: My idea is that this function either returns true if the file is present, or false if its not, any sugestions? try PathFileExists
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow cheers, Alok Gupta VC Forum Q&A :- I/ IV Support CRY- Child Relief and you