/* 实现向二进制文件输入正整数功能 */ #include <stdio.h> void fput_int(int data, FILE * fp) { char * s; int i; s = (char *) &data; for (i = 0; i < sizeof(int) && !feof(fp); ++i) { fputc(s[i], fp); } } int main(void) { FILE * fp = NULL; int i; fp = fopen("file.txt", "wb"); if (NULL == fp) { printf("Can't open file \"test.txt\""); return -1; } for (i = 0; i < 10; ++i) fput_int(i, fp); fclose(fp); fp = NULL; return 0; }