A text file needs to be stored as a string in certain instances.

[codesyntax lang=”cpp”]

void fBufFrame::fileToString(string inFile, string & obuf)
    streampos size;
    char * memblock;
    ifstream file (inFile, ios::in|ios::binary|ios::ate);
    if (file.is_open())
        size = file.tellg();
        memblock = new char [size];
        file.seekg (0, ios::beg);
        file.read (memblock, size);
        obuf = memblock;
        delete[] memblock;
    else obuf = "";



This method determines the length of the file and dynamically allocates memory to store the file’s contents.  Then the contents of the file are stored in the string designated by obuf.  The memory allocated is freed.

Mr. Arch Brooks, Software Engineer, Brooks Computing Systems, LLC authored this article.

Leave a Reply

Your email address will not be published. Required fields are marked *