Thread: saving to a file

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    133

    saving to a file

    Hey guys is there a way i could inform the user if a txt file already exists when they enter the file name e.g. c:\\hi.txt so it won't get over written in my program the saved results are handeled by the following:

    Code:
    	cin.getline(file, 100); 
    
    	system("cls"); 
    
    
    	ofstream f;
    	f.open(file);

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Open the file for reading. If that fails, the file doesn't exist.

    This doesn't work all the time, but it's better than just clobbering the file.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Code:
    if ( file.is_open() ) {
            std::cout<<"File already exists"<<endl;
    }
    Is that the sort of thing you are meaning?

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    133
    I was able to solve the problem using the ios:noreplace and it returns fail if there is a problem, is this a good way to do it?

    Thanks.

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    No, ios::noreplace isn't standard. You can use this, instead:

    Code:
    fstream myFile(filename, ios_base::in); // Open for reading
    if (!myFile) {   // If it doesn't exist, then good
       myFile.open(filename, ios_base::out); // Open for writing
    }
    else {
       cout << "File already exists. Overwrite?";
       // ... and so on
    }
    Last edited by SlyMaelstrom; 03-25-2006 at 01:00 PM.
    Sent from my iPadŽ

  6. #6
    Registered User
    Join Date
    Oct 2005
    Posts
    133
    Hi, thanks for the reply but what do you mean by not standard? Thanks again

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    C++ experts got together and made a document called the C++ standard that defines the language and its rules so that people could write code that would work on different compilers and different operating systems. Something that is non-standard might work on one compiler or one platform, but not on another.

    In this case, ios::no_replace won't work on many modern compilers. For example, it might work with <fstream.h> in VC++ version 6, but when you move to version 7 or 8 and try to compile it, it won't compile (those require the standard header <fstream>). Also, some people trying to help on this forum and others won't be able to compile the code if you use non-standard or outdated code.

    If it works for you and you have no need for it to work in the future or for anyone else, then it is not that bad to use the non-standard <fstream.h> code, but since there is a perfectly valid alternative that won't come with all the other hassles it is probably a better idea to use it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems passing a file pointer to functions
    By smitchell in forum C Programming
    Replies: 4
    Last Post: 09-30-2008, 02:29 PM
  2. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM