how to check if file exists ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • moroccanplaya
    New Member
    • Jan 2011
    • 80

    how to check if file exists ?

    i want to know how to check if the file already exists before creating the file?
    Code:
                    
    puts("please enter a file name to create:\n");
    scanf("%s", &file_name);
    create_pointer = fopen( file_name, "ab");
    if( create_pointer == NULL)	
    //create file//
    else
    {
    //file already exist// how would you check if the file already exists ?? 
    }
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    Before you attempt to open it for writing you could open it for reading - if it exists the open will be sucessful otherwise if it does not exist the open would fail.

    Comment

    • moroccanplaya
      New Member
      • Jan 2011
      • 80

      #3
      do you mean something like this ?
      Code:
      puts("please enter a file name to create:\n");
      scanf("%s", &file_name);
      
      if( create_pointer = fopen(file_name. "r"); 
      printf("file already exist"); 
      else
      {
      if(create_pointer != fopen(file_name"r"); 
      create_pointer = fopen( file_name, "ab");
      }

      Comment

      • horace1
        Recognized Expert Top Contributor
        • Nov 2006
        • 1510

        #4
        I was thinking
        Code:
        puts("please enter a file name to create:\n");
        scanf("%s", &file_name);
        
        if( create_pointer = fopen(file_name. "r") != NULL)
        printf("file already exist");
        else
        {
        create_pointer = fopen( file_name, "ab");
        
        // write to the file
        
        }
        what do you want to do if the file already exists?
        in the second fopen you can open it with "wb" as you know it does not exist

        Comment

        • Oralloy
          Recognized Expert Contributor
          • Jun 2010
          • 988

          #5
          Alternately, you can use stat(), and get everything you ever wanted about the file. Use http://linux.die.net/man/2/stat to get the necessary information.

          Cheers!

          Comment

          • moroccanplaya
            New Member
            • Jan 2011
            • 80

            #6
            thanks allot for your hello i appreciate it

            Comment

            • Oralloy
              Recognized Expert Contributor
              • Jun 2010
              • 988

              #7
              What are you trying to achieve?

              If you want to do what is commonly called "create or append", I think you might be able to use the fopen()options string "a+b".

              Here's a reference on fopen that explains the options string fairly clearly.

              Comment

              • moroccanplaya
                New Member
                • Jan 2011
                • 80

                #8
                thanks it worked using fopen() was fine did mot need to use stat() thanks for your help much appreciated

                Comment

                • Oralloy
                  Recognized Expert Contributor
                  • Jun 2010
                  • 988

                  #9
                  Glad you got it worked out.

                  Cheers!
                  Oralloy

                  Comment

                  Working...