fork(1) download
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. const char * filename = "/tmp/test.bin";
  6.  
  7. int main()
  8. {
  9.  
  10. {
  11. FILE * out = fopen(filename,"wb");
  12. for(int i = 0; i < 100; ++i)
  13. {
  14. fwrite(&i,sizeof(int),1,out);
  15. }
  16. fclose(out);
  17. }
  18.  
  19. FILE* fp;
  20. fp = fopen(filename, "rb");
  21. if (fp == NULL)
  22. {
  23. fprintf(stderr, "Cannot open up file");
  24. exit(EXIT_FAILURE);
  25. }
  26. char ch;
  27. int counter = 0;
  28. while ((ch = getc(fp)) != EOF)
  29. counter++;
  30. counter /= sizeof(int);
  31. int * temp = (int*)malloc(sizeof(int)*counter);
  32. if (temp == NULL)
  33. {
  34. fprintf(stderr, "Cannot give mem");
  35. exit(EXIT_FAILURE);
  36. }
  37. fseek(fp,0,SEEK_SET);
  38. int k=fread(temp, sizeof(int), counter, fp);
  39.  
  40. printf("k = %d\n",k);
  41. printf("temp[20] = %d\n",temp[20]);
  42. }
  43.  
Success #stdin #stdout 0s 9424KB
stdin
Standard input is empty
stdout
k = 100
temp[20] = 20