Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 262
  • Last Modified:

Duplicate the contents of a file


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
forums_mp
Asked:
forums_mp
2 Solutions
 
AxterCommented:
You can just use system() function call, to copy the file.

system("copy c:\\exercise15.txt c:\\exercise1.txt");
0
 
mrwad99Commented:
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
 
mrwad99Commented:
...or you could just use rename, plugging an appropriately formatted output filename into it as above with sprintf()...
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
forums_mpAuthor Commented:

Is 'system' platform dependent?

The ++ equivalent to sprintf would be 'stringstream'?
0
 
mrwad99Commented:
>> 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
 
Jaime OlivaresCommented:
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now