Unix Programming - Potential bug with ofstream in linux

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > May 2004 > Potential bug with ofstream in linux





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Potential bug with ofstream in linux
s

2004-05-12, 10:38 am

Can anyone verify this bug for me?

<Code>
#include <iostream>
#include <fstream>

using namespace std;

int main(void)
{
ofstream outfile;
outfile.open("data.txt", ios::out);
outfile.close();
}
</Code>

Attached is the data.txt. When run, the data.txt file is zeroed-out
(has zero length).

Here are my versions numbers:

[sgray@sgray-pc]$ uname -a
Linux sgray-pc 2.4.18-14 #1 Wed Sep 4 13:35:50 EDT 2002 i686 i686 i386
GNU/Linux

[sgray@sgray-pc]$ gcc -v
Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/3.2/specs
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man
--infodir=/usr/share/info --enable-shared --enable-threads=posix
--disable-checking --host=i386-redhat-linux --with-system-zlib
--enable-__cxa_atexit
Thread model: posix
gcc version 3.2 20020903 (Red Hat Linux 8.0 3.2-7)



Thanks,
Stephen

Paolo Carlini

2004-05-12, 10:38 am

s wrote:
> Can anyone verify this bug for me?


Not a bug: according to Table 92 of the C++ Standard, the
'out' openmode corresponds to "w", as stdio equivalent,
which truncates to zero length (C99, 7.19.5.3, p3).

Paolo.
Paul Pluzhnikov

2004-05-12, 10:39 am

s <youshouldbe@home> writes:

> Can anyone verify this bug for me?

....
> Attached is the data.txt. When run, the data.txt file is zeroed-out
> (has zero length).


That's exactly what one expects given your code.

What did *you* expect your code to do? Perhaps you meant to use
'ios::in' instead of 'ios::out'?

Cheers,
--
In order to understand recursion you must first understand recursion.
Remove /-nsp/ for email.
Fletcher Glenn

2004-05-12, 11:52 am

Paul Pluzhnikov wrote:
> s <youshouldbe@home> writes:
>
>
>
> ...
>
>
>
> That's exactly what one expects given your code.
>
> What did *you* expect your code to do? Perhaps you meant to use
> 'ios::in' instead of 'ios::out'?
>
> Cheers,


Or could you have been thinking of ios::app which allows you
to append to the file.

--

Fletcher Glenn

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com