How do u create multiple files?

so i figured out how to create 1 single file (and write in it) but now im having trouble figuring out how to make multiple files at the same time.
this is what im currently doing to create 1 file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <fstream>
#include <iostream>

using namespace std;

int main()
{
    fstream file;
    file.open("test.txt",ios::out);
    if(file.is_open())
    {
    file<<"Program created by: bigbass1997";
    cout <<"message has been wrote to the file" << endl;
    }
    else
    {
        cout <<"error reading file";
        }
        file.close();
        system("PAUSE");
        return 0;
}
Define "at the same time". Is it A. literally at the same time, like writing to all files at once, meaning multithreading?, or B. literally at the same time writing all files at once, but like a pastry cook that has 10 cakes in front and he goes one cake at a time adding the first icing, then moving to the next icing but only after the 10 cakes got the first icing, or C. writing multiple files sequentailly one after the other?
C. i think.
I want to write different things on each file
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());
}
Last edited on
Topic archived. No new replies allowed.