I wrote this piece of code :
<code>#include <stdio.h>
int main(void)
{
FILE * fp;
int num[100];
int number;
fp = fopen("myfile.bin", "wb");
for (number = 0; number < 100; ++number)
{
num[number] = number;
}
fwrite(num, 4, 100, fp);
fclose(fp);
return 0;
}
</code>
<code>#include <stdio.h>
int main(void)
{
FILE * fp;
int num[100];
int number;
fp = fopen("myfile.bin", "wb");
for (number = 0; number < 100; ++number)
{
num[number] = number;
}
fwrite(num, 4, 100, fp);
fclose(fp);
return 0;
}
</code>
#include <stdio.h>
int main(void)
{
FILE * fp;
int num[100];
int number;
fp = fopen("myfile.bin", "wb");
for (number = 0; number < 100; ++number)
{
num[number] = number;
}
fwrite(num, 4, 100, fp);
fclose(fp);
return 0;
}
And fwrite writes down an excessive byte by value 00 in the first byte of file . It’s like :
00 00 00 00 00 01
An int is only 4 bytes and the other bytes write normally but first one , not
What’s wrong here ? I tested by GCC and Clang and TCC and read the file with Bless and Ghex . Same results
I’m on Intel Pentium G3220 , Debian GNU/Linux 12.5 and using GCC 12.2.0-3