Idiotic conditions call for an idiotic solution:
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE * fp=fopen("temp.dat","w+b");
int number=12346;
int divisor=3;
char * buf = calloc(number,1);
fwrite(buf,number,1,fp);
rewind(fp);
int result=fread(buf,divisor,number,fp);
printf("%d / %d = %d", number, divisor, result);
free(buf);
fclose(fp);
return 0;
}
If also the decimal part is needed, just declare result
as double
and add to it the result of fmod(number,divisor)
.
Explanation of how it works
fwrite
writes number
bytes (number being 123456 in the example above).rewind
resets the file pointer to the front of the file.fread
reads a maximum of number
"records" that are divisor
in length from the file, and returns the number of elements it read.If you write 30 bytes then read back the file in units of 3, you get 10 "units". 30 / 3 = 10