home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / os / msdos / programm / 9273 < prev    next >
Encoding:
Internet Message Format  |  1992-09-11  |  2.0 KB

  1. Path: sparky!uunet!kithrup!hoptoad!wet!mcs
  2. From: mcs@wet.UUCP (Gil Nardo)
  3. Newsgroups: comp.os.msdos.programmer
  4. Subject: Re: C700 streams
  5. Message-ID: <4310@wet.UUCP>
  6. Date: 10 Sep 92 19:11:06 GMT
  7. References: <1263@datmuc.UUCP>
  8. Organization: Wetware Diversions, San Francisco
  9. Lines: 65
  10.  
  11. vincent@datmuc.UUCP (vincent) writes:
  12. > #include    <fstream.h>
  13. > #include    <iomanip.h>
  14. > main (int argc, char **argv)
  15. > {
  16. >     fstream ci;
  17. >     char s [3];
  18. >     ci. open (argv [1], ios::in | ios::binary);
  19. >     ci. unsetf (ios::skipws);
  20. >     s [1] = '_';
  21. >     s [2] = '\0';
  22. >     while (ci. good ()) {
  23. >         ci >> s [0];
  24. >         if (ci. eof ())
  25. >             break;
  26. >         cout << s;
  27. >     }
  28. > }
  29. >
  30. > Looks benign, well it is. BUT I've got a problem with it.
  31. > My input file contains "Hello World!"
  32. > My output is:
  33. > __e_l_l_o _W_o_r_l_d_!_
  34.  
  35.  
  36. The problem, if somebody hasn't answered by now, is that in binary
  37. mode the input will not have the automatic translation of 
  38. the linefeed and carriage return characters to '\n'.  Sooooo, what
  39. end up happening is that after print out
  40.  
  41. H_e_l_l_o _W_o_r_l_d_!_
  42.  
  43. you're programming then reads the linefeed char and puts into
  44. your s array: s[0] = '\l', s[1] = '_', s[2] = 0
  45. and end up moving the cursor to the begginning of the line and
  46. overwriting your 'H' char with the '_' char.
  47. I'm sure you can see fixes for this. Here's one (untested):
  48.  
  49.      while (ci. good ()) {
  50.          ci >> s [0];
  51.          if (ci. eof ())
  52.              break;
  53.         if (s[0] == '\0x0A') { // do look ahead 1 char for LF
  54.             char tchar;
  55.             ci >> tchar; // get next char
  56.             // option taken: ignore LF char + EOF
  57.             // option not taken: print s then break
  58.              if (ci. eof ()) break;
  59.             // translate LF-CR to '\n'
  60.             if (tchar == '\0xD') cout << '\n';
  61.             // print LF and look ahead char
  62.             else { cout << s; s[0] = tchar; cout << s }
  63.         } else cout << s; // no look ahead necessary
  64.      }
  65. -- 
  66. Gil Nardo                  | mcs@wet.com
  67. Migrant Computing Services | (415)664-1032
  68. 1032 Irving Street, #435   |-----------------
  69. San Francisco, 94122       | Supernova = *!
  70.