Hello everybody!
I am writing a program which compresses files into .zip files.
Here's my problem: Whenever I want to compress an executable file, my readFile function does not read the entire file. When I extract the .exe I get a very tiny and incomplete file.
You are also looping on EOF -- which is I hope you understand is typically the wrong thing to do -- even though it is correct in your code as it is.
There is no need for the MAX_FILEBUFFER argument, since you will be storing everything in a string to return. Better memory management is to allocate all the memory you need (the filesize) at the front, then just read into the string. (Which, despite older standard documentation, is safe in every extant implementation.)
std::string miniz_wrapper::readFile(FILE* f, int MAX_FILEBUFFER)
{
// given: f is opened with mode "rb"
std::string result;
longint size;
// Get the size of the necessary block of memory
fseek(f, 0, SEEK_END);
size = ftell(f);
if (size < 0)
throw error or return"" or something;
rewind(f);
// Load the file data into the string
result.resize(size);
if (fread((void*)result.data(), 1, size, f) != (size_t)size)
throw error or return"" or something;
return result;
}
I did not test the code. (I wrote it off the top of my head. JSYK.)
Sadly, neither code works. Both of your methods write exactly 2 characters.
However, @Duoas when I print the size after rewind(f); it prints 720 kb, which is the correct file size.
Dang! I forgot about that there streambuf_iterator! Nice!
(Unfortunately, it violates the OP's API requirement over a *FILE.)
@OP. I don't know what you mean by "neither code works" and "both of your methods write exactly 2 characters".
The code I wrote above doesn't write any characters at all. It reads them. If you have modified it, you might want to consider what your modifications have done to invalidate the code I wrote.