| | |
Strange (extra) data in buffer after ifstream::read()
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
Hey guys,
I've this code:
And it should read itself, but the output is slightly different:
See those last couple of bytes? I have no clue where they came from, you do?
Thanks in advance,
Nick
I've this code:
cpp Syntax (Toggle Plain Text)
#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:
C++ Syntax (Toggle Plain Text)
#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
Last edited by Clockowl; Apr 14th, 2009 at 10:45 am.
One thing I spot is this:
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:
C++ Syntax (Toggle Plain Text)
char *file_content = new char [file_size+1]; file.read(file_content, file_size);
Also: Why do this so difficult? If you just want to print out everything in the file use something like:
C++ Syntax (Toggle Plain Text)
ifstream infile("filename.bla"); string str; while (getline(infile,str)){ cout << str << "\n"; }
Last edited by niek_e; Apr 14th, 2009 at 10:55 am.
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.
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.
Last edited by Clockowl; Apr 14th, 2009 at 11:06 am.
Meh, it's simply the null byte. new doesn't set the whole array to 0 like calloc() does. Thanks. 
Working code:
Feel free to grab the function and use it.

Working code:
cpp Syntax (Toggle Plain Text)
#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.
Last edited by Clockowl; Apr 14th, 2009 at 11:23 am.
![]() |
Other Threads in the C++ Forum
- Previous Thread: Searching Array
- Next Thread: Search Struct
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






