Hi,

I am in the early stages of learning C (previously coded in java, PHP etc.).

The code below is a simple menu which enables you to:
create, delete, open and close a file.

The problem i have is if you choose to create the file, then open it, it is then not possible to delete it (gives error 32).
If you delete without opening it works fine or if you restart the program and then delete the file that was created in the last run it is also ok. Just seems you cant create, open and delete in same operation.

Any input would be helpful.

Code:
// for Win Xp Pro
#define _WIN32_WINNT 0x0501
#include <windows.h>
#include <stdio.h>

int main()
{
  HANDLE hFile;
  char fname[30] = "C:\\testfile.txt";
  int menuOption;

do {
      printf("\n");
      printf("0) Exit\n");
      printf("1) Close File\n");
      printf("2) Delete File\n");
      printf("3) Create File\n");
      printf("4) Open File\n");
      scanf("%d", &menuOption);

    switch(menuOption)
    {
        case 0 :
            break;
        case 1 :
            //close option
            if(CloseHandle(hFile) != 0)
              printf("CloseHandle() succeeded!\n");
            else printf("CloseHandle() failed!\n");
            break;
        case 2 :
            //delete option
            if(CloseHandle(hFile) != 0)
              printf("CloseHandle() succeeded!\n");
            else printf("%s could not be closed! error %u\n", fname, GetLastError());

            if(DeleteFile(fname) != 0)
              printf("%s file successfully deleted!\n", fname);
            else printf("%s could not be deleted! error %u\n", fname, GetLastError());
            break;
        case 3 :
            //create option
            hFile = CreateFile(fname,               // file to be opened
                                GENERIC_WRITE,      // open for writing
                                FILE_SHARE_WRITE,   // share for writing
                                NULL,               // default security
                                CREATE_NEW,         // create new file only
                                FILE_ATTRIBUTE_NORMAL |FILE_ATTRIBUTE_ARCHIVE | SECURITY_IMPERSONATION,
                                                    // normal file archive and impersonate client
                                NULL);              // no attribute template

            if(hFile == INVALID_HANDLE_VALUE)
              printf("Could not create %s file, error %u.\n", fname, GetLastError());
            else printf("%s file created successfully, error if any, %u.\n", fname, GetLastError());
            break;
        case 4 :
            //open option
            hFile = CreateFile(fname,            // file to create
                      GENERIC_WRITE,             // open for writing
                      FILE_SHARE_WRITE,          // allow write share
                      NULL,                      // default security
                      OPEN_EXISTING,             // opens a file
                      FILE_ATTRIBUTE_NORMAL |FILE_ATTRIBUTE_ARCHIVE | SECURITY_IMPERSONATION,
                      NULL);                     // no attribute template

            if(hFile == INVALID_HANDLE_VALUE)
              printf("Could not open %s file, error %u.\n", fname, GetLastError());
            else printf("%s file opened successfully, error if any, %u.\n", fname, GetLastError());
            break;
        default :
            //error message
            printf("Sorry but %d is not a valid option!", menuOption);
            break;
    }
 } while (menuOption !=0);

  return 0;
}
Thanks,
Gid