[c] How to calculate size of the file from size of the buffer?

Hi,

Can I find size of the file from size of the buffer written?

nbECRITS = fwrite(strstr(data->buffer, ";") + 1, sizeof(char), (data->buffsize) - LEN_NOM_FIC, fic_sortie);

Thank You :slight_smile:

It is not guaranteed to work. Use stat() to get a file size.

#include <sys/stat.h>
size_t filesize(int fd)
{
     struct stat st;
     if(fstat(fd, &st) == -1 )
     {
          perror("cannot stat file");
          exit(1);
      }
     return st.st_size;
}

void myfoo(void)
{
    size_t bytes;
    //.......... your code here
     bytes=filesize(fileno(fic_sortie));
    //.......  more code here
}
 

try something like that.