/* 实现文件读取正整数功能 */ #include <stdio.h> int fget_int(FILE * fp) { char * s; int i, tmp; s = (char *) &tmp; for (i = 0; i < sizeof(int); ++i) s[i] = fgetc(fp); return tmp; } int main(void) { FILE * fp = NULL; int data; fp = fopen("file.txt", "rb"); if (NULL == fp) { printf("Can't open file \"test.txt\""); return -1; } data = fget_int(fp); while (!feof(fp)) { printf("%d", data); data = fget_int(fp); } fclose(fp); fp = NULL; return 0; }