ofstream problem

S

slyphiad

i'm kinda new at c++ so be patient ^_^

i was just wondering if u guys could help me to solve this problem
that i had.

i'm trying to create 5 sequential files using ofstream.

this is what i did below:
char filename[][13] =
{"quest.01.cpp","quest.02.cpp","quest.03.cpp","quest.04.cpp","quest.05.cpp"};

ofstream out(&filename);

i've tried other stuff too but it doesnt seem too work.
the only time, i can create file is when i do:

ofstream out("quest.01.cpp");

but this is so inefficient. i dont want to create 5 ofstream for to
create 5 sequential files. is there anyway that i could solve this
problem?

Thanks...
 
O

Owen Jacobson

i'm kinda new at c++ so be patient ^_^

i was just wondering if u guys could help me to solve this problem
that i had.

i'm trying to create 5 sequential files using ofstream.

this is what i did below:
char filename[][13] =
^^ 1994 called. They want their code back.
{"quest.01.cpp","quest.02.cpp","quest.03.cpp","quest.04.cpp","quest.05.cpp"};

ofstream out(&filename);

i've tried other stuff too but it doesnt seem too work.
the only time, i can create file is when i do:

ofstream out("quest.01.cpp");

but this is so inefficient. i dont want to create 5 ofstream for to
create 5 sequential files. is there anyway that i could solve this
problem?

This is, in fact, exactly what you're going to have to do. The easiest
(and clearest) way would be to do it in a loop.

// Define appropriate constant somewhere, i.e.,
// const unsigned NUM_FILES = 5;
for (unsigned i = 0; i < NUM_FILES; ++i) {
std::eek:fstream out (filename);
/* whatever output goes into each file */
out.close ();
}
 
M

Mike Wahler

slyphiad said:
i'm kinda new at c++ so be patient ^_^

i was just wondering if u guys could help me to solve this problem
that i had.

i'm trying to create 5 sequential files using ofstream.

this is what i did below:
char filename[][13] =
{"quest.01.cpp","quest.02.cpp","quest.03.cpp","quest.04.cpp","quest.05.cpp"}
;

ofstream out(&filename);

ofstream out(filename[0]);
ofstream out(filename[1]);
/* etc */

or

ofstream out(&filename[0][0]);
ofstream out(&filename[1][0]);
/* etc */
i've tried other stuff too but it doesnt seem too work.
the only time, i can create file is when i do:

ofstream out("quest.01.cpp");

but this is so inefficient. i dont want to create 5 ofstream for to
create 5 sequential files. is there anyway that i could solve this
problem?

#include <algorithm>
#include <fstream>
#include <iostream>

using namespace std;

void openfile(const char *name)
{
ofstream out(name);
out ? cout << "File " << "'" << name << "' opened\n"
: cout << "Cannot open file " << "'" << name << "'\n";
}

int main()
{
char filename[][13] =
{
"quest.01.cpp",
"quest.02.cpp",
"quest.03.cpp",
"quest.04.cpp",
"quest.05.cpp"
};

for_each(filename,
filename + sizeof filename / sizeof *filename,
openfile);

return 0;
}

-Mike


-Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top