fstream and ios::app

fstream and ios::app

Hello!

I have the following simple code:
-------------------------------------------
#include
#include

int main()
{
fstream w;
w.open("file",ios::app);
if ( w.fail() ) {
cout << "Failure!" << endl;
} else {
cout << "Success!" << endl;
w << "Write in the file." << endl;
w.close();
}
}
-------------------------------------------

When I compile it using Intel C++ compiler
and run it after that I have the following
result:

$ icpc stream.C -o istream
$ istream
Failure!
$ cat file
cat: file: No such file or directory

When I compile it using g++ compiler
and run it after that I have the following
result:

$ g++ stream.C -o gstream
$ gstream
Success!
$ cat file
Write in the file.

Any ideas? What could be wrong?

Thanks.
_Gancho.

4 posts / 0 nouveau(x)
Dernière contribution
Reportez-vous à notre Notice d'optimisation pour plus d'informations sur les choix et l'optimisation des performances dans les produits logiciels Intel.

Hello!
Here is more information about the problem
which might be useful:

* Operating System:
Red Hat 7.2

* Intel Compiler:
$ icpc -V
Intel C++ Compiler for 32-bit applications,
Version 7.0 Build 20021021Z

* Linker:
GNU ld version 2.11.90.0.8 (with BFD 2.11.90.0.8)

* Shared library dependencies:
$ ldd istream
libm.so.6 => /lib/i686/libm.so.6 (0x40027000)
libcxa.so.3 => /lsi/soft/intel/ccompiler/linux/7.0-02052003/compiler70/ia32/lib/libcxa.so.3 (0x4004a000)
libc.so.6 => /lib/i686/libc.so.6 (0x40080000)
/lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x40000000)

Thanks,
_Gancho.

Hi Gancho,
I am sure you are using some older version of GNU compiler. According to new ANSI C++ standard ios::app is not a valid file open mode.

Please refer to "Table - 11" (file open modes) of C++ Standard -
http://www.comnets.rwth-aachen.de/doc/c++std/lib-iostreams.html

Replace with . This will work as expected.

You can even try this with latest GNU compilers - v3.x (your test case will fail with gcc v3.x.)

Regards,
CP

Thank you very much for your help!
_Gancho.

Connectez-vous pour laisser un commentaire.