Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #118598
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Re: fstreams |
| Date | 2024-03-28 09:27 +0000 |
| Organization | A noiseless patient Spider |
| Message-ID | <87le62vh8d.fsf@bsb.me.uk> (permalink) |
| References | <streams-20240327161022@ram.dialup.fu-berlin.de> <cone.1711575132.4129.32648.1004@monster.email-scan.com> <Ka1NN.466293$vFZa.325839@fx13.iad> |
scott@slp53.sl.home (Scott Lurndal) writes:
> Sam <sam@email-scan.com> writes:
>>Stefan Ram writes:
>>
>>> This (barring typos) is from a book by Bjarne:
>>>
>>> int main()
>>> {
>>> string from, to; /* get source and target file names */
>>> cin >> from >> to;
>>>
>>> ifstream is( from.c_str() ); /* open input stream */
>>> ofstream os( to.c_str() ); /* open output stream */
>>>
>>> istream_iterator<string> ii(is); /* make input iterator for stream */
>>> istream_iterator<string> eos; /* input sentinel */
>>> ostream_iterator<string> oo(os,"\n"); /* make out it. for stream */
>>>
>>> set<string> b(ii, eos); /* b is a set initialized from input */
>>> copy(b.begin(), b.end(), oo); /* copy buffer to output */ }
>>>
>>> So, is this good style not checking the stream states after opening?
>>
>>What's the purpose of the above example? Looks to me like it's demonstrating
>>some basic stream operation, so error checking is immaterial. I would think
>>that there'll be a separate discussion about error checking where this topic
>>would be discussed.
>
> It's not clear from which book Stefan found this example. It
> doesn't work correctly, however:
>
> $ cat a.cpp
> #include <string>
> #include <fstream>
> #include <iostream>
> #include <iterator>
> #include <set>
>
> using namespace std;
>
> int main()
> {
> string from, to; /* get source and target file names */
> cin >> from >> to;
>
> ifstream is( from.c_str() ); /* open input stream */
> ofstream os( to.c_str() ); /* open output stream */
>
> istream_iterator<string> ii(is); /* make input iterator for stream */
> istream_iterator<string> eos; /* input sentinel */
> ostream_iterator<string> oo(os,"\n"); /* make out it. for stream */
>
> set<string> b(ii, eos); /* b is a set initialized from input */
> copy(b.begin(), b.end(), oo); /* copy buffer to output */
> }
>
> $ ./a
> i.png
> j.png
> $ ls -l ?.png
> -rw-------. 1 scott scott 2413 Mar 27 13:58 i.png
> -rw-rw-r--. 1 scott scott 2411 Mar 27 15:27 j.png
>
> The output file is two bytes shorter than the input file.
How do you know it is not working correctly?.
The program is not file copy program. SR's first language is not
English so "copy buffer to output" is understandably not the clearest
description. The "copy" is done "through" a set to list the unique
strings in the input.
--
Ben.
Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Re: fstreams Sam <sam@email-scan.com> - 2024-03-27 17:32 -0400
Re: fstreams scott@slp53.sl.home (Scott Lurndal) - 2024-03-27 22:35 +0000
Re: fstreams Michael S <already5chosen@yahoo.com> - 2024-03-28 01:23 +0200
Re: fstreams "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-03-27 18:41 -0700
Re: fstreams Ben Bacarisse <ben.usenet@bsb.me.uk> - 2024-03-28 09:27 +0000
Re: fstreams Ralf Fassel <ralfixx@gmx.de> - 2024-03-28 10:37 +0100
Re: fstreams Sam <sam@email-scan.com> - 2024-03-28 07:24 -0400
Re: fstreams scott@slp53.sl.home (Scott Lurndal) - 2024-03-28 14:16 +0000
Re: fstreams wij <wyniijj5@gmail.com> - 2024-03-28 23:02 +0800
Re: fstreams "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-03-28 12:44 -0700
csiph-web