Copy File

I'm Trying to make a program that copys files and pastes them in the same folder. I got that far but i want the copyed file to be... hard to explin here is and example.
EXAMPLE:
Copyed File: C: example.exe
i want it to look like this:
C: example1.exe

i want to do that without entering it into the code.
try this;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
stringstream stream;
ofstream output;
string filename;
string x;
int i = 0;

int main()
{
    while(i < 10)
    {
	     i++;
	     stream << i;
	     stream >> x;
	     stream.str("");
	     stream.clear();
	     
	     filename = "C:\\example" + x + ".txt";
	     cout <<filename <<endl;
	     
         output.open(filename.c_str());
         output << ".";
         output.close();
    };

return 0;
}
How Does This Work?? I Don't really want to use something and not know how it works. if you could explain that would be very nice.

im not very good at explaining stuff but ill give it a go:
first it creates a stringstream called "stream" -used for converting string to int or int to string; you need to include <sstream>
then create output file stream called "output" -used for outputing to files
you need to include <fstream>;
then creates a string called "filename" to store what we want the file to be called and a string called 'x' so we can put the converted int into x; you need to include <string>;
and then crates an int called 'i' to use in the loop and to name the file;


then we start the main() function and streight away start a "while loop"
so while the int i is less than 10 it will continue to loop;

i++ //increases i by one
stream << i; // outputs i to the stringstream object
stream >> x; // inputs converted int i to x so x will contain whatever i was but as a string;

stream.str(""); // emptys the stringstream
stream.clear(); // clears bit flag (im not to sure what this part is for but i have been tought to do it like this);

filename ="C:\\example" + x + ".txt"; //makes filename contain
"C:\example(whatever x is).txt"
cout << filename <<endl; //outputs to to screen whatever filename is;

output.open(filename.c_str()); makes output
"C:\example(whatever x is).txt" and opens it for output or creates it if it is not already there;
output << "."; // outputs to file "." so that the file contains '.';
output.close(); // closes the filestream;

i hope i explained it alright;
Thank You Very much. You Explained it just right. I got the program to work. thanks very much.

Thanks,
MM
Topic archived. No new replies allowed.