input and output questions about file

Discussion in 'C++' started by Dic4000@163.com, Feb 24, 2007.

  1. Guest

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

    Ö»Ï붨ÒåÒ»¸ö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?
    , Feb 24, 2007
    #1
    1. Advertising

  2. wrote:
    > ÏÂÃæ³ÌÐò½¨Á¢²»ÁËÎļþ,²»ÖªµÀÄÄÀï³ö´íÁË?
    >
    > Ö»Ï붨ÒåÒ»¸ö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
    John Harrison, Feb 24, 2007
    #2
    1. Advertising

  3. Guest

    On 2ÔÂ25ÈÕ, ÉÏÎç12ʱ24·Ö, John Harrison <> wrote:
    > wrote:
    > > ÏÂÃæ³ÌÐò½¨Á¢²»ÁËÎļþ,²»ÖªµÀÄÄÀï³ö´íÁË?

    >
    > > Ö»Ï붨ÒåÒ»¸ö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- Òþ²Ø±»ÒýÓÃÎÄ×Ö -
    >
    > - ÏÔʾÒýÓõÄÎÄ×Ö -


    but I wanna use fstream type ,not ifstream or ofstream type,only use
    it to complete input and output working.
    , Feb 24, 2007
    #3
  4. wrote:
    > ÏÂÃæ³ÌÐò½¨Á¢²»ÁËÎļþ,²»ÖªµÀÄÄÀï³ö´íÁË?
    >
    > Ö»Ï붨ÒåÒ»¸ö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
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Feb 24, 2007
    #4
  5. Chris Theis Guest

    "Victor Bazarov" <> wrote in message
    news:...
    > wrote:
    >> ÏÂÃæ³ÌÐò½¨Á¢²»ÁËÎļþ,²»ÖªµÀÄÄÀï³ö´íÁË?
    >>
    >> Ö»Ï붨ÒåÒ»¸ö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'.


    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
    Chris Theis, Feb 24, 2007
    #5
  6. BobR Guest

    Chris Theis <chris.theis@n o s p am.cern.ch> wrote in message ...
    > "Victor Bazarov" <> wrote in message...
    > > wrote:
    > >> ÏÂÃæ³ÌÐò½¨Á¢²»ÁËÎļþ,²»ÖªµÀÄÄÀï³ö´íÁË?
    > >>
    > >> Ö»Ï붨ÒåÒ»¸ö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; 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::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";

    --
    Bob R
    POVrookie
    BobR, Feb 24, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Emel
    Replies:
    2
    Views:
    5,865
    nish a
    Jan 29, 2010
  2. Krick
    Replies:
    1
    Views:
    1,454
    John C. Bollinger
    Aug 15, 2003
  3. Replies:
    6
    Views:
    445
    CBFalconer
    Feb 24, 2007
  4. Jess
    Replies:
    10
    Views:
    575
  5. Replies:
    34
    Views:
    1,170
    James Kuyper
    May 29, 2009
Loading...

Share This Page