Menu

fopen,fclose problem

2004-04-13
2012-09-26
  • Nobody/Anonymous

    i want to check if a file already exists, using fopen.
    This works, but after calling fopen on the file i cant delete the file until i close my program. I watched the handles that my program created, and everytime i use fopen to check if the file excists a new handle is created.
    I already tried CloseHandle, and played with FindFirstFile without succes. Anybody got an idea??

    testFile = fopen(fileName,"rb");
    if (testFile != NULL)
    {
        fclose (testFile);
        CloseHandle(testFile);
        testFile = NULL;
        return NULL;
    }

    im using devcpp 4.9.8.8 

     
    • Nobody/Anonymous

      Try the following code fragment, it will delete the file "junk.txt" found in the root of the C:\ drive.

      Otherwise it will report its error.

      #include <stdio.h>
      #include <stdlib.h>
      #include <string.h>

      int main (void)
      {
          FILE *fp = fopen("C:\\junk.txt", "rb");
         
          if (fp == NULL)
          {
              printf("File not found\n");
              return -1;
          }

          fclose(fp);

          if (remove("C:\\junk.txt") == -1)
          {
              perror(strerror(errno));
              return -1;
          }
         
          printf("successfully deleted file\n");
         
          return 0;
      }

      It uses fopen(), fclose(), and remove() to perform its magic.

      rr

       
    • Nobody/Anonymous

      thanks for your reply,  but this is no sollution to the problem. Ill describe a little more about my intensions......

      in the program i want to check if a file exsists to prevent overwriting...... and when the program tells that a file already exsists i want to be able to manualy delete it. So the idea is to prefent deleting the original not to delete it :)

      so in short the question is how to close a file opened with fopen and to close it in such a way that the program doesnt put some kind of lock on it.

      or if someone else know how to check if a file exists..... please share it with the rest of this forum :)

       
    • qWake

      qWake - 2004-04-13

      You said that you played with FindFirstFile without success.  You should play with it longer because this is the correct function to use for a Windows program, which seems to be what you are writing.  The function will return a handle to a search structure, NOT a handle to the file.  Be sure to close this handle anyways, but then you will still need to open a handle to your file in order to access it.  You probably should write a separate "bool FileExists" function to contains that logic.

      General platitude recommendation: if you decide to use Win32 file functions in your program, you would probably find it best to use them throughout: use CreateFile and CloseHandle instead of fopen and fclose for example.  The Win32 API gives you better control on files that is not available with standard C++ functions, so you can truncate or extend a file for example.  The drawback of this finer access is that it makes your code Windows-specific.

      If you decide to stick with generic code, your FileExists function might be something like this:

      #include <stdbool.h>
      #include <stdio.h>

      bool fileExists(const char* fileName) {
          FILE* file = fopen(fileName, "rb");
          if (file) {
              fclose(file);
              return true;
          }
          return false;
      }

      int main() {
          char fileName[99];
          printf("File name to check: ");
          gets(fileName);
          if (fileExists(fileName)) {
              FILE* file = fopen(fileName, "rb");
              if (file) {
                  printf("Exists and opened again.\n");
                  fclose(file);
              }
              else {
                  printf("Exists but re-open failed.\n");
              }
          }
          else {
              printf("Does not exists\n");
          }
          return 0;
      }

      qWake

       
    • Nobody/Anonymous

      thanks for your responce :)

      At first i thought i didnt use fopen/fclose correctly, but with the help of your post (which is what i tried at first)

      I did some serious debugging and found out that a sub sub sub procedure was changing the fileName which caused an other procudeure to  open the wrong file etc etc etc...... althoug your post didnt help directly.... it gave me some confidence about the way to use fopen.  This did indeed help me solve my problem.

      Thanks

      PS im new to programming as you see, but im learning everyday something new :)

       
    • Nobody/Anonymous

      mmmmm i believe i should check my post before pressing the submit button...... escpecialy when i rewrite my sentences 4 times :)

       
    • aditsu

      aditsu - 2004-04-14

      you wrote:

      testFile = fopen(fileName,"rb");
      if (testFile != NULL)
      {
      fclose (testFile);
      CloseHandle(testFile);
      testFile = NULL;
      return NULL;
      }

      testFile = fopen(fileName,"rb"); means that testFile is a FILE*
      CloseHandle expects a HANDLE; a FILE* is not a HANDLE
      anyway, it's good that you solved the problem in the end

      Adrian

       
    • Nobody/Anonymous

      the closehandle and  testFile = NULL were desperate mesures for a problem which i couldnt solve......
      when i cant solve the problem i fall back on somewhat random coding in the hope it somehow magicallly solves my problem. :)

      Good thing for me this forum exists

       

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.