Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > input and output questions about file

Reply
Thread Tools

input and output questions about file

 
 
Dic4000@163.com
Guest
Posts: n/a
 
      02-24-2007
下面程序建立不了文件,不知道哪里出错了?

只想定义一个fstream类型来完成输入输出的工作.
#include<iostream>
#include<conio.h>
#include<fstream>


using namespace std;


int main()
{
fstream file("a.txt",fstream::in|fstream: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?

 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      02-24-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> 下面程序建立不了文件,不知道哪里出错了?
>
> 只想定义一个fstream类型来完成输入输出的工作.
> #include<iostream>
> #include<conio.h>
> #include<fstream>
>
>
> using namespace std;
>
>
> int main()
> {
> fstream file("a.txt",fstream::in|fstream: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
 
Reply With Quote
 
 
 
 
Dic4000@163.com
Guest
Posts: n/a
 
      02-24-2007
On 2月25日, 上午12时24分, John Harrison <(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote:
> > 下面程序建立不了文件,不知道哪里出错了?

>
> > 只想定义一个fstream类型来完成输入输出的工作.
> > #include<iostream>
> > #include<conio.h>
> > #include<fstream>

>
> > using namespace std;

>
> > int main()
> > {
> > fstream file("a.txt",fstream::in|fstream: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- 隐藏被引用文字 -
>
> - 显示引用的文字 -


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

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      02-24-2007
(E-Mail Removed) wrote:
> 下面程序建立不了文件,不知道哪里出错了?
>
> 只想定义一个fstream类型来完成输入输出的工作.
> #include<iostream>
> #include<conio.h>
> #include<fstream>
>
>
> using namespace std;
>
>
> int main()
> {
> fstream file("a.txt",fstream::in|fstream: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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
Chris Theis
Guest
Posts: n/a
 
      02-24-2007
"Victor Bazarov" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed). ..
> (E-Mail Removed) wrote:
>> 下面程序建立不了文件,不知道哪里出错了?
>>
>> 只想定义一个fstream类型来完成输入输出的工作.
>> #include<iostream>
>> #include<conio.h>
>> #include<fstream>
>>
>>
>> using namespace std;
>>
>>
>> int main()
>> {
>> fstream file("a.txt",fstream::in|fstream: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'.


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:ut and this should do the trick. You actually won't need to
supply the flags yourself.

HTH
Chris

 
Reply With Quote
 
BobR
Guest
Posts: n/a
 
      02-24-2007

Chris Theis <chris.theis@n o s p am.cern.ch> wrote in message ...
> "Victor Bazarov" <(E-Mail Removed)> wrote in message...
> > (E-Mail Removed) wrote:
> >> 下面程序建立不了文件,不知道哪里出错了?
> >>
> >> 只想定义一个fstream类型来完成输入输出的工作.
> >> #include<iostream>
> >> #include<conio.h>
> >> #include<fstream>
> >> using namespace std;
> >> int main(){
> >> fstream file("a.txt",fstream::in|fstream:ut|fstream::app );
> >> string s1,s2;
> >> if(!file) cerr<<"error"<<endl; file://why the program have
> >> file://error when connecting

up "a.txt"
> >> s1="abcd 1234\n";
> >> file<<s1; file://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'.

>
> 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: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";

--
Bob R
POVrookie


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Input from file and output to file - VHDL Emel VHDL 2 01-29-2010 08:01 AM
input and output questions about file george972@mailinator.com C Programming 34 05-29-2009 10:48 AM
several input-output questions Jess C++ 10 06-18-2007 09:56 AM
input and output questions about file Dic4000@163.com C Programming 6 02-24-2007 05:18 PM
java input and output stream to the same file at the same time? Krick Java 1 08-15-2003 05:55 PM



Advertisments