Solved

Duplicate the contents of a file

Posted on 2004-10-16
6
236 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
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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

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…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

820 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