Solved

Duplicate the contents of a file

Posted on 2004-10-16
6
246 Views
Last Modified: 2010-04-01

I'm opting to get acclimcated with the remove and replace functions with regards to file systems.   Lets supposed I have a file excercise15.txt.  I'll copy exercise15.txt to testfile.txt.  In theory I'd like to duplicate exercise15.txt 10 times.   So now I'd have
testfile1.txt .... testfile10.txt.   How do I achieve that?

Here's my thougths thus far but I'm unsure how to integrate the "RenameFile function" with the rest of the program.   Program currently dumps contents of exercise15.txt to a file testfile.txt.


void RenameFile( const std::string& OldName, const std::string& NewName )
 {
  if( !std::rename( OldName.c_str(), NewName.c_str() ) == 0 )
      throw std::runtime_error( "Could not rename " + OldName + "." );
 }

int main(int argc, char* argv[])
{
  ifstream inFile ("exercise15.txt");
  fstream outFile ("testfile.txt");

   outFile << inFile.rdbuf();

   // Print the contents of the input file.
  //std::copy( istream_iterator<string>(inFile),
   //          istream_iterator<string>(),
   //          ostream_iterator<string>( cout, "\n" ) );

   // "Print" the contents of the output (ToFile) file
  outFile.seekg(0, std::ios_base::beg);
  //std::cout << outFile.rdbuf(); //or use std::copy

  inFile.close();
  outFile.close();
}
0
Comment
Question by:forums_mp
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 12328573
You can just use system() function call, to copy the file.

system("copy c:\\exercise15.txt c:\\exercise1.txt");
0
 
LVL 19

Accepted Solution

by:
mrwad99 earned 30 total points
ID: 12328729
Yeah; to get files numbered 1...x you could use

char cmd[100]; // Say

for (int i = 1; i <= x; i++) {  // x is max no. of copies to create
      sprintf(cmd, "copy inputFile.txt OutputFile_%i.txt", i);
      system(cmd);
}
0
 
LVL 19

Expert Comment

by:mrwad99
ID: 12328733
...or you could just use rename, plugging an appropriately formatted output filename into it as above with sprintf()...
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:forums_mp
ID: 12329071

Is 'system' platform dependent?

The ++ equivalent to sprintf would be 'stringstream'?
0
 
LVL 19

Expert Comment

by:mrwad99
ID: 12329090
>> Is 'system' platform dependent?

I don't think so; maybe Axter could clarify that...

>> The ++ equivalent to sprintf would be 'stringstream'?

You could go into all that, but why use something else when this works fine ?
0
 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 20 total points
ID: 12329381
There are many "C++ portable" ways to copy a file, have a look to:
http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_20615999.html

Just have to open a input stream, an output stream and copy all contents.
0

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
FMX enumerated colours 2 156
show out valin of json for debugging in visaul c++ 1 54
No module found pypyodbc, 3 58
How do i run a c++ file? 15 58
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question