This program is named CopyFile.cpp, which copies files using text I/O. I need to revise the following program so that it copies files using binary I/O.
Well the first thing you need to know is that strings will mess with your binary data. So if you're going to store file data in ram, use a vector<char>
second: to output you can simply use the stream operator <<
get/put work just fine for binary file copying. Just open both files in binary mode and you're fine.
Also there's no advantage to using vector<char> for strings. std::string pretty much is a vector<char> but with other things to make it more intuitive/easier to use for strings.
@disch, except for a stupid little swap of return chars. Maybe it was my fault but every binary file I read into a string and passed through my sockets ended up with an extra couple bytes, or messed up code... With a vector<char> everything worked perfectly, and more swiftly for that matter.
edit: oh and yes, It was in ios::binary, probably still my fault
Could you both show me how your ideas look in code? I would like to know the best way to do this, you know, learn it right the first time rather than try to re-learn it later...
that code is significantly different than the code I originally had. The code you did would be the input and output streams of my code I am trying to convert, correct?
@ultifinitus thanks for your help. I think my curiosity gets the best of me some times. I meant know offense by asking Disch his opinion. I am just trying to see all the different ways of doing things and decide for myself what I like and is most efficient.
No worries mate, "his" way isn't any different from "my" way, it's a simple swap of vector<char> to string, and the write to put. You could puzzle it out if you really wanted to.
I don't really get it. If the OP original is to do a binary file copy then just use an array of bytes or int or whatever as the temporary buffer from source and copy to destination is enough. Why need vector<string> or other data structure to incur un-necessary cost ? Unless besides just blind binary copy the OP intent to do some processing on the read-in data ? Hmmm....