Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > ofstream problem

Reply
Thread Tools

ofstream problem

 
 
slyphiad
Guest
Posts: n/a
 
      09-14-2004
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","que st.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...
 
Reply With Quote
 
 
 
 
Owen Jacobson
Guest
Posts: n/a
 
      09-14-2004
On Mon, 13 Sep 2004 20:15:57 -0700, slyphiad wrote:

> 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","que st.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:fstream out (filename[i]);
/* whatever output goes into each file */
out.close ();
}

--
Some say the Wired doesn't have political borders like the real world,
but there are far too many nonsense-spouting anarchists or idiots who
think that pranks are a revolution.

 
Reply With Quote
 
 
 
 
Mike Wahler
Guest
Posts: n/a
 
      09-14-2004

"slyphiad" <> wrote in message
news: om...
> 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","que st.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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ofstream * vs. ofstream Squid Seven C++ 5 07-14-2005 07:34 AM
ofstream problem Gurikar C++ 3 05-06-2005 08:29 PM
ofstream problem opening file Alfons C++ 1 02-06-2005 08:00 PM
Problem which ifstream and ofstream class in VC++ Armando C++ 2 01-23-2004 03:50 PM
seemingly simple ofstream problem please help Tom Johnson C++ 4 08-15-2003 10:49 PM



Advertisments
 



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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57