simple outfile question

Discussion in 'C++' started by solartimba, Sep 21, 2004.

  1. solartimba

    solartimba Guest

    Hi,
    I am in another country, and I need to finish a program. But I do not
    have any reference books. Can you tell me how to print this to a file
    without ending the stream with an endl. Stated differently, I don't
    want an endl after the last data point printed to the file.

    I know this is a basic question, and I apologize.

    ******************************

    void outFile()
    {
    ofstream outfile(outPath.c_str());

    vector<cVocabulary>::iterator iterSer;

    for(iterSer=series.begin(); iterSer!=series.end(); ++iterSer)
    {
    outfile << iterSer->returnVocabulary() << endl;
    outfile << iterSer->returnVocType() << endl;
    outfile << iterSer->returnDefinition() << endl;
    outfile << iterSer->returnExample() << endl;
    }

    outfile.close();
    }
     
    solartimba, Sep 21, 2004
    #1
    1. Advertising

  2. "solartimba" <> wrote in message
    news:...
    > Hi,
    > I am in another country, and I need to finish a program. But I do not
    > have any reference books. Can you tell me how to print this to a file
    > without ending the stream with an endl. Stated differently, I don't
    > want an endl after the last data point printed to the file.
    >
    > I know this is a basic question, and I apologize.
    >
    > ******************************
    >
    > void outFile()
    > {
    > ofstream outfile(outPath.c_str());
    >
    > vector<cVocabulary>::iterator iterSer;
    >
    > for(iterSer=series.begin(); iterSer!=series.end ++iterSer)
    > {
    > outfile << iterSer->returnVocabulary() << endl;
    > outfile << iterSer->returnVocType() << endl;
    > outfile << iterSer->returnDefinition() << endl;
    > outfile << iterSer->returnExample() << endl;
    > }
    >
    > outfile.close();
    > }


    Simple enough, just change your loop

    for(iterSer=series.begin(); iterSer!=series.end ++iterSer)
    {
    if (iterSer != series.begin())
    outfile << endl;
    outfile << iterSer->returnVocabulary() << endl;
    outfile << iterSer->returnVocType() << endl;
    outfile << iterSer->returnDefinition() << endl;
    outfile << iterSer->returnExample();
    }

    john
     
    John Harrison, Sep 21, 2004
    #2
    1. Advertising

  3. solartimba

    David Hilsee Guest

    "John Harrison" <> wrote in message
    news:...
    >
    > "solartimba" <> wrote in message
    > news:...
    > > Hi,
    > > I am in another country, and I need to finish a program. But I do not
    > > have any reference books. Can you tell me how to print this to a file
    > > without ending the stream with an endl. Stated differently, I don't
    > > want an endl after the last data point printed to the file.
    > >
    > > I know this is a basic question, and I apologize.
    > >
    > > ******************************
    > >
    > > void outFile()
    > > {
    > > ofstream outfile(outPath.c_str());
    > >
    > > vector<cVocabulary>::iterator iterSer;
    > >
    > > for(iterSer=series.begin(); iterSer!=series.end ++iterSer)
    > > {
    > > outfile << iterSer->returnVocabulary() << endl;
    > > outfile << iterSer->returnVocType() << endl;
    > > outfile << iterSer->returnDefinition() << endl;
    > > outfile << iterSer->returnExample() << endl;
    > > }
    > >
    > > outfile.close();
    > > }

    >
    > Simple enough, just change your loop
    >
    > for(iterSer=series.begin(); iterSer!=series.end ++iterSer)
    > {
    > if (iterSer != series.begin())
    > outfile << endl;
    > outfile << iterSer->returnVocabulary() << endl;
    > outfile << iterSer->returnVocType() << endl;
    > outfile << iterSer->returnDefinition() << endl;
    > outfile << iterSer->returnExample();
    > }


    You meant the following, right?

    // Note: series.end, and the for loop in general, were changed
    for(iterSer=series.begin(); iterSer!=series.end(); ++iterSer)
    {
    outfile << iterSer->returnVocabulary() << endl;
    outfile << iterSer->returnVocType() << endl;
    outfile << iterSer->returnDefinition() << endl;
    outfile << iterSer->returnExample();

    // use endl unless this is the last element in the vector
    if ( iterSer != series.end() - 1 ) {
    outfile << endl;
    }
    }

    --
    David Hilsee
     
    David Hilsee, Sep 22, 2004
    #3
  4. "David Hilsee" <> wrote in message
    news:...
    > "John Harrison" <> wrote in message
    > news:...
    >>
    >> "solartimba" <> wrote in message
    >> news:...
    >> > Hi,
    >> > I am in another country, and I need to finish a program. But I do not
    >> > have any reference books. Can you tell me how to print this to a file
    >> > without ending the stream with an endl. Stated differently, I don't
    >> > want an endl after the last data point printed to the file.
    >> >
    >> > I know this is a basic question, and I apologize.
    >> >
    >> > ******************************
    >> >
    >> > void outFile()
    >> > {
    >> > ofstream outfile(outPath.c_str());
    >> >
    >> > vector<cVocabulary>::iterator iterSer;
    >> >
    >> > for(iterSer=series.begin(); it erSer!=series.end++iterSer
    >> > {
    >> > outfile << iterSer->returnVocabulary() << endl;
    >> > outfile << iterSer->returnVocType() << endl;
    >> > outfile << iterSer->returnDefinition() << endl;
    >> > outfile << iterSer->returnExample() << endl;
    >> > }
    >> >
    >> > outfile.close();
    >> > }

    >>
    >> Simple enough, just change your loop
    >>
    >> for(iterSer=series.begin(); it erSer!=series.end++iterSer
    >> {
    >> if (iterSer != series.begin())
    >> outfile << endl;
    >> outfile << iterSer->returnVocabulary() << endl;
    >> outfile << iterSer->returnVocType() << endl;
    >> outfile << iterSer->returnDefinition() << endl;
    >> outfile << iterSer->returnExample();
    >> }

    >
    > You meant the following, right?
    >
    > // Note: series.end, and the for loop in general, were changed
    > for(iterSer=series.begin(); iterSer!=series.end ++iterSer)
    > {
    > outfile << iterSer->returnVocabulary() << endl;
    > outfile << iterSer->returnVocType() << endl;
    > outfile << iterSer->returnDefinition() << endl;
    > outfile << iterSer->returnExample();
    >
    > // use endl unless this is the last element in the vector
    > if ( iterSer != series.end() - 1 ) {
    > outfile << endl;
    > }
    > }
    >


    I've meant what I said (although the OP's code seems to have got a bit
    scrambled and I repeated that). Your loop works as well, have you seen a
    problem with mine?

    I prefer mine because it doesn't depend on the iterator being random access.

    john
     
    John Harrison, Sep 22, 2004
    #4
  5. solartimba

    David Hilsee Guest

    "John Harrison" <> wrote in message
    news:...
    >
    > "David Hilsee" <> wrote in message
    > news:...
    > > "John Harrison" <> wrote in message
    > > news:...
    > >>
    > >> "solartimba" <> wrote in message
    > >> news:...
    > >> > Hi,
    > >> > I am in another country, and I need to finish a program. But I do

    not
    > >> > have any reference books. Can you tell me how to print this to a

    file
    > >> > without ending the stream with an endl. Stated differently, I don't
    > >> > want an endl after the last data point printed to the file.
    > >> >
    > >> > I know this is a basic question, and I apologize.
    > >> >
    > >> > ******************************
    > >> >
    > >> > void outFile()
    > >> > {
    > >> > ofstream outfile(outPath.c_str());
    > >> >
    > >> > vector<cVocabulary>::iterator iterSer;
    > >> >
    > >> > for(iterSer=series.begin(); it erSer!=series.end++iterSer
    > >> > {
    > >> > outfile << iterSer->returnVocabulary() << endl;
    > >> > outfile << iterSer->returnVocType() << endl;
    > >> > outfile << iterSer->returnDefinition() << endl;
    > >> > outfile << iterSer->returnExample() << endl;
    > >> > }
    > >> >
    > >> > outfile.close();
    > >> > }
    > >>
    > >> Simple enough, just change your loop
    > >>
    > >> for(iterSer=series.begin(); it erSer!=series.end++iterSer
    > >> {
    > >> if (iterSer != series.begin())
    > >> outfile << endl;
    > >> outfile << iterSer->returnVocabulary() << endl;
    > >> outfile << iterSer->returnVocType() << endl;
    > >> outfile << iterSer->returnDefinition() << endl;
    > >> outfile << iterSer->returnExample();
    > >> }

    > >
    > > You meant the following, right?
    > >
    > > // Note: series.end, and the for loop in general, were changed
    > > for(iterSer=series.begin(); iterSer!=series.end ++iterSer)
    > > {
    > > outfile << iterSer->returnVocabulary() << endl;
    > > outfile << iterSer->returnVocType() << endl;
    > > outfile << iterSer->returnDefinition() << endl;
    > > outfile << iterSer->returnExample();
    > >
    > > // use endl unless this is the last element in the vector
    > > if ( iterSer != series.end() - 1 ) {
    > > outfile << endl;
    > > }
    > > }
    > >

    >
    > I've meant what I said (although the OP's code seems to have got a bit
    > scrambled and I repeated that). Your loop works as well, have you seen a
    > problem with mine?
    >
    > I prefer mine because it doesn't depend on the iterator being random

    access.

    Oops, they do the same thing. My mistake.

    --
    David Hilsee
     
    David Hilsee, Sep 22, 2004
    #5
    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. Replies:
    0
    Views:
    580
  2. Replies:
    3
    Views:
    348
    Eric McCoy
    Dec 22, 2005
  3. Replies:
    0
    Views:
    269
  4. OUTFILE - like creating a log?

    , Dec 11, 2006, in forum: C Programming
    Replies:
    7
    Views:
    402
    goose
    Dec 13, 2006
  5. Bob Hatch
    Replies:
    2
    Views:
    452
    Jeremy Bopp
    Feb 2, 2011
Loading...

Share This Page