Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > simple outfile question

Reply
Thread Tools

simple outfile question

 
 
solartimba
Guest
Posts: n/a
 
      09-21-2004
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();
}
 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      09-21-2004

"solartimba" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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


 
Reply With Quote
 
 
 
 
David Hilsee
Guest
Posts: n/a
 
      09-22-2004
"John Harrison" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> "solartimba" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) om...
> > 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


 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      09-22-2004

"David Hilsee" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "John Harrison" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>>
>> "solartimba" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed) om...
>> > 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


 
Reply With Quote
 
David Hilsee
Guest
Posts: n/a
 
      09-22-2004
"John Harrison" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> "David Hilsee" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > "John Harrison" <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed)...
> >>
> >> "solartimba" <(E-Mail Removed)> wrote in message
> >> news:(E-Mail Removed) om...
> >> > 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


 
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
Help with outfile.write(line) unless line =~ search_text Bob Hatch Ruby 2 02-02-2011 07:41 PM
OUTFILE - like creating a log? rhitz1218@gmail.com C Programming 7 12-13-2006 12:55 PM
type error on porting outfile.write pmiller@gnf.org Python 3 12-22-2005 12:11 AM
type error on porting outfile.write pmiller@gnf.org Python 0 12-20-2005 11:46 PM
Re: Simple Simple question!!! ashelley@inlandkwpp.com ASP .Net 0 06-25-2004 04:18 PM



Advertisments