1

I'm trying to duplicate files by using an ifstream to read the file and ofstream to write back out, but having some problems. Here's what I have so far:

std::ifstream File(_str.c_str(), std::ios::binary);

std::string strFile;
strFile.assign( (std::istreambuf_iterator<char>(File) ),
        (std::istreambuf_iterator<char>() )   );

File.close();

return strFile;

What's happening is some values of the string are null, and the string is terminating right there. For example if it was

1 'h'
2 'i'
3 0
4 'h'
5 'i'

it would return simply "hi"

I have tried various for loops and ways of getting around this, for example replacing the 0's with spaces, and replacing the 0's with "-NULL-" then on the user side replacing "-NULL-" back with 0 again, but I can't seem to add 0 to the string.

Could anyone give me some tips for what I could do to get around this problem? What I'm trying to do is transfer a .png via UDP, which is why I'm chucking the file into a string

Cheers!

Matt
  • 175
  • 1
  • 12
  • It sounds to me like you're using the wrong data structure. you should be using a vector, ideally chunked iteratively through whatever your packet size is going to be (I'm assuming there is overhead at the front to identify frame ids etc). – WhozCraig Dec 17 '13 at 07:18
  • Thanks for your suggestions guys, but none of these ways solve my problem. Both of these ways also terminate the string when it reaches a 0, which results in an uncompleted string or uncompleted file. Thanks for the suggestion of chunking up the data, however I am simply trying to fix the issue of the prematurely terminating string before I move on! – Matt Dec 17 '13 at 07:40

0 Answers0