Article View: comp.os.msdos.programmer
Article #1443Re: C700 streams
From: Gil Nardo
Date: Thu, 10 Sep 1992 19:11
Date: Thu, 10 Sep 1992 19:11
65 lines
2082 bytes
2082 bytes
vincent@datmuc.UUCP (vincent) writes:
>
> #include <fstream.h>
> #include <iomanip.h>
>
> main (int argc, char **argv)
> {
> fstream ci;
> char s [3];
>
> ci. open (argv [1], ios::in | ios::binary);
> ci. unsetf (ios::skipws);
>
> s [1] = '_';
> s [2] = '\0';
>
> while (ci. good ()) {
> ci >> s [0];
> if (ci. eof ())
> break;
> cout << s;
> }
> }
>
> Looks benign, well it is. BUT I've got a problem with it.
> My input file contains "Hello World!"
> My output is:
>
> __e_l_l_o _W_o_r_l_d_!_
The problem, if somebody hasn't answered by now, is that in binary
mode the input will not have the automatic translation of
the linefeed and carriage return characters to '\n'. Sooooo, what
end up happening is that after print out
H_e_l_l_o _W_o_r_l_d_!_
you're programming then reads the linefeed char and puts into
your s array: s[0] = '\l', s[1] = '_', s[2] = 0
and end up moving the cursor to the begginning of the line and
overwriting your 'H' char with the '_' char.
I'm sure you can see fixes for this. Here's one (untested):
while (ci. good ()) {
ci >> s [0];
if (ci. eof ())
break;
if (s[0] == '\0x0A') { // do look ahead 1 char for LF
char tchar;
ci >> tchar; // get next char
// option taken: ignore LF char + EOF
// option not taken: print s then break
if (ci. eof ()) break;
// translate LF-CR to '\n'
if (tchar == '\0xD') cout << '\n';
// print LF and look ahead char
else { cout << s; s[0] = tchar; cout << s }
} else cout << s; // no look ahead necessary
}
--
Gil Nardo | mcs@wet.com
Migrant Computing Services | (415)664-1032
1032 Irving Street, #435 |-----------------
San Francisco, 94122 | Supernova = *!
Technical Details
Message-ID:
<4310@wet.UUCP>Path:
arc1-ca.pugleaf.net!sparky!uunet!kithrup!hoptoad!wet!mcsReferences:
<1263@datmuc.UUCP>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!kithrup!hoptoad!wet!mcs From: mcs@wet.UUCP (Gil Nardo) Newsgroups: comp.os.msdos.programmer Subject: Re: C700 streams Message-ID: <4310@wet.UUCP> Date: 10 Sep 92 19:11:06 GMT References: <1263@datmuc.UUCP> Organization: Wetware Diversions, San Francisco Lines: 65