0
void compresszstd(const char *input,const char *output) {

//

  size_t const fBuff_size = (size_t)fileSize; //input file size
  void *const fBuff = malloc(fBuff_size); //input file buff

  size_t const cBuff_size = ZSTD_compressBound(fBuff_size); // compressed size
  void *const cBuff = malloc(cBuff_size);// compressed data

// compress input buff and get size of compressed file
  size_t const c_size = ZSTD_compress(cBuff, cBuff_size, fBuff, fBuff_size, 1);

  fwrite(&cBuff, 1, c_size, output);

  fprintf(stdout, "%u\n%u\n", (unsigned)fBuff_size, (unsigned)c_size);

}

I tried to use zstd compress. When I compress test.txt which contains,

LWpSxCFgUh BmmJnEHaYG urlMrzBDFW jkYpByALnm UVaOzGwWkO pUmAAglAnE AXDeSGPpFe IrIItGYEwC pOrhUHPsMh IdtdfAofXN eRZLTEirRt OQkrVFtqfY JEQFixzifR dwreUUZYVV EkrIobWWfQ oCfdaXrlPi WGopEbfHGy bSxtEulqWH OuualBJWHa XVsqhFTECO

I need to get the size of the file before compression and the size of the file after zstd compression. So the size of before and after should be 220 and 194, but I get 220 and 18. Does anyone know what is problem?

Jonah KIM
  • 23
  • 3
  • Welcome to Stack Overflow. Please read [the help pages](http://stackoverflow.com/help), take the SO [tour], read [ask], as well as [how to write the "perfect" question](https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/), especially its [checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). Also please learn how to create a [mre]. – Some programmer dude Jun 07 '23 at 18:08
  • 1
    Especially that last point, about creating a [mre], is very important. The code you show isn't the one you're building and running, so we can't know if any errors in it may be because you're leaving out thing, you have made mistakes in writing it here, or if it's the actual problem you're asking about. For example, there are some things in the shown code that make it not build, and there are other things that will lead to undefined behavior. But I don't know if the latter (the ones causing UB) might be what's causing you problem. – Some programmer dude Jun 07 '23 at 18:09
  • 1
    It's still not a proper [mre], so still not really reliable. But the problem is very likely this part: `fwrite(&cBuff, 1, c_size, output);`. Please take some time with [a good `fwrite` reference](https://en.cppreference.com/w/c/io/fwrite), your text-books or tutorials, and then please explain to yourself what the first argument `&cBuff` does. – Some programmer dude Jun 07 '23 at 19:26
  • 1
    I also recommend you take some time to read [the ZSTD documentation](http://facebook.github.io/zstd/zstd_manual.html). About `ZSTD_compress` is says that it returns "compressed size written into `dst` ... **or an error code if it fails (which can be tested using ZSTD_isError())**" (emphasis mine). You don't have *any* error checking. You need to have error checking. Not only for the `ZSTD_compress` call, but for *all* functions that returns pointers or error/success status (like e.g. `malloc` or `fwrite`). – Some programmer dude Jun 07 '23 at 19:31

0 Answers0