I wrote this basic function :
int save_files(PCHAR fileName)
{
errno_t err;
FILE* pFile = NULL;
do
{
if (!fileName)
{
printf("Input is NULL \n");
break;
}
err = fopen_s( &pFile, fileName, "r");
if(0 != err)
{
printf("The file %s was not opened for reading\n", fileName);
}
else
{
printf("The file %s was opened for reading \n", fileName);
}
/*getting the fileSize */
fileSize = dbg_getFileSize(pFile);
printf("############# FILE SIZE IS : %d #############\n" );
}
this is the function get file size :
UINT32 dbg_getFileSize(FILE *file)
{
UINT32 size = 0 ;
if (file == NULL)
{
return -1;
}
fseek(file , 0L , SEEK_END);
size = ftell(file);
fseek(file, 0L, SEEK_SET);/*set it to the head!!! */
return size;
}
I open the same path all the time , and get different size every time
I tried opening it with “r” and “rb” , but still getting the same different numbers..
You get different file sizes because the following line:
doesn’t actually specify the variable you’re trying to print. Hence it’s probably getting whatever rubbish is on the stack when you call it (I say probably, but anything could happen given that you’ve invoked the dreaded “undefined behaviour” (a)).
You might want to try this instead:
(a) From
C99 7.19.6.1 The fprintf function, unchanged inC11 7.20.6.1, the equivalent section: