simple outfile question

S

solartimba

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();
}
 
J

John Harrison

solartimba said:
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
 
D

David Hilsee

John Harrison said:
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;
}
}
 
J

John Harrison

David Hilsee said:
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
 
D

David Hilsee

John Harrison said:
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.
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top