file already exists

This is a discussion on file already exists within the C++ Programming forums, part of the General Programming Boards category; can anybody tell me how to check if a file exists or not, while creating a new file, and if ...

  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, 11:39 PM
  2. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 02: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, 08:54 AM
  4. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 05:54 PM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 11:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21