You're opening the file the first time in text mode and the second time in binary mode. You need to open it both times in binary mode. If it's not in binary mode, the first control-z (hex value 1A) signals the "end of file".
Add the following includes (getting rid of <io.h>
):
#include <unistd.h>
#include <fcntl.h>
The call open like this:
int id = open("spiderman.torrent", O_RDONLY|O_BINARY);
Here's an example of control-z ending the file:
#include <stdio.h>
void writeit() {
FILE *f = fopen("test.txt", "wb");
fprintf(f, "hello world\r\n");
fputc(0x1A, f);
fprintf(f, "goodbye universe\r\n");
fclose(f);
}
void readit() {
int c;
FILE *f = fopen("test.txt", "r");
while ((c = fgetc(f)) != EOF)
putchar(c);
fclose(f);
}
int main() {
writeit();
readit();
return 0;
}
The above only prints "hello world" and not "goodbye universe".