Thread: file already exists

  1. #1
    I'm Back
    Join Date
    Dec 2001
    Posts
    556

    Exclamation file already exists

    can anybody tell me how to check if a file exists or not, while creating a new file, and if it does it displays "ALREADY EXISTS".

    f1.open(fname,ios::noreplace);
    if(!f1)
    cout<<"CREATED";
    else
    cout<<"ALREADY EXISTS";

    this is not correct, i think.

    also hows' creatnew() in io.h diff from the above.
    -

  2. #2
    Unregistered
    Guest
    noreplace causes open() to fail if file already exists.

    !fin means ifstream isn't open.

    therefore I think it should be

    f1.open(fname,ios::noreplace);
    if(!f1)//fin failed to open
    cout<<"ALREADY EXISTS";
    else
    cout<<"CREATED";

  3. #3
    I'm Back
    Join Date
    Dec 2001
    Posts
    556
    thanks, never thought of that it should be vice-versa.

    i'll try that.
    -

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  3. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  4. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM