Inputfile outputfile same name

This is a discussion on Inputfile outputfile same name within the C++ Programming forums, part of the General Programming Boards category; How would you go to give the same name to the outputfile as the inputfile? I know that Windows will ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Location
    France
    Posts
    749

    Inputfile outputfile same name

    How would you go to give the same name to the outputfile as the inputfile?
    I know that Windows will add (2) to the name but that wouldnt matter.

    Code:
     
    int main (int argc, char* argv[]){
         
          ifstream TheFile ("1.txt");
          ofstream TheCopy;
          TheCopy.open (TheFile,ios::app);
          TheCopy << "hello";
          
          TheFile.close();
          TheCopy.close();
    }
    Last edited by Ducky; 01-26-2008 at 01:16 AM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    230
    if you mean in the same directory, I don't think that's possible. You can't have 2 files with the same name in same directory regardless of what system or language you're using.

    But if it's just the same file name in another directory then I'm guessing what you did would work fine with a little adjustment to the file location (I'm not sure. I'm still learning C++ but I've been doing C file io for a while).

    Why do you want to do this anyway?

    Note: you can make the extension different and, with the correct settings on Windows, the files will *look* the same since you can hide the extension.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Location
    France
    Posts
    749
    I want the user to enter a file name,
    i want the program to do something with the file
    and put out a copy with the same name.
    Windows renames the second file to file(2).txt if you already got the same name in the folder but i think only when you're downloading
    the same file not when you're copying yourself from folder to folder.
    But maybe it renames it also when its creating a new file, i dont know.
    If its not the case i would like to append (2) to it.
    Something like TheCopy.open (TheFile + "(2)" ,ios::app);
    Last edited by Ducky; 01-26-2008 at 03:57 AM.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    230
    well what you can do is rename the old file, open it, process it's data, then write to the old name. Is that what you wanted?

  5. #5
    Registered User
    Join Date
    Dec 2007
    Location
    France
    Posts
    749
    "then write to the old name."

    Im sorry i dont understand you.
    Actually i wanna keep the old file unchanged. Same name, same content.

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    It somewhat depends on the process you are using.

    If you read the whole input file, close it, and write the output file, then there are no considerations.

    But, if you want to write as you read, then you have a couple choices.

    You can use tempnam() or tempfile() and determine the final name needed at the end, or you can determine the final name needed at the beginning and just use it from the get-go.

    If you want to use a suffix of (1) or (2), then you'll need to figure out if the one you need already exists in the directory you will be writing to, and if it does, bump the sequence number between the parens.

    That's how I see it.

    I'm writing a program right now where the user wants his input file to be overwritten by his output file, but I can't destroy the input file until the output file is completely written (and I read/write, read/write, etc.) So, I write to a tempnam() file, and at the end, erase the input file and then rename the tempnam()'ed file to the original input file.

    Todd

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,420
    Quote Originally Posted by Ducky View Post
    I know that Windows will add (2) to the name but that wouldnt matter.
    No, it won't. Explorer does that in its code, not Windows.
    What you type is what you get.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Dec 2007
    Location
    France
    Posts
    749
    Thank you Todd i think im gonna go with the suffix of (1) or (2) option seeing that i want to
    keep the original.
    Thank you Elysia for the info.
    And thanks Abda92 for trying to help me.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ASCII code goes into minus
    By Degenko in forum C Programming
    Replies: 4
    Last Post: 05-26-2007, 12:35 PM
  2. reading through an array
    By ammochck21 in forum C++ Programming
    Replies: 11
    Last Post: 11-29-2006, 06:02 AM
  3. Problem with file i/o
    By Dragoncaster131 in forum C Programming
    Replies: 8
    Last Post: 04-06-2004, 06:34 PM
  4. problem with fread
    By GaPe in forum C Programming
    Replies: 3
    Last Post: 02-23-2003, 02:54 AM

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