fstream的使用

fstream的使用
#include <iostream>
#include <fstream>
using namespace std;

#ifdef WIN32
#define TEST_FILE   "c:\\tmp\\test.txt"
#else
#define TEST_FILE   "/tmp/test.txt"
#endif

void test ( )
{
    {
        //fstream sfs;
        //sfs.open(TEST_FILE, ios_base::out);
        fstream sfs (TEST_FILE, ios_base : :out );
       
        char buf [ ] = "1234567890";
        sfs.write (buf, sizeof (buf ) );

        sfs.close ( );
    }

    {
        int len;
        char * buf;

        //fstream sfs;
        //sfs.open(TEST_FILE);
        fstream sfs (TEST_FILE );
       
        sfs.seekg ( 0, ios : :end );
        len = sfs.tellg ( );
        sfs.seekg ( 0, ios : :beg );

        buf = new char [len ];
        sfs.read (buf, len );
        cout < < buf < < endl;
        delete [ ]buf;

        sfs.close ( );
    }
}

int main ( int argc, char * argv [ ] )
{  
    test ( );
    return 0;
}

你可能感兴趣的:(fstream的使用)