home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / lang / cplus / 18309 < prev    next >
Encoding:
Text File  |  1992-12-22  |  2.7 KB  |  86 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!rational.com!thor!rmartin
  3. From: rmartin@thor.Rational.COM (Bob Martin)
  4. Subject: Re: Questions about iostream. Please help!
  5. Message-ID: <rmartin.725042821@thor>
  6. Sender: news@rational.com
  7. Organization: Rational
  8. References: <1gt91eINNfrf@miles.mps.ohio-state.edu>
  9. Date: Tue, 22 Dec 1992 16:47:01 GMT
  10. Lines: 74
  11.  
  12. ren@miles.mps.ohio-state.edu (Liming Ren) writes:
  13.  
  14. |The following program was posted in this group (not by me) a while ago. 
  15.  
  16. |#include    <iostream.h>
  17. |#include    <strstream.h>
  18. |#include    <fstream.h>
  19. |int main( int argc, char ** argv )
  20. |{
  21. |    istream * is;
  22. |    if( argc == 2 ){
  23. |        is = new ifstream( argv[ 1 ] );
  24. |    }else{
  25. |        is = &cin;
  26. |    }
  27. |    while( *is ){
  28. |        ostrstream out_str;
  29. |        is->get( *out_str.rdbuf( ));
  30. |        is->ignore( 1, '\n' );
  31. |        cout <<out_str.rdbuf( )<< endl;
  32. |    }
  33.  
  34. |    if( argc == 2 ){    // Delete the object created. 
  35. |        delete is;
  36. |    }
  37. |    return 0;
  38. |}
  39.  
  40.  
  41. |After I study it, I have two questions:
  42.  
  43. |(1) If I use it as : a.out file1>file2, diff tells me there is an
  44. |extra newline added to file2 at the end of file. I don't see how this
  45. |can happen. How to correct it?
  46.  
  47. I think that there is one extra loop happening at the end.  The 'get'
  48. on the last line terminates before the line-end.  The ignore gobbles
  49. the line end, but the EOF has not been reached.  The loop continues
  50. because EOF has not been reached.  The next get extracts nothing
  51. because it hits the EOF.  The ignore finds no line-end.  But the cout
  52. puts one out anyway.  
  53.  
  54. |(1) About the main while loop. It is my understanding that *is is a
  55. |class instance of istream with the following picyure:
  56.  
  57. |    ______             _____________________
  58. |    | is |---------->|                   |
  59. |    ------           |                   |
  60. |                     | istream           |
  61. |                     |___________________|
  62.  
  63. |How (*is) can be zero and how it is set to zero? Does this mean that
  64. |the class instance is zero? I am comfused here.
  65.  
  66. *is points to an istream.  An istream is a derivative of class ios.
  67. ios declares operator void*().  Thus a "user-defined" conversion takes
  68. place whenever an ios object appears in a conditional syntax.  The ios
  69. is converted to a void*.  The void* will be zero if the failbit or
  70. badbit of the ios is set.  Otherwise the void* will be non-zero.
  71.  
  72. Thus you can do cute things like:
  73.  
  74. while (cin) {...}
  75.  
  76. which will loop until cin complains about something, typically EOF.
  77.  
  78.  
  79.  
  80.  
  81. --
  82. Robert Martin                        Training courses offered in:
  83. R. C. M. Consulting                       Object Oriented Analysis
  84. 2080 Cranbrook Rd.                        Object Oriented Design
  85. Green Oaks, Il 60048 (708) 918-1004       C++
  86.