user-0 提问: 如何将TXT文件里面的数据存入BIN文件？

回复:
user: user-1
content: 不要迷信书、考题、老师、回帖； 要迷信CPU、编译器、调试器、运行结果。 并请结合“盲人摸太阳”和“驾船出海时一定只带一个指南针。”加以理解。 任何理论、权威、传说、真理、标准、解释、想象、知识……都比不上摆在眼前的事实！

user: user-0
content: 引用 1 楼 zhao4zhong1 的回复:仅供参考：#include <stdio.h> #include <string.h> #include <conio.h> FILE *fi,*fo; unsigned int n; int c; //------------------------------------------------------- void main(int argc,char *argv[]) {     if (argc<3) {         cprintf("Bin2Hex srcfile desfile");         return;     }     if ((fi=fopen(argv[1],"rb"))==NULL) {         cprintf("Can not find file %s",argv[1]);         return;     }     if ((fo=fopen(argv[2],"w"))==NULL) {         fclose(fi);         cprintf("Can not create file %s",argv[2]);         return;     }     n=0;     while (1) {         c=fgetc(fi);         if (EOF==c) break;         n++;         if (1==n)        fprintf(fo,  "0x%02X",c);         else {             if (1==n%16) fprintf(fo,"\n0x%02X",c);             else         fprintf(fo, ",0x%02X",c);         }     }     fcloseall();     cprintf("OK to Bin2Hex %u bytes.",n); }  #include <stdio.h> #include <string.h> #include <conio.h> FILE *fi,*fo; int r; unsigned int n,c; long offset; //------------------------------------------------------- void main(int argc,char *argv[]) {     if (argc<3) {         cprintf("Hex2Bin srcfile desfile");         return;     }     if ((fi=fopen(argv[1],"rb"))==NULL) {         cprintf("Can not find file %s",argv[1]);         return;     }     if ((fo=fopen(argv[2],"wb"))==NULL) {         fclose(fi);         cprintf("Can not create file %s",argv[2]);         return;     }     n=0;     while (1) {         offset=ftell(fi);         r=fscanf(fi,"%2x",&c);         if (EOF==r) break;//         if (1==r) {             fputc(c,fo);             n++;         } else {//0==r             fseek(fi,offset,SEEK_SET);//因为VC6的Release版及其VC7,8,9,10,...版的fscanf未按约定保持offset不变，所以此处必须恢复offset             r=fgetc(fi);             if (EOF==r) break;//         }     }     fcloseall();     cprintf("OK to Hex2Bin %d bytes.",n); }  谢谢您赵老师！如果我增加TXT里面的数据，程序需要改吗？是不是这样写也可以啊？

user: user-1
content: 再供参考：#include <stdio.h> #include <stdlib.h> char s[]="00000000000000000000000000000001"          "00000000000000000000000000000010"          "00000000000000000000000000000011"; char *p; char *ep; char c; unsigned long v; FILE *f; void main() {     f=fopen("data.bin","wb");     p=s;     while (1) {         if (0==*p) break;         ep=p+32;         c=*ep;         *ep=0;         v=strtoul(p,&ep,2);         fwrite(&v,sizeof(v),1,f);         *ep=c;         p+=32;     }     fclose(f); }

user: user-1
content: 仅供参考：#include <stdio.h> #include <string.h> #include <conio.h> FILE *fi,*fo; unsigned int n; int c; //------------------------------------------------------- void main(int argc,char *argv[]) {     if (argc<3) {         cprintf("Bin2Hex srcfile desfile");         return;     }     if ((fi=fopen(argv[1],"rb"))==NULL) {         cprintf("Can not find file %s",argv[1]);         return;     }     if ((fo=fopen(argv[2],"w"))==NULL) {         fclose(fi);         cprintf("Can not create file %s",argv[2]);         return;     }     n=0;     while (1) {         c=fgetc(fi);         if (EOF==c) break;         n++;         if (1==n)        fprintf(fo,  "0x%02X",c);         else {             if (1==n%16) fprintf(fo,"\n0x%02X",c);             else         fprintf(fo, ",0x%02X",c);         }     }     fcloseall();     cprintf("OK to Bin2Hex %u bytes.",n); }  #include <stdio.h> #include <string.h> #include <conio.h> FILE *fi,*fo; int r; unsigned int n,c; long offset; //------------------------------------------------------- void main(int argc,char *argv[]) {     if (argc<3) {         cprintf("Hex2Bin srcfile desfile");         return;     }     if ((fi=fopen(argv[1],"rb"))==NULL) {         cprintf("Can not find file %s",argv[1]);         return;     }     if ((fo=fopen(argv[2],"wb"))==NULL) {         fclose(fi);         cprintf("Can not create file %s",argv[2]);         return;     }     n=0;     while (1) {         offset=ftell(fi);         r=fscanf(fi,"%2x",&c);         if (EOF==r) break;//         if (1==r) {             fputc(c,fo);             n++;         } else {//0==r             fseek(fi,offset,SEEK_SET);//因为VC6的Release版及其VC7,8,9,10,...版的fscanf未按约定保持offset不变，所以此处必须恢复offset             r=fgetc(fi);             if (EOF==r) break;//         }     }     fcloseall();     cprintf("OK to Hex2Bin %d bytes.",n); }