Solved

Duplicate the contents of a file

Posted on 2004-10-16
6
201 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
6 Comments
 
LVL 30

Expert Comment

by:Axter
Comment Utility
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
Comment Utility
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
Comment Utility
...or you could just use rename, plugging an appropriately formatted output filename into it as above with sprintf()...
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:forums_mp
Comment Utility

Is 'system' platform dependent?

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

Expert Comment

by:mrwad99
Comment Utility
>> 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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

744 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now