Thread: Removing *Unchanged* Output File (ofstream) :: C++

  1. #1
    Registered User
    Join Date
    Nov 2001

    Removing *Unchanged* Output File (ofstream) :: C++


    I am working on an interesting and simple problem. A program I am working on initializes an ofstream object. It does calculations (arbitrary) and writes to the output file. The problem is sometimes it writes new data and sometimes it does not. I am currently using a bool to make sure if it does not write new data, then it should remove the output file when it finishes. There is one false assumption. I am assuming the output file is always empty. That is not true because the output file is openned with "ios::app," which is appending mode. The program sometimes remove files that are not empty.

    I tried using the empty() function, but it does not work for an ofstream object. I would like to know the best way to check to make sure an ofstream object is *empty* return from the a end of class or before the program closes.


  2. #2
    Registered User
    Join Date
    Dec 2001
    Hmm try this code to check if an ofstream object is empty

    ofstream output;
    // do stuff here

    filebuf * FileBuffer = output.rdbuf();
    if ( FileBuffer->sgetc() == EOF )
    //the first char is EOF so the file is empty
    //file is not empty
    FileBuffer = NULL;
    if you delete the file make sure do set filebuffer to null

    I dont have a compiler to test that out right now, but i think it should work

  3. #3
    Registered User
    Join Date
    Nov 2001
    Okay. Thanks.

    I implemented the seekp() and tellp() method. It works flawlessly.


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  3. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  4. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM
  5. Simple File Creation Algorithm
    By muffin in forum C Programming
    Replies: 13
    Last Post: 08-24-2001, 03:28 PM