Topic: Mixing C and C++ I/O (FILE * and iostream)


Author: "Sergey P. Derevyago" <non-existent@iobox.com>
Date: 6 Feb 2002 02:16:52 GMT
Raw View
"James Kuyper Jr." wrote:
> >         Does the standard give "some guarantees"?
> I couldn't find any. Only the standard streams (cin, cout, clog, and
> wide equivalents) are required to interoperate with their C library
> equivalents (stdin, stdout, stderr, and wide equivalents). Even those
> aren't required to continue doing so, if sync_with_stdio(false) is
> called.
 Yes, it's the point :)
 So the fragment in question (... Probably you'll get inconsistent results,
seeing some parts of the file twice and others not at all ...) is rather
confusing :)
--
         With all respect, Sergey.          http://cpp3.virtualave.net/
         mailto : ders at skeptik.net



      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]

[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]






Author: xleobx@qmailcomq.com
Date: 6 Feb 2002 02:16:51 GMT
Raw View
In comp.std.c++ Sergey P. Derevyago <non-existent@iobox.com> wrote:

> Matthew Austern wrote:
>> I wrote a column on this that you may find useful:
>> http://www.cuj.com/experts/1811/austern.htm?topic=experts
>  A the URL above contains the following fragment:
> -----------------------------------8<-----------------------------------
> Suppose, for example, that you open a file for reading both with
> iostreams and with stdio:

> std::ifstream f1("file.txt");
> FILE* f2 = fopen("file.txt");

> What happens if you try to read from both f1 and f2?

> The C++ Standard doesn't say what happens; it doesn't give any
> guarantee that this sort of thing will work. In fact, it probably won't.
> Probably you'll get inconsistent results, seeing some parts of the
> file twice and others not at all. If you want to view a file with
> iostreams and stdio at the same time, you'll have to extend the
> standard library.
> -----------------------------------8<-----------------------------------
> I'm really surprised! What does it mean: "... it doesn't give any guarantee
> that this sort of thing will work"?
>  IMHO this sort of thing will surely work. f1 and f2 must be absolutely
> independent (i.e. they must use different file descriptors).

The example is not good (it hides the matter of the question).
Consider fdopen and its iostreams analog applied to _one_ file descriptor.
In this case mixing stdio and iostreams will definitely produce inconsistent
results.


 Leo



      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]

[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]






Author: "James Kuyper Jr." <kuyper@wizard.net>
Date: 4 Feb 2002 16:49:41 GMT
Raw View
"Sergey P. Derevyago" wrote:
....
>         Let's consider the following examples with the same question:
>
> 1.
> ifstream f1("file.txt");
> ifstream f2("file.txt");
>
> 2.
> FILE* f1 = fopen("file.txt");
> FILE* f2 = fopen("file.txt");
>
> 3.
> ifstream f1("file1.txt");
> FILE* f2 = fopen("file2.txt");  // where file2.txt is a link to file1.txt
>
>         Does the standard give "some guarantees"?

I couldn't find any. Only the standard streams (cin, cout, clog, and
wide equivalents) are required to interoperate with their C library
equivalents (stdin, stdout, stderr, and wide equivalents). Even those
aren't required to continue doing so, if sync_with_stdio(false) is
called.



      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]

[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]






Author: "Sergey P. Derevyago" <non-existent@iobox.com>
Date: 1 Feb 2002 22:21:49 GMT
Raw View
Matthew Austern wrote:
> I wrote a column on this that you may find useful:
> http://www.cuj.com/experts/1811/austern.htm?topic=experts
 A the URL above contains the following fragment:
-----------------------------------8<-----------------------------------
Suppose, for example, that you open a file for reading both with
iostreams and with stdio:

std::ifstream f1("file.txt");
FILE* f2 = fopen("file.txt");

What happens if you try to read from both f1 and f2?

The C++ Standard doesn't say what happens; it doesn't give any
guarantee that this sort of thing will work. In fact, it probably won't.
Probably you'll get inconsistent results, seeing some parts of the
file twice and others not at all. If you want to view a file with
iostreams and stdio at the same time, you'll have to extend the
standard library.
-----------------------------------8<-----------------------------------
 I'm really surprised! What does it mean: "... it doesn't give any guarantee
that this sort of thing will work"?
 IMHO this sort of thing will surely work. f1 and f2 must be absolutely
independent (i.e. they must use different file descriptors).

 Let's consider the following examples with the same question:

1.
ifstream f1("file.txt");
ifstream f2("file.txt");

2.
FILE* f1 = fopen("file.txt");
FILE* f2 = fopen("file.txt");

3.
ifstream f1("file1.txt");
FILE* f2 = fopen("file2.txt");  // where file2.txt is a link to file1.txt

 Does the standard give "some guarantees"?
--
         With all respect, Sergey.          http://cpp3.virtualave.net/
         mailto : ders at skeptik.net

      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]

[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]