Thread: tempnam() - when does it get deleted?

  1. #1
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332

    tempnam() - when does it get deleted?

    I have a program that optionally allows the user to overwrite the input file (binary) with the output file (text).

    When I detect the output file is the same name as the input file, I create a temporary file, via tempnam(NULL), and write the output to it.

    When finished writing, I close both the input file and output file, and then look to see if the original input file needs to be overlaid with the output file. If it does, I open the output file in ios::in mode and the input file in ios:ut mode and simply copy it over via in.read and out.write.

    However, every time I end up with zero bytes read, and thus zero written. When I do not go through the overlay process, the output file is fine and has the data it is supposed to. When I do go through the overlay process, my counters from the primary process tell me the same amount of data was written. I have verified the output file does contains data from the primary operation.

    Does anything in this process sound suspect?

    Thanks, Todd
    Last edited by Dino; 01-16-2008 at 08:37 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    AFAIK, it only creates the temporary name, not the actual file.
    So removing the file is still down to you.

    http://www.cplusplus.com/reference/c...io/tmpnam.html
    See also the difference with tmpfile()
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    I think I might know what the problem is. I've defined the output file as an ostream. Methinks it should be a fstream. I'll try that and report back.

    Todd

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. HELP - How can I recover deleted file?
    By brett in forum Linux Programming
    Replies: 5
    Last Post: 09-30-2003, 10:10 AM
  2. Someone deleted my post
    By DarkViper in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 11-27-2002, 06:05 PM
  3. Deleted instance
    By Boksha in forum C++ Programming
    Replies: 3
    Last Post: 05-28-2002, 11:15 AM
  4. What actually happens when a file is deleted ?
    By pritesh in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 02-06-2002, 02:05 PM