user-0 提问: 请教如何人把一个数字，比如25600打印到一个两个字节的二进制文件里？

回复:
user: user-1
content: #include <stdio.h> int main() {     char s[10];     char t[10];     FILE *f;     int a,b;      sprintf(s,"123");     sprintf(s+3,"ABC");     sscanf("25600","%hd",(short *)(s+6));     sscanf("-25600","%hd",(short *)(s+8));     f=fopen("d.bin","wb");     fwrite(s,1,10,f);     fclose(f);     f=fopen("d.bin","rb");     fread(t,1,10,f);     fclose(f);     printf("%.3s\n",t);     printf("%.3s\n",t+3);     printf("%02X%02X %hd\n",(unsigned char)t[6],(unsigned char)t[7],*(short *)(t+6));     printf("%02X%02X %hd\n",(unsigned char)t[8],(unsigned char)t[9],*(short *)(t+8));     sscanf("1 2.345 6","%d%*f%d",&a,&b);     printf("a=%d,b=%d\n",a,b);     return 0; } //123 //ABC //0064 25600 //009C -25600 //a=1,b=6 //

user: user-0
content: 百度的关于sscanf的实在没法看明白  http://baike.baidu.com/link?url=JOc35yv69O2BVR1aNGOOKGdkIaDGISUWYbbeoZZ_NesTNqf5UiZ5b4HTWQcFufTAH3iB_KZJ5Y1_0LAAHNjCba 比如这句话搞不清楚啥意思 6、特别的：%*[width] [{h | l | I64 | L}]type 表示满足该条件的被过滤掉，不会向目标参数中写入值

user: user-0
content: 引用 7 楼 zhao4zhong1 的回复://有字符串如下，其中123是三个字符(不是数值，各占一个字节),ABC是三个字符, //25600是一个数值(占2个字节)，-25600也是一个数值(占2个字节) //我想mempy到一块内存，然后一次性fwrite到二进制文件，总共10个字节，遵循小端CPU原则 #include <stdio.h> int main() {     char s[10];     FILE *f;      sprintf(s,"123");     sprintf(s+3,"ABC");     sscanf("25600","%hd",(short *)(s+6));     sscanf("-25600","%hd",(short *)(s+8));     f=fopen("d.bin","wb");     fwrite(s,1,10,f);     fclose(f);     return 0; }  谢谢赵老师 如果我要打印成4个字节呢？ 这样 ,(int *)(s+6)); 还是 ,(long int *)(s+6)); ？ int型可能是16位也可能是32位,这样不是会有问题吗？

user: user-1
content: //有字符串如下，其中123是三个字符(不是数值，各占一个字节),ABC是三个字符, //25600是一个数值(占2个字节)，-25600也是一个数值(占2个字节) //我想mempy到一块内存，然后一次性fwrite到二进制文件，总共10个字节，遵循小端CPU原则 #include <stdio.h> int main() {     char s[10];     FILE *f;      sprintf(s,"123");     sprintf(s+3,"ABC");     sscanf("25600","%hd",(short *)(s+6));     sscanf("-25600","%hd",(short *)(s+8));     f=fopen("d.bin","wb");     fwrite(s,1,10,f);     fclose(f);     return 0; }

user: user-1
content: //有字符串如下，其中123是三个字符(不是数值，各占一个字节),ABC是三个字符, //25600是一个数值(占2个字节)，-25600也是一个数值(占2个字节) //我想mempy到一块内存，然后一次性fwrite到二进制文件，总共10个字节，遵循小端CPU原则 #include <stdio.h> int main() {     char s[]="123ABC\x00\x64\x00\x9c";     FILE *f;      f=fopen("d.bin","wb");     fwrite(s,1,10,f);     fclose(f);     return 0; }

user: user-0
content: 引用 3 楼 zhao4zhong1 的回复:#include <stdio.h> int main() {     char s[]="25600-25600";     short int a,b;     FILE *f;     sscanf(s,"%hd%hd",&a,&b);     printf("%hd,%hd",a,b);//25600,-25600     f=fopen("a.bin","wb");     fwrite(&a,2,1,f);     fclose(f);     f=fopen("b.bin","wb");     fwrite(&b,2,1,f);     fclose(f);     return 0; }  谢谢赵老师 有字符串如下，其中123是三个字符(不是数值，各占一个字节),ABC是三个字符, 25600是一个数值(占2个字节)，-25600也是一个数值(占2个字节) 我想mempy到一块内存，然后一次性fwrite到二进制文件，总共10个字节，遵循小端CPU原则 "123ABC25600-25600"

user: user-1
content: #include <stdio.h> int main() {     char s[]="25600-25600";     short int *p;     char c[4];     FILE *f;      p=(short int *)c;     sscanf(s,"%hd%hd",p,p+1);     printf("%hd,%hd",p[0],p[1]);//25600,-25600     f=fopen("c.bin","wb");     fwrite(c,2,2,f);     fclose(f);     return 0; }

user: user-1
content: #include <stdio.h> int main() {     char s[]="25600-25600";     short int a,b;     FILE *f;     sscanf(s,"%hd%hd",&a,&b);     printf("%hd,%hd",a,b);//25600,-25600     f=fopen("a.bin","wb");     fwrite(&a,2,1,f);     fclose(f);     f=fopen("b.bin","wb");     fwrite(&b,2,1,f);     fclose(f);     return 0; }

user: user-0
content: 引用 1 楼 zhao4zhong1 的回复:#include <stdio.h> int main() {  short int a=25600,b=-25600;  FILE *f;  f=fopen("a.bin","wb");  fwrite(&a,2,1,f);  fclose(f);  f=fopen("b.bin","wb");  fwrite(&b,2,1,f);  fclose(f);  return 0; } 谢谢，如果是这样的自负的字符串呢？25600-25600,我想再内存里先生成二进制4个字节，然后一次性fwrite到文件

user: user-1
content: #include <stdio.h> int main() {  short int a=25600,b=-25600;  FILE *f;  f=fopen("a.bin","wb");  fwrite(&a,2,1,f);  fclose(f);  f=fopen("b.bin","wb");  fwrite(&b,2,1,f);  fclose(f);  return 0; }