943,796 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1540
  • C++ RSS
Apr 14th, 2009
0

Strange (extra) data in buffer after ifstream::read()

Expand Post »
Hey guys,

I've this code:
cpp Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <fstream>
  3. #include <sstream>
  4. #include <string>
  5. using namespace std;
  6.  
  7. bool load_file(const string &filepath, string &dest){
  8. ifstream file;
  9. file.open(filepath.c_str(), ios::binary);
  10. if(!file.good()){
  11. cerr << "Fatal error while opening file." << endl;
  12. return false;
  13. }
  14.  
  15. //get filesize in bytes from ifstream
  16. //seek end, get pointer, rewind
  17. file.seekg(0, ios_base::end);
  18. size_t file_size = file.tellg();
  19. file.seekg(0, ios_base::beg);
  20. if(!file.good()){
  21. cerr << "Fatal error while getting filesize." << endl;
  22. return false;
  23. }
  24.  
  25. //read file in string from ifstream
  26. //allocate array, read file in array, set string to array
  27. char *file_content = new char [file_size+1];
  28. file.read(file_content, file_size);
  29. cout << file_content;
  30. dest = file_content;
  31.  
  32. //clean up
  33. //close file, free array
  34. file.close();
  35. delete[] file_content;
  36.  
  37. return true;
  38. }
  39.  
  40.  
  41. int main(){
  42. string file_content;
  43. load_file("main.cpp", file_content);
  44. //cout << file_content;
  45.  
  46. return 0;
  47. }

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

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <fstream>
  3. #include <sstream>
  4. #include <string>
  5. using namespace std;
  6.  
  7. bool load_file(const string &filepath, string &dest){
  8. ifstream file;
  9. file.open(filepath.c_str(), ios::binary);
  10. if(!file.good()){
  11. cerr << "Fatal error while opening file." << endl;
  12. return false;
  13. }
  14.  
  15. //get filesize in bytes from ifstream
  16. //seek end, get pointer, rewind
  17. file.seekg(0, ios_base::end);
  18. size_t file_size = file.tellg();
  19. file.seekg(0, ios_base::beg);
  20. if(!file.good()){
  21. cerr << "Fatal error while getting filesize." << endl;
  22. return false;
  23. }
  24.  
  25. //read file in string from ifstream
  26. //allocate array, read file in array, set string to array
  27. char *file_content = new char [file_size+1];
  28. file.read(file_content, file_size);
  29. cout << file_content;
  30. dest = file_content;
  31.  
  32. //clean up
  33. //close file, free array
  34. file.close();
  35. delete[] file_content;
  36.  
  37. return true;
  38. }
  39.  
  40.  
  41. int main(){
  42. string file_content;
  43. load_file("main.cpp", file_content);
  44. //cout << file_content;
  45.  
  46.  
  47. return 0;
  48. }
  49.  
  50. 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.
Reputation Points: 69
Solved Threads: 28
Posting Whiz
Clockowl is offline Offline
376 posts
since May 2008
Apr 14th, 2009
0

Re: Strange (extra) data in buffer after ifstream::read()

That's weird - it works perfect for me.
Reputation Points: 42
Solved Threads: 13
Junior Poster in Training
unbeatable0 is offline Offline
90 posts
since Sep 2008
Apr 14th, 2009
0

Re: Strange (extra) data in buffer after ifstream::read()

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.
Reputation Points: 69
Solved Threads: 28
Posting Whiz
Clockowl is offline Offline
376 posts
since May 2008
Apr 14th, 2009
0

Re: Strange (extra) data in buffer after ifstream::read()

One thing I spot is this:
C++ Syntax (Toggle Plain Text)
  1. char *file_content = new char [file_size+1];
  2. 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:
C++ Syntax (Toggle Plain Text)
  1. ifstream infile("filename.bla");
  2. string str;
  3. while (getline(infile,str)){
  4. cout << str << "\n";
  5. }
Last edited by Nick Evan; Apr 14th, 2009 at 10:55 am.
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
Apr 14th, 2009
0

Re: Strange (extra) data in buffer after ifstream::read()

Doesn't new set the allocated memory to 0, like C's calloc() ?
Last edited by Clockowl; Apr 14th, 2009 at 10:56 am.
Reputation Points: 69
Solved Threads: 28
Posting Whiz
Clockowl is offline Offline
376 posts
since May 2008
Apr 14th, 2009
0

Re: Strange (extra) data in buffer after ifstream::read()

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.
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
Apr 14th, 2009
0

Re: Strange (extra) data in buffer after ifstream::read()

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.
Last edited by Clockowl; Apr 14th, 2009 at 11:06 am.
Reputation Points: 69
Solved Threads: 28
Posting Whiz
Clockowl is offline Offline
376 posts
since May 2008
Apr 14th, 2009
0

Re: Strange (extra) data in buffer after ifstream::read()

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

Working code:

cpp Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <fstream>
  3. #include <sstream>
  4. #include <string>
  5.  
  6. /* load_file(filepath, destination)
  7.  
  8.   -Returns whether it succeed or not. True meaning succeeded.
  9.   -Takes a path and writes to a std::string, completely memory safe.
  10. */
  11.  
  12. bool load_file(const char *filepath, std::string &dest){
  13. using namespace std;
  14.  
  15. ifstream file;
  16. file.open(filepath, ios::binary);
  17. if(!file.good()){
  18. cerr << "Fatal error while opening file." << endl;
  19. return false;
  20. }
  21.  
  22. //get filesize in bytes from ifstream
  23. //seek end, get pointer, rewind
  24. file.seekg(0, ios_base::end);
  25. size_t file_size = file.tellg();
  26. file.seekg(0, ios_base::beg);
  27. if(!file.good()){
  28. cerr << "Fatal error while getting filesize." << endl;
  29. return false;
  30. }
  31.  
  32. //read file in string from ifstream
  33. //allocate array, read file in array, set the term. null byte, set string to array
  34. char *file_content = new char [file_size+1];
  35. file.read(file_content, file_size);
  36. file_content[file_size] = '\0';
  37. dest = file_content;
  38.  
  39. //clean up
  40. //close file, free array
  41. file.close();
  42. delete[] file_content;
  43.  
  44. return true;
  45. }
  46.  
  47.  
  48. int main(){
  49. using namespace std;
  50.  
  51. string file_content;
  52. load_file("main.cpp", file_content);
  53. cout << file_content;
  54.  
  55. return 0;
  56. }

Feel free to grab the function and use it.
Last edited by Clockowl; Apr 14th, 2009 at 11:23 am.
Reputation Points: 69
Solved Threads: 28
Posting Whiz
Clockowl is offline Offline
376 posts
since May 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Searching Array
Next Thread in C++ Forum Timeline: Search Struct





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC