Solved

Duplicate the contents of a file

Posted on 2004-10-16
6
213 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
why "." vs "->" 23 119
ADO Memory leak with DELPHI 2007 37 170
PDF library for Delphi 2 105
Exception thrown at 0x00007FFD5BC81F28 7 38
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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

910 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

23 Experts available now in Live!

Get 1:1 Help Now