这道题意图是什么?

mawl2002 2009-03-05 07:57:36
#include <stdio.h>

int main()
{
int d;
int count = 0;

FILE *fp = NULL;

fp = fopen("data.txt", "r");

if( fp != NULL ){
printf("open file ok\n");
while( !feof(fp) ){
printf("enter loop\n");
fscanf(fp, "%d", &d);
count++;
}

}else{
printf("file open failed!\n");
return -1;
}

fclose( fp );

printf("count = %d\n", count );

return 0;

}

文件的内容为12345,写出语句的执行结果。
...全文
152 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
mawl2002 2009-03-07
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 rsp19801226 的回复:]
int main()
{
int d;
int count = 0;

FILE *fp = NULL;

fp = fopen("data.txt", "r"); //open the data.txt with reading mode

if( fp != NULL ){ //if the txt is opened successfully
printf("open file ok\n"); //print this sentence
while( !feof(fp) ){ //当文件没有读完
printf("enter loop\n");// 打印此句
fscanf(fp, "%d", &d…
[/Quote]

Got it, thank you
rsp19801226 2009-03-06
  • 打赏
  • 举报
回复
int main()
{
int d;
int count = 0;

FILE *fp = NULL;

fp = fopen("data.txt", "r"); //open the data.txt with reading mode

if( fp != NULL ){ //if the txt is opened successfully
printf("open file ok\n"); //print this sentence
while( !feof(fp) ){ //当文件没有读完
printf("enter loop\n");// 打印此句
fscanf(fp, "%d", &d); //以整数为单位读取数据,12345没有超过整数范围,被作为1个整数计算在count里面
count++; //它会以第一个非数字的字符结束
}

}else{
printf("file open failed!\n");
return -1;
}

fclose( fp );

printf("count = %d\n", count );

return 0;

}
所以说你如果以%lf来读取是可以多获取数字位的
Johnsen2009 2009-03-05
  • 打赏
  • 举报
回复
打印文件并计算数字个数
traceless 2009-03-05
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 mawl2002 的回复:]
中间的打印语句是我加的,关键是count的值跟什么有关系?
[/Quote]

结果是1
如果是1234 56的话,那么count=2

此题的话跟你从磁盘文件中读入了多少次%d有关
beck_zhou 2009-03-05
  • 打赏
  • 举报
回复
读取int型的,需要以 空格或“,”来count++吧
beck_zhou 2009-03-05
  • 打赏
  • 举报
回复
读取int型的,需要以 空格或“,”来count++吧
mawl2002 2009-03-05
  • 打赏
  • 举报
回复
中间的打印语句是我加的,关键是count的值跟什么有关系?
  • 打赏
  • 举报
回复
结果确实是1,不过数字再多几位,d接收的数据就不准了,会溢出.
因为fscanf太强大了.
关键考fscanf的用法.
mawl2002 2009-03-05
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 zsf81 的回复:]
12345 5??
[/Quote]
结果是1
zsf81 2009-03-05
  • 打赏
  • 举报
回复
12345 5??
arong1234 2009-03-05
  • 打赏
  • 举报
回复
看你会不会文件IO

69,369

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧