Re: How do you use FindFirstFile ?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



William DePalo [MVP VC++] wrote:

Take this quick hack:

#include <windows.h>
#include <iostream>

DWORD DoesFileExist(LPCSTR pszName)
{
 char  szMsg[256];
 DWORD dwStatus;

 if ( GetFileAttributes(pszName) != INVALID_FILE_ATTRIBUTES )
  dwStatus = ERROR_SUCCESS;

 else
  dwStatus = GetLastError();

 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
              0, dwStatus, 0, szMsg, sizeof(szMsg), 0);


std::cout << pszName << " : " << szMsg;

 return dwStatus;
}

int main()
{
 DoesFileExist("<>");
 DoesFileExist("a.b");
 DoesFileExist("c:\\windows\\notepad.exe");

 return 0;
}

It prints

<> : The filename, directory name, or volume label syntax is incorrect.
a.b : The system cannot find the file specified.
c:\windows\notepad.exe : The operation completed successfully.

on this XP/Pro/SP2 box.


If I do so then I have to print a message when the file does not exist.
For my app it is correct and valid for the file not to exist. I have nothing meaningful to print in this case. I only have to set a flag to ask some library to create the file later if I will need to.


"Timothy Madden"
Romania
.



Relevant Pages

  • reading a few bytes into a file
    ... Hello, I have this small C app that someone wrote for me ages ago, and am tired of it breaking every time I move it around to various systems, I will include it below. ... I am hoping I can do this in perl, as a one liner, were it will read a the first 4 bytes of a file, grab 4 bytes out of the file, which is a big endian value, and return that as an int to me. ... int main (int argc, char *argv) { ...
    (perl.beginners)
  • Re: [MFC REQUEST] Filename completion in sh(1)
    ... * This code is derived from software contributed to The NetBSD Foundation ... +static const char * ... +static int ... void ckfree; ...
    (freebsd-current)
  • Re: [MFC REQUEST] Filename completion in sh(1)
    ... pointer ckrealloc; ... void stunalloc; ... int unaliascmd(int, char **); ... -extern struct output output; ...
    (freebsd-current)
  • Re: [MFC REQUEST] Filename completion in sh(1)
    ... pointer ckrealloc; ... void stunalloc; ... int unaliascmd(int, char **); ... -extern struct output output; ...
    (freebsd-current)
  • Re: [MFC REQUEST] Filename completion in sh(1)
    ... pointer ckrealloc; ... void stunalloc; ... int unaliascmd(int, char **); ... -extern struct output output; ...
    (freebsd-current)