The following program is used to open a file in mode both read and
append, but it has
a problem that the file can't be opened.
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
int main()
{
// open in both input and append mode
fstream inOut("copy.out", ios_base::in|ios_base::app);
//fstream inOut("copy.out", ios_base::in);
if (!inOut)
{
cerr << "failed to open file!" << endl;
exit(1);
}
// do something with inOut
inOut.close()
return 0;
}
When I compile and run it, the result says "failed to open file!" .
But if I change to the commend line which only contain mode
ios_base::in,then
file can be opened successfully.
Why dose this happen? I wonder whether ios_base::in can't use with
ios_base::app together? But it does work in BS's book.
Can any one give me some help? 5 3920
"ehui928" <eh*****@gmail.comskrev i meddelandet
news:11*********************@h48g2000cwc.googlegro ups.com...
The following program is used to open a file in mode both read and
append, but it has
a problem that the file can't be opened.
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
int main()
{
// open in both input and append mode
fstream inOut("copy.out", ios_base::in|ios_base::app);
//fstream inOut("copy.out", ios_base::in);
if (!inOut)
{
cerr << "failed to open file!" << endl;
exit(1);
}
// do something with inOut
inOut.close()
return 0;
}
When I compile and run it, the result says "failed to open file!" .
But if I change to the commend line which only contain mode
ios_base::in,then
file can be opened successfully.
Why dose this happen? I wonder whether ios_base::in can't use with
ios_base::app together?
You can't. The combination is not in the list of file open modes in
the standard. You can only use app with out.
If you think about it, what would read-append mean ?
But it does work in BS's book.
So Bjarne doesn't know C++? :-)
Or perhaps it has changed since he wrote it?
Bo Persson
TO Bo Persson:
thanks first!
You can't. The combination is not in the list of file open modes in
the standard. You can only use app with out.
If you think about it, what would read-append mean ?
Read-append means that I want write something at the end of a file
while reading it(for example, when a particular character has been read
then I want append something to the end of the file).
Now , I changed the line used to open the file to following
fstream inOut("copy.out", ios_base::in|ios_base::out|ios_base::app);
the program works fine , and get the correct result.
Anyway, I still have some doubt in it.
ehui928 wrote:
TO Bo Persson:
thanks first!
>You can't. The combination is not in the list of file open modes in the standard. You can only use app with out.
If you think about it, what would read-append mean ?
Read-append means that I want write something at the end of a file
If you want to write to the file, you need to open it for writing.
Rolf Magnus wrote:
ehui928 wrote:
TO Bo Persson:
thanks first!
You can't. The combination is not in the list of file open modes in
the standard. You can only use app with out.
If you think about it, what would read-append mean ?
Read-append means that I want write something at the end of a file
If you want to write to the file, you need to open it for writing.
"ios::app implies ios::out.", as several (presumably old) pages seem to
claim. http://techpubs.sgi.com/library/tpl/...B%2B/fstream.z
However, when giving just "fstream::app", the open fails, and when
giving
"fstream::app|fstream::out" as arg to the fstream constructor, it goes
OK
(linux, g++ 4.0.4). So, apparently all of them manuals are old.
Still, using app|out|in fails, so apparently one isn't suppose to open
a file both
for reading and appending. Granted, I never needed to do so, but hey,
maybe one day I might. jo******@gmail.com wrote:
"ios::app implies ios::out.", as several (presumably old) pages seem to
claim.
http://techpubs.sgi.com/library/tpl/...B%2B/fstream.z
>
However, when giving just "fstream::app", the open fails, and when
giving "fstream::app|fstream::out" as arg to the fstream constructor, it
goes OK (linux, g++ 4.0.4). So, apparently all of them manuals are old.
I don't know if those manuals are old, but the API they describe is. This is
not the standard library class std::fstream. You can easily see that
because that would be defined in <fstreamand within namespace std, while
the class that manual page is referring to is defined in <fstream.hand
not in namespace std. This is probably a predecessor to the standard
fstream class. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Marc Schellens |
last post by:
fstream.open( name, mode) fails
fstream.fail() returns true.
How can I find out what went wrong?
(ie. file does not exist for reading, permissions...)
thanks,
marc
|
by: Brandon McCombs |
last post by:
This may be the wrong group but I didn't see anything for VC++ so I'm
trying here.
I have a C++ book by Deitel and Deitel that says I can use fstream
File("data.dat", ios::in | ios::out |...
|
by: Mike - EMAIL IGNORED |
last post by:
Using RH9, from this fragment:
fstream theStream_;
string fPath = "myFile";
int mode = O_EXCL | O_CREAT;
theStream_.open(fPath.c_str(),mode);
I get the failure shown below. ...
|
by: dumboo |
last post by:
hi there
i have been trying to open file using fstream, in read+write+binary mode
fstream ff;
ff.open(filename, ios::in | ios::out | ios::binary);
now this works
what i m looking for is,...
|
by: Rate Spring |
last post by:
hi, in win2000 and VC6, I operate a txt file. When I write a line data into
file and add "\r\n" at the end of line, I find:
fstream << "\r\n" write 3 chars : 0D 0D 0A
fstream << "\n" ...
|
by: wiso |
last post by:
My problem is this (from:
http://www.cplusplus.com/ref/iostream/fstream/open.html)
#include <fstream>
using namespace std;
int main()
{
fstream f;
|
by: vinothg |
last post by:
I have a binary file,which contains strings of 30 bytes each.I need to open the file,read the strings one by one and if the string is not found i need to write it.But unfortunately both read and...
|
by: Dic4000 |
last post by:
ÏÂÃæ³ÌÐò½¨Á¢²»ÁËÎļþ,²»ÖªµÀÄÄÀï³ö´íÁË?
Ö»Ï붨ÒåÒ»¸öfstreamÀàÐÍÀ´Íê³ÉÊäÈëÊä³öµÄ¹¤×÷.
#include<iostream>
#include<conio.h>
#include<fstream>
using namespace std;
|
by: Sachin Garg |
last post by:
I have a program which opens a fstream in binary input+output mode, creating
the file if it doesn't exists. But writing doesn't works after reading, it
must be something obvious that I am not aware...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |