fork 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. fseek(fp,0,SEEK_END);
  28. int counter = ftell(fp)/sizeof(int);
  29.  
  30. int * temp = (int*)malloc(sizeof(int)*counter);
  31. if (temp == NULL)
  32. {
  33. fprintf(stderr, "Cannot give mem");
  34. exit(EXIT_FAILURE);
  35. }
  36. fseek(fp,0,SEEK_SET);
  37. int k=fread(temp, sizeof(int), counter, fp);
  38.  
  39. printf("k = %d\n",k);
  40. printf("temp[20] = %d\n",temp[20]);
  41. }
  42.  
Success #stdin #stdout 0s 9424KB
stdin
Standard input is empty
stdout
k = 100
temp[20] = 20