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