Hey guys,

I've this code:

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;

bool load_file(const string &filepath, string &dest){
    ifstream file;
    file.open(filepath.c_str(), ios::binary);
    if(!file.good()){
        cerr << "Fatal error while opening file." << endl;
        return false;
    }

    //get filesize in bytes from ifstream
        //seek end, get pointer, rewind
    file.seekg(0, ios_base::end);
    size_t file_size = file.tellg();
    file.seekg(0, ios_base::beg);
    if(!file.good()){
        cerr << "Fatal error while getting filesize." << endl;
        return false;
    }

    //read file in string from ifstream
        //allocate array, read file in array, set string to array
    char *file_content = new char [file_size+1];
    file.read(file_content, file_size);
    cout << file_content;
    dest = file_content;

    //clean up
        //close file, free array
    file.close();
    delete[] file_content;

    return true;
}


int main(){
    string file_content;
    load_file("main.cpp", file_content);
    //cout << file_content;

    return 0;
}

And it should read itself, but the output is slightly different:

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;

bool load_file(const string &filepath, string &dest){
    ifstream file;
    file.open(filepath.c_str(), ios::binary);
    if(!file.good()){
        cerr << "Fatal error while opening file." << endl;
        return false;
    }

    //get filesize in bytes from ifstream
        //seek end, get pointer, rewind
    file.seekg(0, ios_base::end);
    size_t file_size = file.tellg();
    file.seekg(0, ios_base::beg);
    if(!file.good()){
        cerr << "Fatal error while getting filesize." << endl;
        return false;
    }

    //read file in string from ifstream
        //allocate array, read file in array, set string to array
    char *file_content = new char [file_size+1];
    file.read(file_content, file_size);
    cout << file_content;
    dest = file_content;

    //clean up
        //close file, free array
    file.close();
    delete[] file_content;

    return true;
}


int main(){
    string file_content;
    load_file("main.cpp", file_content);
    //cout << file_content;


    return 0;
}

L▬

See those last couple of bytes? I have no clue where they came from, you do?

Thanks in advance,
Nick

Recommended Answers

All 7 Replies

That's weird - it works perfect for me.

It's a memory error (from the looks of it), presumably reading over EOF or forgetting the terminating null byte in the char array. If it's working for you, try adjusting the file it's reading a bit (add a comment or something).

But, I don't see where I'm doing something wrong, hahaha. :(

One thing I spot is this:

char *file_content = new char [file_size+1];
file.read(file_content, file_size);

Why do you make your array one byte to big? That byte will never get a value. So when you display it, it just dumps out whatever value is on that memory address represented as a char.

Also: Why do this so difficult? If you just want to print out everything in the file use something like:

ifstream infile("filename.bla");
string str;
while (getline(infile,str)){
    cout << str << "\n";
}

Doesn't new set the allocated memory to 0, like C's calloc() ?

I meant: why use char *file_content = new char [file_size+1]; instead of char *file_content = new char [file_size]; I also edited my previous post to include a small snippet and a question.

I don't want to output the whole file, I want to have the whole file in memory.

And, I used that so I can use the char array as a null-terminated string. That was the point... The file is not guaranteed to end in a null byte, is it?

Say the file is 200 bytes, I need to allocate 201 bytes, 200 for the file, 1 for the terminating null byte.

Meh, it's simply the null byte. new doesn't set the whole array to 0 like calloc() does. Thanks. :)

Working code:

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>

/* load_file(filepath, destination)

   -Returns whether it succeed or not. True meaning succeeded.
   -Takes a path and writes to a std::string, completely memory safe.
*/

bool load_file(const char *filepath, std::string &dest){
    using namespace std;

    ifstream file;
    file.open(filepath, ios::binary);
    if(!file.good()){
        cerr << "Fatal error while opening file." << endl;
        return false;
    }

    //get filesize in bytes from ifstream
        //seek end, get pointer, rewind
    file.seekg(0, ios_base::end);
    size_t file_size = file.tellg();
    file.seekg(0, ios_base::beg);
    if(!file.good()){
        cerr << "Fatal error while getting filesize." << endl;
        return false;
    }

    //read file in string from ifstream
        //allocate array, read file in array, set the term. null byte, set string to array
    char *file_content = new char [file_size+1];
    file.read(file_content, file_size);
    file_content[file_size] = '\0';
    dest = file_content;

    //clean up
        //close file, free array
    file.close();
    delete[] file_content;

    return true;
}


int main(){
    using namespace std;

    string file_content;
    load_file("main.cpp", file_content);
    cout << file_content;

    return 0;
}

Feel free to grab the function and use it.

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.