fstream tests

Discussion in 'C++' started by Ioannis Vranos, Jun 10, 2008.

  1. Hi, I am experimenting with fstream type, I have written the following code:

    #include <iostream>
    #include <fstream>
    #include <string>

    int main()
    {
    using namespace std;

    char s[200]= {};

    fstream file("test.txt");

    file<< "This is a test\n"<< 3*9<< "\nThis is the end\n";

    file>> s;

    if(!file)
    cerr<<"Malfunction!\n";

    cout<< s<< endl;

    file<< 123<< "\n";

    }


    The file test.txt already exists.


    The output of the program to the standard output is:

    john@john-desktop:~/Projects/foobar-cpp/src$ ./foobar-cpp
    Malfunction!

    john@john-desktop:~/Projects/foobar-cpp/src$


    The contents of test.txt are:

    john@john-desktop:~/Projects/foobar-cpp/src$ cat test.txt
    This is a test
    27
    This is the end
    john@john-desktop:~/Projects/foobar-cpp/src$



    Question:

    Why the file object does not output to the char array?
     
    Ioannis Vranos, Jun 10, 2008
    #1
    1. Advertising

  2. Ioannis Vranos

    Eric Pruneau Guest

    "Ioannis Vranos" <> a écrit dans le message
    de news: g2lrc8$128k$...
    > Hi, I am experimenting with fstream type, I have written the following
    > code:
    >
    > #include <iostream>
    > #include <fstream>
    > #include <string>
    >
    > int main()
    > {
    > using namespace std;
    >
    > char s[200]= {};
    >
    > fstream file("test.txt");


    I bet file.is_open() returns false after this... try
    fstream file("test.txt", fstream::trunc | fstream::in | fstream::eek:ut);

    >
    > file<< "This is a test\n"<< 3*9<< "\nThis is the end\n";


    this actually do nothing since your file is not really open, but let assume
    your file is now correctly open
    >
    > file>> s;


    problem here, check where your get pointer is.... Most likely that your
    program will crash!
    From now on if you want to do read/write operations your friends gonna be
    seekp, seekg functions inherited from ostream and istream

    >
    > if(!file)
    > cerr<<"Malfunction!\n";
    >
    > cout<< s<< endl;
    >
    > file<< 123<< "\n";
    >
    > }
    >
    >
    > The file test.txt already exists.


    .... and I bet it was not created by this program

    --------------------

    Eric Pruneau
     
    Eric Pruneau, Jun 11, 2008
    #2
    1. Advertising

  3. Ioannis Vranos

    James Kanze Guest

    On Jun 10, 2:17 pm, Ioannis Vranos <>
    wrote:
    > Hi, I am experimenting with fstream type, I have written the
    > following code:


    > #include <iostream>
    > #include <fstream>
    > #include <string>


    > int main()
    > {
    > using namespace std;


    > char s[200]= {};


    > fstream file("test.txt");


    You really should check to ensure that the open worked. (It
    won't if the file doesn't exist.)

    > file<< "This is a test\n"<< 3*9<< "\nThis is the end\n";


    > file>> s;


    And what should this read? You're positionned at the end of the
    file (unless the file previously existed, and contained more
    bytes than you've written). So the read should fail. In fact,
    the standard says that output shall not be directly followed by
    input unless there is an intervening flush or seek request, so
    this input should fail in all cases.

    > if(!file)
    > cerr<<"Malfunction!\n";


    > cout<< s<< endl;


    > file<< 123<< "\n";
    > }


    > The file test.txt already exists.


    > The output of the program to the standard output is:


    > john@john-desktop:~/Projects/foobar-cpp/src$ ./foobar-cpp
    > Malfunction!


    Which is what should be expected.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Jun 11, 2008
    #3
  4. fstream file("test.txt", ios_base::in | ios_base::eek:ut
    | ios_base::trunc);


    In the above code, is it guaranteed that with the use of
    "ios_base::trunc", a new file will be created under all circumstances in
    which

    ofstream file("test.txt", ios_base::eek:ut | ios_base::trunc);

    will create a new file (e.g. when test.txt does not pre-exist)?
     
    Ioannis Vranos, Jun 12, 2008
    #4
    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. Replies:
    8
    Views:
    919
  2. Marc Schellens

    vector of fstream

    Marc Schellens, Jul 15, 2003, in forum: C++
    Replies:
    2
    Views:
    407
    John Harrison
    Jul 16, 2003
  3. Marc Schellens

    fstream.open( name, mode) fails

    Marc Schellens, Jul 15, 2003, in forum: C++
    Replies:
    0
    Views:
    1,484
    Marc Schellens
    Jul 15, 2003
  4. Armando
    Replies:
    6
    Views:
    779
    Armando
    Jan 29, 2004
  5. dayo
    Replies:
    11
    Views:
    374
    Ilya Zakharevich
    Dec 16, 2005
Loading...

Share This Page