fstream problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • las

    fstream problem

    #include <iostream>
    #include <fstream>
    #include <stdlib.h>
    #include <string>

    int main()
    {
    string token1 = "Hello\nall ";
    fstream newfile("C:/temp/test1.txt", ios::trunc);
    if ( newfile.good() )
    {
    cout << token1.c_str() << endl << "Size of string is " <<
    token1.length() << endl;
    newfile.write( token1.c_str(), token1.length() );
    newfile.flush() ;
    }
    else
    {
    cout << "Error with newfile" << endl;
    }

    file.close();
    system("PAUSE") ;
    return 0;
    }

    Why doesnt the above code write the token1 string to the test1.txt file ?


  • Simon Saunders

    #2
    Re: fstream problem

    "las" <laasunde@onlin e.no> wrote in message
    news:AYaRa.6557 $os2.98895@news 2.e.nsc.no...[color=blue]
    > #include <iostream>
    > #include <fstream>
    > #include <stdlib.h>
    > #include <string>
    >
    > int main()
    > {
    > string token1 = "Hello\nall ";
    > fstream newfile("C:/temp/test1.txt", ios::trunc);
    > if ( newfile.good() )
    > {
    > cout << token1.c_str() << endl << "Size of string is " <<
    > token1.length() << endl;
    > newfile.write( token1.c_str(), token1.length() );
    > newfile.flush() ;
    > }
    > else
    > {
    > cout << "Error with newfile" << endl;
    > }
    >
    > file.close();
    > system("PAUSE") ;
    > return 0;
    > }
    >
    > Why doesnt the above code write the token1 string to the test1.txt file ?
    >
    >[/color]

    Try the following instead:

    fstream newfile("C:/temp/test1.txt", ios::out);

    or:

    ofstream newfile("C:/temp/test1.txt");



    Comment

    Working...