Thread: file existence check and ask if aleady exist to make another file name

  1. #1
    Registered User
    Join Date
    Feb 2013

    file existence check and ask if aleady exist to make another file name

    i have this code to check if file exist already and its work,
    but what i need is : is to add feature to function to see if the file already exist open same file and rename it with another name ,probably file2

    assume file name is "abc.c"
    this scenario like this

    User>>(provide file name)

    Code>>(check if file already exist show msg:

    "File already (abc.c) exist would you like to create another file and rename it ("abc2.c") "maybe it need loop to search directory "????

    User>>if press yes ok it will make another file with another name
    but if no exit the function and dont create another file

    // function to check file existence and here what i need to add the feature above
    int fExist(const char* fn){
        struct stat buffer;
        int exist = stat(fn,&buffer);
        if(exist == 0)
            return 1;
        else // -1
            return 0;

    and here is in the main():

    int exist = fExist(filename);
                                printf("File already (%s) exist.\n",filename);
                                fd=open(filename,O_WRONLY |O_CREAT,mode);                    
                                printf("FIle is being downloaded ...\n");
                                /* write the data from file to buffer    */                        
                                printf("Finish downloading....\n");

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    New Zealand
    Popping up a message sounds like it just adds more potential for this to screw up.
    While the message box is open the user could manually create a file with the new suggested name, or delete or rename the original file. I recommend that you don't show a message until after you've done it, informing the user of what you have done.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Registered User
    Join Date
    Nov 2012
    If you can use open(3) you might want to consider using O_CREAT and O_EXCL together, which will cause the call to open to fail if it already exists, making the "check for existence" part and the "create the file" part an atomic operation. See Man Page for open (freebsd Section 2) - The UNIX and Linux Forums for details

    The reason this is important on multi-user systems is because the following incorrect behavior is possible:

    1. Your program uses stat to see if `foo' exists and finds that it doesn't
    2. Some other program creates `foo', unknown to your program
    3. Your program overwrites `foo' because it still thinks from step 1 that it doesn't exist

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 05-13-2012, 10:00 AM
  2. Replies: 2
    Last Post: 04-13-2012, 07:39 PM
  3. Replies: 7
    Last Post: 06-16-2008, 01:18 PM
  4. File Existence Check :(
    By Moni in forum A Brief History of
    Replies: 1
    Last Post: 09-07-2003, 05:25 PM

Tags for this Thread