Thread: Search I/O file data

  1. #1
    Beginner
    Join Date
    Oct 2003
    Posts
    8

    Search I/O file data

    Hi all,

    I have created a section in my program such that it will prompt user to enter the filename. From this, it will create the file with the filename the user have entered.

    However, I would like to know how can I check if the file already exists? If it already exists, do not create the file and simply open the existing file and add more data from the last line.

    Here's part of the code
    Code:
    ofstream outfile;
    ifstream infile;
    
    char pfile[20];
    
    cout << "Enter filename: ";
    cin >> pfile;
    cin.ignore(1);
    
    /* open an existing configuration file to check if the filename exists 
    (I'm not sure how to check... this simply open the file) */
    infile.open("config.cfg", ios::in, 0);
    
    // create file base on what user specify
    ofstream f(pfile);
    
    // open configuration file and store filename in it
    outfile.open("config.cfg", ios::out, 0);
    outfile << pfile;
    outfile.close();
    Thanks :)

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Simply open it with the right "ios" flag. There are flag types for appending, truncating, etc.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Beginner
    Join Date
    Oct 2003
    Posts
    8
    Hmm, i'm not really sure how to go about it. Mind showing it to me? I only know how to use ios:ut, ios::in etc.

  4. #4
    Registered User Dante Shamest's Avatar
    Join Date
    Apr 2003
    Posts
    970
    Code:
          /*
           * Checks if a file exists.
           *  file - name of the file
           * Returns: true if file exists, false otherwise.
           */
          bool doesFileExist( char * file )
          {
            ifstream input( file, ios::in ) ;
    
            if ( input.is_open() )  // if file can be opened, if exists
            {
              input.close() ;
              return true ;
            }
            else
            {
              input.close() ;
              return false ;
            }
          }

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    A few seconds on Google will give you some examples. Most everything you'll ever need can be found there; in the event no one is around here, or you feel like answering your own questions.

    There is no real need to ever check if it exists, if you're just going to append to it anyway.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Beginner
    Join Date
    Oct 2003
    Posts
    8
    Thanks Dante

    I get your idea quzah... but I guess i need to read more on how I/O file system works

    BTW, I've got a question. Let's say I have 2 base class, 2 derived class. Each base class constructor contains all the question that will prompt the user and is called when I do a

    Code:
    Instance[array] = new BaseClassName;
    in my main() switch statement. So, I would like to know, where should I actually place the file open/close? I have tried to place inside the constructor but everytime, it overwrites the information in it.

    Any idea?

    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  2. File I/O Question
    By Achy in forum C Programming
    Replies: 2
    Last Post: 11-18-2005, 12:09 AM
  3. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  4. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM