为什么 putw on 使用 getw 显示正确的输出,而 fprintf 和 printf 仅使用 scanf 显示正确的输出?
why putw on shows correct output with getw, and fprintf and printf only shows correct output with scanf?
谁能给我解释一下这里到底发生了什么?
我的文件管理有问题。
代码说明:
在这里,它从用户那里获取整数,然后存储在 DATA 文件中。
然后它从 DATA 文件中读取整数并通过 ODD 和 EVEN 过滤它们并将其存储在相应的文件中。
这是我的代码。
#include<stdio.h>
int main()
{
FILE *f1, *f2, *f3;
int number;
printf("Enter the content of data file\n");
f1 = fopen("DATA","w");
for(int i=1;i<=30;i++)
{
/*problem is here*/
number=getw(stdin);
//scanf("%d",&number);
if(number==-1 || number==EOF) break;
putw(number,f1);
}
fclose(f1);
f1 = fopen("DATA","r");
f2 = fopen("ODD","w");
f3 = fopen("EVEN","w");
while((number = getw(f1))!=EOF)
{
if(number % 2 == 0)
putw(number, f3);
else
putw(number,f2);
}
fclose(f1);
fclose(f2);
fclose(f3);
f2 = fopen("ODD","r");
f3 = fopen("EVEN","r");
printf("\n\ncontents of ODD file\n");
while((number=getw(f2))!=EOF)
/* problem is here */
putw(number,stdout);
//fprintf(stdout,"%d\n",number);
//printf("%d",number);
printf("\n\ncontents of EVEN file\n");
while((number=getw(f3))!=EOF)
/*problem is here */
//putw(number,stdout);
fprintf(stdout,"%d\n",number);
//printf("%d",number);
fclose(f2);
fclose(f3);
printf("\n");
return 0;
}
输出:
Enter the content of data file
111
222
333
444
555
contents of ODD file
111
333
555
contents of EVEN file
171061810
171193396
所以在这里,为什么 putw
使用 getw
显示正确的输出,而 fprintf
和 printf
仅使用 scanf
显示正确的输出。但是,所有文件中存储的数据都是正确的!!!
为什么会这样?
why putw shows correct output with getw, and fprintf and printf only shows correct output with scanf
因为 getw() 函数从流中读取下一个 word。 单词的大小是 int 的大小,可能因机器而异`
在评论中可以看到:171061810
是0xA323232
(十六进制数)。当您使用 ASCII 码从十六进制转换为字符时,您会得到:
Hex Character
A ==> \n
32 ==> 2
32 ==> 2
32 ==> 2
so
171061810 is "\n222".
类似于444
。
谁能给我解释一下这里到底发生了什么? 我的文件管理有问题。
代码说明: 在这里,它从用户那里获取整数,然后存储在 DATA 文件中。 然后它从 DATA 文件中读取整数并通过 ODD 和 EVEN 过滤它们并将其存储在相应的文件中。
这是我的代码。
#include<stdio.h>
int main()
{
FILE *f1, *f2, *f3;
int number;
printf("Enter the content of data file\n");
f1 = fopen("DATA","w");
for(int i=1;i<=30;i++)
{
/*problem is here*/
number=getw(stdin);
//scanf("%d",&number);
if(number==-1 || number==EOF) break;
putw(number,f1);
}
fclose(f1);
f1 = fopen("DATA","r");
f2 = fopen("ODD","w");
f3 = fopen("EVEN","w");
while((number = getw(f1))!=EOF)
{
if(number % 2 == 0)
putw(number, f3);
else
putw(number,f2);
}
fclose(f1);
fclose(f2);
fclose(f3);
f2 = fopen("ODD","r");
f3 = fopen("EVEN","r");
printf("\n\ncontents of ODD file\n");
while((number=getw(f2))!=EOF)
/* problem is here */
putw(number,stdout);
//fprintf(stdout,"%d\n",number);
//printf("%d",number);
printf("\n\ncontents of EVEN file\n");
while((number=getw(f3))!=EOF)
/*problem is here */
//putw(number,stdout);
fprintf(stdout,"%d\n",number);
//printf("%d",number);
fclose(f2);
fclose(f3);
printf("\n");
return 0;
}
输出:
Enter the content of data file
111
222
333
444
555
contents of ODD file
111
333
555
contents of EVEN file
171061810
171193396
所以在这里,为什么 putw
使用 getw
显示正确的输出,而 fprintf
和 printf
仅使用 scanf
显示正确的输出。但是,所有文件中存储的数据都是正确的!!!
为什么会这样?
why putw shows correct output with getw, and fprintf and printf only shows correct output with scanf
因为 getw() 函数从流中读取下一个 word。 单词的大小是 int 的大小,可能因机器而异`
在评论中可以看到:171061810
是0xA323232
(十六进制数)。当您使用 ASCII 码从十六进制转换为字符时,您会得到:
Hex Character
A ==> \n
32 ==> 2
32 ==> 2
32 ==> 2
so
171061810 is "\n222".
类似于444
。