I have used function write() in a loop to write 2 names in binary mode to a file. Now, when I try to read those 2 names I use this logic : 1) find size of file in bytes; 2) create buffer with that size; 1) read into buffer until end of file; 4) print out buffer.

void read_from_bin1(const char *file_name,int size){

    char *buffer = new char[size];
    ifstream ispis(file_name);

    while (!ispis.eof()){

        cout << "Loop" << endl;
        ispis.read(buffer, size);

    }

    cout << buffer << endl;

    delete [] buffer;
    buffer = nullptr;

}

The function stores only first name in buffer even though loop repats twice. I quess the problem is my lack of understanding this read() func. or there is something weird going on with this char*.

Every read overwrites the prior read. Try read and print. Does read() the method produce a null term string, or return a size somewhere?

http://www.cplusplus.com/reference/istream/istream/read/ inherited from
http://www.cplusplus.com/reference/fstream/ifstream/

No, no, so I would avoid it, or use the supporting calls that tell you what will be read. How were the names 'framed'? In binary, you make the protocol. Maybe no loop, just read that size chunk. Maybe verify that size is available. Otherwise, size is your caller's problem. Since you open every call, only good for reading first <size> bytes of the file.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.