He made sure of the correctness of the recorded data by viewing them through a hex editor. So feel free to open the file for reading.
FILE* fp; fp = fopen("C:\\Users\\seven\\source\\repos\\NeCuda\\NeCuda\\input.bin", "rb"); if (fp == NULL) { fprintf(stderr, "Cannot open up file"); exit(EXIT_FAILURE); } I counted the number of bytes written to a binary file and divided by how much one byte it takes to find out the number of array elements.
while ((ch = getc(fp)) != EOF) counter++; counter /= sizeof(int); I allocated an appropriate amount of memory and checked that memory was really allocated.
temp = (int*)malloc(sizeof(int)*counter); if (temp == NULL) { fprintf(stderr, "Cannot give mem"); exit(EXIT_FAILURE); } Now I want to write in the newly created array the data from the file
int k=fread(temp, sizeof(int), counter, fp); But nothing is written, the pointer (as the debugger is shown) points to garbage (trailing). fread returned 0 and wrote this zero to k - it means that fread did not work exactly. How to make fread write data from a file to temp?
Followed the tips below:
fseek(fp, 0L, SEEK_END); counter=ftell(fp); temp = (int*)malloc(sizeof(int)*counter); if (temp == NULL) { fprintf(stderr, "Cannot give mem"); exit(EXIT_FAILURE); } fseek(fp, 0, SEEK_SET); int k=fread(temp, sizeof(int), counter, fp); show(temp); But still there is no result
In the debugger, I see that the pointer is: fp 0x000002c9f7afc840 {_Placeholder = 0x0000000000000000}
fseek()/ftell()- Fat-Zercounteryou now have the size in bytes; and you need - inint'ah. - Harry