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

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: May 2008
Posts: 374
Reputation: Clockowl is on a distinguished road 
Solved Threads: 27
Clockowl's Avatar
Clockowl Clockowl is offline Offline
Posting Whiz

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

 
0
  #1
Apr 14th, 2009
Hey guys,

I've this code:
  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:

  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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 90
Reputation: unbeatable0 is an unknown quantity at this point 
Solved Threads: 12
unbeatable0 unbeatable0 is offline Offline
Junior Poster in Training

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

 
0
  #2
Apr 14th, 2009
That's weird - it works perfect for me.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 374
Reputation: Clockowl is on a distinguished road 
Solved Threads: 27
Clockowl's Avatar
Clockowl Clockowl is offline Offline
Posting Whiz

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

 
0
  #3
Apr 14th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,848
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 299
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is online now Online
Roasting Maven

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

 
0
  #4
Apr 14th, 2009
One thing I spot is this:
  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:
  1. ifstream infile("filename.bla");
  2. string str;
  3. while (getline(infile,str)){
  4. cout << str << "\n";
  5. }
Last edited by niek_e; Apr 14th, 2009 at 10:55 am.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 374
Reputation: Clockowl is on a distinguished road 
Solved Threads: 27
Clockowl's Avatar
Clockowl Clockowl is offline Offline
Posting Whiz

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

 
0
  #5
Apr 14th, 2009
Doesn't new set the allocated memory to 0, like C's calloc() ?
Last edited by Clockowl; Apr 14th, 2009 at 10:56 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,848
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 299
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is online now Online
Roasting Maven

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

 
0
  #6
Apr 14th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 374
Reputation: Clockowl is on a distinguished road 
Solved Threads: 27
Clockowl's Avatar
Clockowl Clockowl is offline Offline
Posting Whiz

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

 
0
  #7
Apr 14th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 374
Reputation: Clockowl is on a distinguished road 
Solved Threads: 27
Clockowl's Avatar
Clockowl Clockowl is offline Offline
Posting Whiz

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

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

Working code:

  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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC