C++ fread() into a std::string

19,128

Solution 1

Set the string to be large enough first to avoid buffer overrun, and access the byte array as &mystring[0] to satisfy const and other requirements of std::string.

FILE* myfile = fopen("myfile.bin", "rb");
if (myfile != NULL) {
    short stringlength = 6;
    string mystring( stringlength, '\0' );
    fseek(myfile , 0, SEEK_SET);
    fread(&mystring[0], sizeof(char), (size_t)stringlength, myfile);
    cout << mystring;
    fclose(myfile );
}

There are many, many issues in this code but that is a minimal adjustment to properly use std::string.

Solution 2

I would recommend this as the best way to do such a thing. Also you should check to make sure that all the bytes were read.

    FILE* sFile = fopen(this->file.c_str(), "r");

    // if unable to open file
    if (sFile == nullptr)
    {
        return false;
    }

    // seek to end of file
    fseek(sFile, 0, SEEK_END);

    // get current file position which is end from seek
    size_t size = ftell(sFile);

    std::string ss;

    // allocate string space and set length
    ss.resize(size);

    // go back to beginning of file for read
    rewind(sFile);

    // read 1*size bytes from sfile into ss
    fread(&ss[0], 1, size, sFile);

    // close the file
    fclose(sFile);

Solution 3

string::c_str() returns const char* which you can not modify.

One way to do this would be use a char* first and construct a string from it.

Example

char buffer = malloc(stringlength * sizeof(char));
fread(buffer, sizeof(char), (size_t)stringlength, myfile);
string mystring(buffer);
free(buffer);

But then again, if you want a string, you should perhaps ask yourself Why am I using fopen and fread in the first place??

fstream would be a much much better option. You can read more about it here

Solution 4

Please check out the following regarding c_str to see some things that are wrong with your program. A few issues include the c_str not being modifiable, but also that it returns a pointer to your string contents, but you never initialized the string.

http://www.cplusplus.com/reference/string/string/c_str/

As for resolving it... you could try reading into a char* and then initializing your string from that.

Solution 5

No it is not. std::string::c_str() method does not return a modifiable character sequence as you can validate from here. A better solution would be using a buffer char array. Here is an example:

FILE* myfile = fopen("myfile.bin", "rb");
    if (myfile != NULL) {
        char buffer[7]; //Or you can use malloc() / new instead.  
        short stringlength = 6;
        fseek(myfile , 0, SEEK_SET);
        fread(buffer, sizeof(char), (size_t)stringlength, myfile);
        string mystring(buffer);
        cout << mystring;
        fclose(myfile );
        //use free() or delete if buffer is allocated dynamically
}
Share:
19,128
ali
Author by

ali

I am a web, desktop and mobile applications developers with great knowledge in web development, medium knowledge in desktop applications development and a beginner status in mobile applications development.

Updated on June 04, 2022

Comments

  • ali
    ali almost 2 years

    Like always, problems with pointers. This time I am trying to read a file (opened in binary mode) and store some portion of it in a std::string object. Let's see:

    FILE* myfile = fopen("myfile.bin", "rb");
    if (myfile != NULL) {
        short stringlength = 6;
        string mystring;
        fseek(myfile , 0, SEEK_SET);
        fread((char*)mystring.c_str(), sizeof(char), (size_t)stringlength, myfile);
        cout << mystring;
        fclose(myfile );
    }
    

    Is this possible? I don't get any message. I am sure the file is O.K. When I try with char* it does work but I want to store it directly into the string. Thanks for your help!