Can someone please explain to me why the rand() function wont return values in %10. It also doesn't look like the for loop is running the amount of times I want. I want to generate 1000 random numbers and write them to the file, but the print statement outputs around 300 integers.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#define ELEMENTS 1000
main()
{
srand(time(NULL));
FILE *fp1;
int num, buffer;
fp1 = fopen("myFile.bin","wb");
for(int i=0; i<ELEMENTS; i++)
{
num = rand()%1000;
fwrite(&num, sizeof(int), 1, fp1);
}//end for
fclose(fp1);
fp1 = fopen("myFile.bin", "rb");
while(feof(fp1)==0)
{
fread(&buffer, sizeof(int), 1, fp1);
printf("\n%d\n",buffer);
}//end for
fclose(fp1);
}//end main
Aucun commentaire:
Enregistrer un commentaire