Newbie question: How to "save as" an existing text file

This is a discussion on Newbie question: How to "save as" an existing text file within the C++ Programming forums, part of the General Programming Boards category; Can someone please help me on this? I need to make a copy of an existing .txt file and save ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    6

    Question Newbie question: How to "save as" an existing text file

    Can someone please help me on this?

    I need to make a copy of an existing .txt file and save it under a different name. The original file needs to be preserved so a simple "rename" can't do the trick. I've been searching on the web and the only way I found was to read the existing file into some sort of a data structure, e.g. vector<string>, then dump the whole thing into a new .txt file.

    I'm a bit concerned that the files are pretty big so is there a better way of doing this, please? Many thanks.

  2. #2
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,236
    Yes, a copy command will do the trick.
    Open cmd and type "copy spath\sorcefile.txt dpath\destfile.txt"

    [edit]
    Windows Assumed
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    6

    Did you mean this?

    Hi siavoshkc,

    Thanks very much for your reply. The code you suggested is indeed a dos command. So did you mean I put this line in a batch file the execute it in my C++ program?

    p.s. sorry that I didn't say clearly in my original post that I was looking for solutions in C++

    Thanks again

  4. #4
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,236
    If its a programming practice. You create a memory buffer. Load data into it. Save it somewhere else or with a different name. If the file does not fit in memory, do the same thing in a loop to copy whole of it.

    If it is not a programming practice tell me what it is.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    6

    Thumbs up solution

    Sorry, me again. I think I found the solution:

    system("copy c:\\org.txt c:\\dest.txt");

    Thanks again for your help.

  6. #6
    Registered User
    Join Date
    Mar 2010
    Posts
    6
    Quote Originally Posted by siavoshkc View Post
    If its a programming practice. You create a memory buffer. Load data into it. Save it somewhere else or with a different name. If the file does not fit in memory, do the same thing in a loop to copy whole of it.

    If it is not a programming practice tell me what it is.
    Yup, it's a programming practice, in c++

  7. #7
    Registered User
    Join Date
    Mar 2010
    Posts
    6

    the link

    and here's the original page where i found the solution
    C++ copy / delete file

  8. #8
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,236
    Quote Originally Posted by suny
    Sorry, me again. I think I found the solution:

    system("copy c:\\org.txt c:\\dest.txt");

    Thanks again for your help.
    If it satisfies you, right. But you are not programming, you are just executing a written program. You should write its code yourself if you really want to learn programming.

    BTW it is generally a bad practice to use system().
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,671
    Quote Originally Posted by siavoshkc View Post
    BTW it is generally a bad practice to use system().
    At best, it is non-portable. At worst, it is a security hole.

    Under windows (which is the system where examples in preceding posts will typically work) the win32 SDK has a function named CopyFile() that will have the required effect. Catch is, that is also non-portable (only works on Microsoft operating systems).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

  10. #10
    Registered User
    Join Date
    Mar 2010
    Posts
    6
    Right. Thank you both.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 04:53 AM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 01:36 PM
  3. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  4. Question about file I/O from a newbie
    By henrik in forum C Programming
    Replies: 4
    Last Post: 11-13-2007, 12:48 AM
  5. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 02:19 PM

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