input and output questions about file

Discussion in 'C Programming' 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. Lew Pitcher Guest

    On Feb 24, 10:13 am, "" <> wrote:
    > ÏÂÃæ³ÌÐò½¨Á¢²»ÁËÎļþ,²»ÖªµÀÄÄÀï³ö´íÁË?
    >
    > Ö»Ï붨ÒåÒ»¸öfstreamÀàÐÍÀ´Íê³ÉÊäÈëÊä³öµÄ¹¤×÷.
    > #include<iostream>
    > #include<conio.h>
    > #include<fstream>
    >
    > using namespace std;

    <snip>
    > 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?


    You go talk to the guys in comp.lang.c++
    Just down the hall, second door on the right (just after the koolade
    dispenser)

    --
    Lew
     
    Lew Pitcher, Feb 24, 2007
    #2
    1. Advertising

  3. Simon Biber Guest

    wrote:
    > ÏÂÃæ³ÌÐò½¨Á¢²»ÁËÎļþ,²»ÖªµÀÄÄÀï³ö´íÁË?
    >
    > Ö»Ï붨ÒåÒ»¸öfstreamÀàÐÍÀ´Íê³ÉÊäÈëÊä³öµÄ¹¤×÷.


    ÏÂÃæ³ÌÐòÓÖ²»ÊÇCÓïÑÔ£¬Äãµ½comp.lang.c++È¥°É¡£

    The program below is not even in the C language. I suggest you go to
    comp.lang.c++

    --
    Simon.
     
    Simon Biber, Feb 24, 2007
    #3
  4. Guest

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

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

    >
    > ÏÂÃæ³ÌÐòÓÖ²»ÊÇCÓïÑÔ£¬Äãµ½comp.lang.c++È¥°É¡£
    >
    > The program below is not even in the C language. I suggest you go to
    > comp.lang.c++
    >
    > --
    > Simon.


    I see.
     
    , Feb 24, 2007
    #4
  5. wrote:
    > 下é¢ç¨‹åºå»ºç«‹ä¸äº†æ–‡ä»¶,ä¸çŸ¥é“哪里出错了?
    >
    > åªæƒ³å®šä¹‰ä¸€ä¸ªfstream类型æ¥å®Œæˆè¾“入输出的工作.
    > #include<iostream>
    > #include<conio.h>
    > #include<fstream>


    <iostream> and <fstream> are C++ headers. They are not part of C;
    questions involving anything associated with those headers belong in
    <news:comp.lang.c++>, not <news:comp.lang.c>.

    <conio.h> is not a standard C or C++ header. Any questions involving
    anthing associated with that header should go to an
    implementation-specific newgroup, mailing-list, or technical support.
    They do not belong here (comp.lang.c) or in comp.lang.c++.
    >
    > using namespace std;


    That is a syntax error in C.
     
    Martin Ambuhl, Feb 24, 2007
    #5
  6. On 24 Feb 2007 07:13:08 -0800, in comp.lang.c , ""
    <> wrote:

    >using namespace std;


    comp.lang.c++ is down the hall, second left.
    --
    Mark McIntyre

    "Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are,
    by definition, not smart enough to debug it."
    --Brian Kernighan
     
    Mark McIntyre, Feb 24, 2007
    #6
  7. CBFalconer Guest

    "" wrote:
    >

    .... snip incomprehensible ...
    >
    > #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:


    Don't believe you.

    junk.c:1:19: iostream: No such file or directory (ENOENT)
    junk.c:2:18: conio.h: No such file or directory (ENOENT)
    junk.c:3:18: fstream: No such file or directory (ENOENT)
    junk.c:5: parse error before "namespace"
    junk.c:5: warning: type defaults to `int' in declaration of `std'
    junk.c:5: ISO C forbids data definition with no type or storage
    class
    junk.c: In function `main':
    junk.c:9: `fstream' undeclared (first use in this function)
    junk.c:9: (Each undeclared identifier is reported only once
    junk.c:9: for each function it appears in.)
    junk.c:9: parse error before "file"
    junk.c:10: `string' undeclared (first use in this function)
    junk.c:12: `file' undeclared (first use in this function)
    junk.c:12: `cerr' undeclared (first use in this function)
    junk.c:12: `endl' undeclared (first use in this function)
    junk.c:12: parse error before '/' token
    junk.c:17: `s1' undeclared (first use in this function)
    junk.c:17: parse error before '/' token
    junk.c:17:24: missing terminating ' character
    junk.c:17:24: warning: character constant too long
    junk.c:20: `s2' undeclared (first use in this function)
    junk.c:21: `cout' undeclared (first use in this function)
    junk.c:21: parse error before '/' token
    junk.c:25: warning: implicit declaration of function `getch'

    c != C++

    --
    Chuck F (cbfalconer at maineline dot net)
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net>
     
    CBFalconer, Feb 24, 2007
    #7
    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,875
    nish a
    Jan 29, 2010
  2. Krick
    Replies:
    1
    Views:
    1,464
    John C. Bollinger
    Aug 15, 2003
  3. Replies:
    5
    Views:
    397
  4. Jess
    Replies:
    10
    Views:
    577
  5. Replies:
    34
    Views:
    1,180
    James Kuyper
    May 29, 2009
Loading...

Share This Page