Well, A and C both serve to create different files.
Now, let me ask you this: How different is the data that goes in each file when compared one another? Would you say that they have no relationship? If they don't, then I see no choice: Write the same code you have shown again for the second file and edit the contents to be written, and so on for every file.
But after doing this you'll note repetition: Everything is the same except the contents. So encapsulate the mechanics of opening the file, writing to the file and closing the file in a function that takes the data to be written as a paramenter, and another parameter to receive the filename.
If you do the function as described above, you can then just call the function to create the different functions. Let's imagine that you created the function and you named it MyCreateFile():
1 2 3
|
MyCreateFile("test01.txt", "This is the content of test01.txt.");
MyCreateFile("test02.txt", "Now this is the second file! This is getting repetitive. :-)");
MyCreateFile("test03.txt", "Ok, now I'm bored. I'll stop here.");
|
If the content is larger, maybe the above construct is a little cumbersome, so we can see if something can come up nicer if the situation calls for it.
If the content between files is not so different, you might be able to use a loop to write all files. Example assuming you created the MyCreateFile() function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
//This function converts a number to a string. It requires #include <sstream>
template<class T>
std::string NumberToString(T number)
{
std::ostringstream os;
os << number;
return os.str();
}
//This code just shows how to create multiple files with a loop.
typedef char *LPSTR;
LPSTR fileNames[] = { "test01.txt", "test02.txt", "test03.txt" };
std::string data;
for (int i = 0; i < 3; i++)
{
data = "This is content for file #";
data += NumberToString(i);
MyCreateFile(fileNames[i], data.c_str());
}
|