flushing streams

M

Mr. Mxyztplk

So I've got Windows/DOS formatted base64 lines in a file, e.g.

ZE1ipHGlcWj4xISPGajKkV56emvBDHT862dCo72EDfWFmGlOh98xlYQY902uAjIy
QydwBgYcLYnCYOKXWwAePvHlouLugXCDFR1HLaKQ2/e51Qzn9yEEl4w+QPtd9G6y
RrzmDa2Pypcs9rwpD8TjExgK+40F/eMVWeduwpYPHsxO5ryldT1H9olQ15zK8pMQ

etc.

as per Dos\Windows we have a carriage return and line feed formation of
'\n' at the end of each of these lines. The upshot is that I want to
strip the newlines and process this as a continuous stream without said
newlines, e. g.

ZE1ipHGlcWj4xISPGajKkV56emvBDHT862dCo72EDfWFmGlOh98xlYQY902uAjIyQydwBgYc
LYnCYOKXWwAePvHlouLugXCDFR1HLaKQ2/e51Qzn9yEEl4w+QPtd9G6yRrzmDa2Pypcs9rwp
D8TjExgK+40F/eMVWeduwpYPHsxO5ryldT1H9olQ15zK8pMQ

(of course disregard the wrapping, it would be one long stream...)

and I want to do this in a manner more efficient than going through
every byte in the stream and erasing the newlines.


This code snippet
stringstream ss;
string line;
fin.open("ciphertext.b64", ios::in | ios::binary);
ss << fin.rdbuf();
fin.close();
getline(ss, line);
cout << line << flush;
getline(ss, line);
cout << line << flush;

is giving me the output
QydwBgYcLYnCYOKXWwAePvHlouLugXCDFR1HLaKQ2/e51Qzn9yEEl4w+QPtd9G6y

rather than what I should expect, i. e.

ZE1ipHGlcWj4xISPGajKkV56emvBDHT862dCo72EDfWFmGlOh98xlYQY902uAjIyQydwBgYc
LYnCYOKXWwAePvHlouLugXCDFR1HLaKQ2/e51Qzn9yEEl4w+QPtd9G6y

Harsh criticisms of my stunted coding would be much appreciated.
 
M

Mr. Mxyztplk

@speranza.aioe.org:

Never mind, a simple substitution of
ss >> line;
cout << line;
ss >> line;
cout << line;

for
getline(ss, line);
cout << line << flush;
getline(ss, line);
cout << line << flush;

yields one the sought after behavior.
 
B

Barry Schwarz

@speranza.aioe.org:

Never mind, a simple substitution of
ss >> line;
cout << line;
ss >> line;
cout << line;

for


yields one the sought after behavior.

If any of the binary bytes happen to match a white space character,
your code will skip them. White space characters at the beginning of
the extraction are skipped. A white space character after a sequence
of non-space characters serves to terminate the extraction. The
combination of these two behaviors insures that line will never
contain a white space character.
 
M

Mr. Mxyztplk

If any of the binary bytes happen to match a white space character,
your code will skip them. White space characters at the beginning of
the extraction are skipped. A white space character after a sequence
of non-space characters serves to terminate the extraction. The
combination of these two behaviors insures that line will never
contain a white space character.

I was missing the dangling line feed as it were, which then did precisely
what it was designed to do and sent cout back to the beginning of the line.
 

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,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top