input and output questions about file

D

Dic4000

ÏÂÃæ³ÌÐò½¨Á¢²»ÁËÎļþ,²»ÖªµÀÄÄÀï³ö´íÁË?

Ö»Ï붨ÒåÒ»¸öfstreamÀàÐÍÀ´Íê³ÉÊäÈëÊä³öµÄ¹¤×÷.
#include<iostream>
#include<conio.h>
#include<fstream>


using namespace std;


int main()
{
fstream file("a.txt",fstream::in|fstream::eek:ut|fstream::app);
string s1,s2;


if(!file) cerr<<"error"<<endl; //why the program have
//error when connecting
up "a.txt"


s1="abcd 1234\n";
file<<s1; //why don't write in a.txt?
file.flush();
file.seekg(0);
file>>s2;
cout<<"s2="<<s2<<endl;//s2 is empty


file.close();


getch();
return 0;}


the program can run without any error,but the result is:


error


s2=


the program don't create file "a.txt",How do i do?
 
J

John Harrison

ÏÂÃæ³ÌÐò½¨Á¢²»ÁËÎļþ,²»ÖªµÀÄÄÀï³ö´íÁË?

Ö»Ï붨ÒåÒ»¸öfstreamÀàÐÍÀ´Íê³ÉÊäÈëÊä³öµÄ¹¤×÷.
#include<iostream>
#include<conio.h>
#include<fstream>


using namespace std;


int main()
{
fstream file("a.txt",fstream::in|fstream::eek:ut|fstream::app);
string s1,s2;


if(!file) cerr<<"error"<<endl; //why the program have
//error when connecting
up "a.txt"


s1="abcd 1234\n";
file<<s1; //why don't write in a.txt?
file.flush();
file.seekg(0);
file>>s2;
cout<<"s2="<<s2<<endl;//s2 is empty


file.close();


getch();
return 0;}


the program can run without any error,but the result is:


error


s2=


the program don't create file "a.txt",How do i do?

ofstream file("a.txt");

The file doesn't get created because you said you wanted to read from it
(that's what fstream::in does). Use ofstream for files you want to write
to, and ifstream from files you want to read from.

john
 
D

Dic4000

ofstream file("a.txt");

The file doesn't get created because you said you wanted to read from it
(that's what fstream::in does). Use ofstream for files you want to write
to, and ifstream from files you want to read from.

john- Òþ²Ø±»ÒýÓÃÎÄ×Ö -

- ÏÔʾÒýÓõÄÎÄ×Ö -

but I wanna use fstream type ,not ifstream or ofstream type,only use
it to complete input and output working.
 
V

Victor Bazarov

ÏÂÃæ³ÌÐò½¨Á¢²»ÁËÎļþ,²»ÖªµÀÄÄÀï³ö´íÁË?

Ö»Ï붨ÒåÒ»¸öfstreamÀàÐÍÀ´Íê³ÉÊäÈëÊä³öµÄ¹¤×÷.
#include<iostream>
#include<conio.h>
#include<fstream>


using namespace std;


int main()
{
fstream file("a.txt",fstream::in|fstream::eek:ut|fstream::app);
string s1,s2;


if(!file) cerr<<"error"<<endl; //why the program have
//error when connecting
up "a.txt"


s1="abcd 1234\n";
file<<s1; //why don't write in a.txt?
file.flush();
file.seekg(0);
file>>s2;
cout<<"s2="<<s2<<endl;//s2 is empty


file.close();


getch();
return 0;}


the program can run without any error,but the result is:


error

Doesn't that contradict what you just wrote? How can the program
run without any error, yet display "error"?
s2=


the program don't create file "a.txt",How do i do?

I am not sure, but I think 'ios::in' is mutually exclusive with
'ios::app'. Try droppint 'ios::app'.

V
 
C

Chris Theis

Victor Bazarov said:
Doesn't that contradict what you just wrote? How can the program
run without any error, yet display "error"?


I am not sure, but I think 'ios::in' is mutually exclusive with
'ios::app'. Try droppint 'ios::app'.

You're absolutely right on this one.

To the OP:

What you've been looking for is the stdio equivalent to "a+". However, the
standard mandates that only certain combinations of file open modes can be
supplied (see Table 92 of ISO:IEC 14882:2003(E)). Otherwise, your test of
the stream will fail as you've experienced. As Victor suggested already,
ios::app can only be used if neither ios::in nor ios::trunc is given.

By default there is a standard ctor for fstreams with the open mode set to
ios::in | ios::eek:ut and this should do the trick. You actually won't need to
supply the flags yourself.

HTH
Chris
 
B

BobR

Chris Theis said:
You're absolutely right on this one.

To the OP:

What you've been looking for is the stdio equivalent to "a+". However, the
standard mandates that only certain combinations of file open modes can be
supplied (see Table 92 of ISO:IEC 14882:2003(E)). Otherwise, your test of
the stream will fail as you've experienced. As Victor suggested already,
ios::app can only be used if neither ios::in nor ios::trunc is given.

By default there is a standard ctor for fstreams with the open mode set to
ios::in | ios::eek:ut and this should do the trick. You actually won't need to
supply the flags yourself.

HTH
Chris

Add: For the 'app' part, you might try:

file.seekp( 0, std::ios::end ); // note: not 'seekg'.

file<<"output stuff";
 

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

Forum statistics

Threads
473,968
Messages
2,570,149
Members
46,695
Latest member
StanleyDri

Latest Threads

Top