gukn9700 wrote:
> The following program opens a text file and wants to append new text
> to it, but everytime I run it, it returns me with "open file fail!".
> Can anybody help?
>
>
> #include <fstream>
> #include <iostream>
> #include <cstdlib>
>
> using namespace std;
>
> int main()
> {
> fstream outfile("test.txt", ios_base::in | ios_base:
ut |
> ios_base::app);
This combination of file modes is not allowed; consequently the file
open attempt fails. FWIW, when specifying append mode, the only legal
ios_base mode combinations are,
out | app
binary | out | app
[see Table 92 in ISO/IEC 14882:1998]
> if (!outfile)
> {
> cout << "open file fail!\n";
> exit(1);
DO NOT use the C library function 'exit()' to terminate a C++ program. I
realize that C programs commonly use exit() to terminate themselves, but
using exit() in a C++ program is asking for trouble. To learn the
specific reasons why you should not use exit() in a C++ program, locate
any good book / FAQ on the C++ programming language.
FWIW, if you want/need to respond to error conditions in a C++ program,
you should use C++'s exception handling mechanism to report/handle these
conditions. [Research the following C++ keywords: try, catch, throw].
> }
> [remainder of code snipped]
>
--
Jim
To reply by email, remove "link" and change "now.here" to "yahoo"
jfischer_link5809{at}now.here.com