ofstream problem

Discussion in 'C++' started by slyphiad, Sep 14, 2004.

  1. slyphiad

    slyphiad Guest

    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...
    slyphiad, Sep 14, 2004
    #1
    1. Advertising

  2. 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","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 ();
    }

    --
    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.
    Owen Jacobson, Sep 14, 2004
    #2
    1. Advertising

  3. slyphiad

    Mike Wahler Guest

    "slyphiad" <> wrote in message
    news:...
    > 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
    Mike Wahler, Sep 14, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tom Johnson
    Replies:
    4
    Views:
    381
    red floyd
    Aug 15, 2003
  2. Armando
    Replies:
    2
    Views:
    7,037
    Martijn Lievaart
    Jan 23, 2004
  3. Alfons
    Replies:
    1
    Views:
    426
  4. Gurikar

    ofstream problem

    Gurikar, May 6, 2005, in forum: C++
    Replies:
    3
    Views:
    462
    Kristo
    May 6, 2005
  5. Squid Seven

    ofstream * vs. ofstream

    Squid Seven, Jul 13, 2005, in forum: C++
    Replies:
    5
    Views:
    589
    Ivan Johansen
    Jul 14, 2005
Loading...

Share This Page