c语言的细节太多了。。。。。
这几天在写一个从文件中读取数据的程序时遇到了问题。
呵呵呵,查询良久终于解决了。
我这个程序是先向文件div.in中写入两个数字,用空格分隔,然后从文件中
再读出来,并输出。这里我用的是unsigned short型变量
现在我写入文件的是 12 45
问题是写文件成功了,但是读文件时只能读出来第二个数字45,第一个数字读出来始终是0
不知这是什么原因。。。。(我把unsigned short 该为int型可以正常读出文件,但是我这里有条件必须要用unsigned short)
#include
#include
#define FALSE 0
#define TRUE 1
//读文件函数
int readFile(unsigned short *num1, unsigned short *num2);
int main(void)
{
unsigned short a, b;
readFile(&a,&b); //读文件
printf("\n%d,%d\n",a,b); //打印从文件中读出的数据
getch();
return 0;
}
int readFile(unsigned short *num1, unsigned short *num2)
{
FILE *fp;
unsigned short a =12, b=45;
if((fp = fopen("div.in","w")) == NULL) { //向文件中写入内容12 45
printf("error write!");
getch();
return FALSE;
} else {
fprintf(fp,"%d %d",a,b); //向文件中写入a和b
printf("write sucessed!\n");
fclose(fp);
}
if((fp = fopen("div.in", "r")) == NULL) { //从文件中读取内容
printf("open file error!");
getch();
return FALSE;
} else {
fscanf(fp, "%d %d\n", num1, num2); //从文件中读取数据,并存放在num1和num2中
fclose(fp); //num1和num2是从主函数中传入的指针型变量
printf("\n%d,%d",*num1,*num2); //输出读取的数据
return TRUE;
}
}
再读出来,并输出。这里我用的是unsigned short型变量
现在我写入文件的是 12 45
问题是写文件成功了,但是读文件时只能读出来第二个数字45,第一个数字读出来始终是0
不知这是什么原因。。。。(我把unsigned short 该为int型可以正常读出文件,但是我这里有条件必须要用unsigned short)
#include
#include
#define TRUE 1
//读文件函数
int readFile(unsigned short *num1, unsigned short *num2);
int main(void)
{
unsigned short a, b;
readFile(&a,&b); //读文件
printf("\n%d,%d\n",a,b); //打印从文件中读出的数据
getch();
return 0;
}
int readFile(unsigned short *num1, unsigned short *num2)
{
FILE *fp;
unsigned short a =12, b=45;
if((fp = fopen("div.in","w")) == NULL) { //向文件中写入内容12 45
printf("error write!");
getch();
return FALSE;
} else {
fprintf(fp,"%d %d",a,b); //向文件中写入a和b
printf("write sucessed!\n");
fclose(fp);
}
if((fp = fopen("div.in", "r")) == NULL) { //从文件中读取内容
printf("open file error!");
getch();
return FALSE;
} else {
fscanf(fp, "%d %d\n", num1, num2); //从文件中读取数据,并存放在num1和num2中
fclose(fp); //num1和num2是从主函数中传入的指针型变量
printf("\n%d,%d",*num1,*num2); //输出读取的数据
return TRUE;
}
}
之前感觉这不可思议啊!明明都没错啊,呵呵后来终于发现问题出在格式控制符上了。unsigned short 对应的应该是%hu 。之前我自以为,这里应该是%ud,这有点定式思维了。所以只需将程序中红色字体部分的%d改为%hu,一切皆可搞定!!! 这可是让我烦恼了好几天啊!!
下面给出一张表这个表要好好看啊.....
| 关键字 | 位长 | 范围 | printf chars |
|---|---|---|---|
char |
1 | -128..127(或0..255,与体系结构相关) | %c |
unsigned char |
1 | 0..255 | |
signed char |
1 | -128..127 | |
int |
2 or 4 |
-32768..32767 or -2147483648..2147483647 |
%i, %d |
unsigned int |
2 or 4 |
0..65535 or 0..4294967295 |
%u |
signed int |
2 or 4 |
-32768..32767 or -2147483648..2147483647 |
%i, %d |
short int |
2 | -32768..32767 | %hi |
unsigned short |
2 | 0..65535 | %hu |
signed short |
2 | -32768..32767 | |
long int |
4 | -2147483648..2147483647 | %li, %ld |
unsigned long |
4 | 0..4294967295 | %lu |
signed long |
4 | -2147483648..2147483647 | |
long long |
8 | -9223372036854775808..9223372036854775807 | %lli |
unsigned long long |
8 | 0..18446744073709551615 | %llu |
float |
4 | 3.4x10-38..3.4x10+38 (7 sf) | %f, %e, %g |
double |
8 | 1.7x10-308..1.7x10+308 (15 sf) | %f, %e, %g |
long double |
8 或以上 | 编译器相关 | %Lf, %Le, %Lg |