Can not open file with "ios_base::in | ios_base::app"

R

Robin

// Pls note that test.txt exists.
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
fstream fs("test.txt", ios_base::in | ios_base::app);
if(!fs)
{
cout << "Error while opening file!" << endl;
return 1;
}
string s;
getline(fs, s);
cout << s << endl;
fs << "hello" << endl;
return 0;
}

The output is always "Error while opening file!" even when test.txt
exists. What's the problem?

System: Linux 2.6.20-2-generic, Complier: GCC 4.1.2

Thanks very much for your attention.
 
L

Lionel B

// Pls note that test.txt exists.
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
fstream fs("test.txt", ios_base::in | ios_base::app);

The ios_base::app means "append" or "Seek to end before each write", and
only makes sense if you are opening a file for *output* (don't think it
works if ios::in is specified). Try instead just:

fstream fs("test.txt");

as the default open mode for an fstream mode is ios::in|ios::eek:ut, which
looks like what you want.
if(!fs)
{
cout << "Error while opening file!" << endl;
return 1;
}
string s;
getline(fs, s);

You should test whether that read worked...
cout << s << endl;

Looks like you want to append to the file. This sets the write position to
the end of file:

fs.seekp(0,ios::end);

fs << "hello" << endl;

Again you should test whether that write worked... and you should close
the fs once you're done.

fs.close();
 
R

Robin

"Lionel B дµÀ£º
"
On Fri, 05 Jan 2007 04:20:08 -0800, Robin wrote:

Thanks very much for reply.

That kind of usage (ios_base::in | ios_base::app) was found in some
books, for example, Lippman's C++ Primer 3rd Edition. Seems not
supported by newer compilers.
 
L

Lionel B

"Lionel B 写é“:
"

Thanks very much for reply.

That kind of usage (ios_base::in | ios_base::app) was found in some
books, for example, Lippman's C++ Primer 3rd Edition. Seems not
supported by newer compilers.

Yes, I don't know what the Standard says (maybe someone can enlighten us
on this), but it doesn't work for me here on a recent gcc. In principle
I'm not sure why it *shouldn't* work, since read and write positions are
separately maintained.
 
J

Jacek Dziedzic

Robin said:
// Pls note that test.txt exists.
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
fstream fs("test.txt", ios_base::in | ios_base::app);
if(!fs)
{
cout << "Error while opening file!" << endl;
return 1;
}
string s;
getline(fs, s);
cout << s << endl;
fs << "hello" << endl;
return 0;
}

The output is always "Error while opening file!" even when test.txt
exists. What's the problem?

System: Linux 2.6.20-2-generic, Complier: GCC 4.1.2

Thanks very much for your attention.

A wild guess -- perhaps the file exists, but is
not accessible for you to open (sharing violation,
access rights, etc.) at the time this code is ran.

HTH,
- J.
 
B

Bo Persson

Lionel said:
Yes, I don't know what the Standard says (maybe someone can
enlighten us on this), but it doesn't work for me here on a recent
gcc. In principle I'm not sure why it *shouldn't* work, since read
and write positions are separately maintained.

Read and write positions are not separately maintained, there is just one
"stream position". You have to do a seek to change between read and write
modes.

The standard contains an explicit list of possible combinations of open
modes. The in | app combination just isn't in that list.


Bo Persson
 
L

Lionel B

Read and write positions are not separately maintained, there is just one
"stream position". You have to do a seek to change between read and write
modes.

Is that right? I stand corrected then (I thought seekg() and seekp()
maintained separate read/write positions simultaneously).
The standard contains an explicit list of possible combinations of open
modes. The in | app combination just isn't in that list.

Right. Thanks,
 
B

BobR

Lionel B wrote in message ...
Is that right? I stand corrected then (I thought seekg() and seekp()
maintained separate read/write positions simultaneously).
Lionel B

Bo's statement now has me confused[1]. Is it just a GCC thing?
Try this and see what output you get:

#include <iostream>
#include <sstream> // or <fstream>
#include <string>

int main(){
using std::cout; // for NG post
std::stringstream ss( "Hello World!\n" );
cout<<" ss 1 tellp="<<ss.tellp()<<std::endl;
cout<<" ss 1 tellg="<<ss.tellg()<<std::endl;
ss.seekp( 0, std::ios_base::end );
cout<<" ss 2 tellp="<<ss.tellp()<<std::endl;
cout<<" ss 2 tellg="<<ss.tellg()<<std::endl;
ss.seekg( 1 );
ss << "Bite me!\n";
cout<<" ss 3 tellp="<<ss.tellp()<<std::endl;
cout<<" ss 3 tellg="<<ss.tellg()<<std::endl;
std::string Line;
std::getline( ss, Line );
cout<<" stringstream getline(ss, Line) ="<<Line<<std::endl;
cout<<" stringstream ss ="<<ss.str()<<std::endl;
}

/* - output - [ GCC MinGW 3.3.1 ]
ss 1 tellp=0
ss 1 tellg=0
ss 2 tellp=13
ss 2 tellg=0
ss 3 tellp=22
ss 3 tellg=1
stringstream getline(ss, Line) =ello World!
stringstream ss =Hello World!
Bite me!
*/

[1] - unless Bo was only refering to the 'std::ios_base::[ in|app ]' case.
 
P

P.J. Plauger

Lionel B wrote in message ...
Is that right? I stand corrected then (I thought seekg() and seekp()
maintained separate read/write positions simultaneously).
Lionel B

Bo's statement now has me confused[1]. Is it just a GCC thing?
Try this and see what output you get:

#include <iostream>
#include <sstream> // or <fstream>
#include <string>

int main(){
using std::cout; // for NG post
std::stringstream ss( "Hello World!\n" );

stringstream maintains two seek positions, fstream maintains one.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
 
B

BobR

P.J. Plauger wrote in message ...
Bo's statement now has me confused[1]. Is it just a GCC thing?
Try this and see what output you get:
#include <iostream>
#include <sstream> // or <fstream>
#include <string>
int main(){
using std::cout; // for NG post
std::stringstream ss( "Hello World!\n" );

stringstream maintains two seek positions, fstream maintains one.

Thank you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top