Topic : C++ Binary File I/O
Author : CS2604 class
Page : << Previous 2  
Go to page :


a position relative to the current put pointer location, or relative to the end of the file.  

Reading and Writing Complex Data
Although the read and write methods accept a char* pointer, there is no requirement that the data you read and/or write be held in a char array. You can read or write complex data objects using simple type casting of pointers:
    #include <fstream.h>
    ...
    class Data {
        int    key;
        double value;
    };
    
    Data x;
    Data *y = new Data[10];

    fstream myFile ("data.bin", ios::in | ios::out | ios::binary);
    myFile.seekp (location1);
    myFile.write ((char*)&x, sizeof (Data));
    ...
    myFile.seekg (0);
    myFile.read ((char*)y, sizeof (Data) * 10);


Closing a File
For all file stream objects, use:
    myFile.close();

Page : << Previous 2