fscanf和feof的组合使用
http://stackoverflow.com/questions/15719360/using-fscanf-using-feof
靶子代码:
#include<stdio.h>
void main(){
FILE *fp;
int a,b;
fp=fopen("hello.txt","r");
while(!feof(fp)){
fscanf(fp,"%d %d",&a,&b);
printf("%d %d\n",a,b);
}
}
My hello.txt is
1 2
3 4
My Output is
1 2
3 4
4 4
解决方法
1. It means that the way the feof()
function (and other functionality with regards to EOF in general) is used is often misunderstood and wrong. So is your code.
First, fscanf()
doesn't always do what you think it does, and getting lines from a file is better performed using fgets()
. However, if you're really inclined to use fscanf()
, then check if it could read someting at all, else when it couldn't, you will print the variables one time more than needed. So what you should do is:
while (fscanf(fp,"%d %d",&a,&b) == )
{
printf("%d %d\n",a,b);
}
2.The reason you're getting an extra line is that EOF isn't set until after fscanf
tries to read a third time, so it fails, and you print the results anyway. This would do the sort of thing you've intended
while(){
fscanf(fp,"%d %d",&a,&b);
if (feof(fp))
break;
printf("%d %d\n",a,b);
}
fscanf和feof的组合使用的更多相关文章
- fopen\fread\fwrite\fscanf\fprintf\fseek\feof\rewind\fgets\fputc等系列函数使用总结
转载自:http://blog.csdn.net/xidianzhimeng/article/details/23541289 1 fopen 函数原型:FILE * fopen(const char ...
- fscanf和fprintf
fscanf和fprintf fscanf的字符串是在键盘的缓冲区,fprintf是在显示器的缓冲区. 1.函数原型: int fprintf(FILE *fp, const char *format ...
- 文件内容操作篇clearerr fclose fdopen feof fflush fgetc fgets fileno fopen fputc fputs fread freopen fseek ftell fwrite getc getchar gets
clearerr(清除文件流的错误旗标) 相关函数 feof 表头文件 #include<stdio.h> 定义函数 void clearerr(FILE * stream); 函数说明 ...
- fscanf()函数基本用法
FILE *fp; while(!feof(fp)) { fscanf(fp,"%s%d%lf",a,&b,&c);//这里%s对应的a不需要加上取地址符号& ...
- Linux C 文件输入输出函数 fopen()、getc()/fgetc()、putc()/fputc()、fclose()、fprintf()、fscanf()、fgets()、fputs()、fseek()、ftell()、fgetpos()、fsetpos() 详解
fopen(打开文件) 定义函数 FILE * fopen(const char * path,const char * mode); 函数说明 参数path字符串包含欲打开的文件路径及文件名,参 ...
- 函数fgets和fputs、fread和fwrite、fscanf和fprintf用法小结 (转)
函数fgets和fputs.fread和fwrite.fscanf和fprintf用法小结 字符串读写函数fgets和fputs 一.读字符串函数fgets函数的功能是从指定的文件中读一个字符串到字符 ...
- C++之函数fgetc和fputc、fgets和fputs、fread和fwrite、fscanf和fprintf用法小结
#include <iostream> #include <cstdio> #include <cstdlib> using namespace std; int ...
- 用fscanf()从文件取数据时,如何判断文件结束
例子:从键盘输入若干行字符(每行长度不等),输入后把它们存储到一磁盘文件中.再从该文件中读入这些数据,将其中小写字母转换成大写字母后再显示屏上输出. 有两种方法 1.使用feof()函数 #inclu ...
- fscanf的返回值未成功输入的元素个数 .xml
pre{ line-height:1; color:#38ede1; background-color:#5b2814; font-size:16px;}.sysFunc{color:#008080; ...
随机推荐
- bzoj 2803 [Poi2012]Prefixuffix 兼字符串hash入门
打cf的时候遇到的问题,clairs告诉我这是POI2012 的原题..原谅我菜没写过..于是拐过来写这道题并且学了下string hash. 字符串hash基于Rabin-Karp算法,并且对于 ...
- POJ 3378 Crazy Thairs(树状数组+DP)
[题目链接] http://poj.org/problem?id=3378 [题目大意] 给出一个序列,求序列中长度等于5的LIS数量. [题解] 我们发现对于每个数长度为k的LIS有dp[k][i] ...
- BZOJ 1098 [POI2007]办公楼biu(反向图bfs+并查集优化)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1098 [题目大意] 现在有一张图,要求将这张图的点划分为尽量多的分组,对于不同分组的两 ...
- 【后缀自动机】hihocoder1449 后缀自动机三·重复旋律6
解题方法提示 小Hi:上次我们已经学习了后缀自动机了,今天我们再来解决一个用到后缀自动机的问题. 小Ho:好!那我们开始吧! 小Hi:现在我们要对K=1..length(S)求出所有长度为K的子串中出 ...
- 【KMP求最小周期】POJ2406-Power Strings
[题意] 给出一个字符串,求出最小周期. [思路] 对KMP的next数组的理解与运用orz ①证明:如果最小周期不等于它本身,则前缀和后缀必定有交叉. 如果没有交叉,以当前的next[n]为最小周期 ...
- CDOJ 1284 苦恼的郭大侠 map启发式合并
苦恼的郭大侠 题目连接: http://acm.uestc.edu.cn/#/problem/show/1284 Description 花开雷霆崖,血染伊吕波. 公元1772年. 郭大侠终于照着天行 ...
- Codeforces Gym 100269K Kids in a Friendly Class 构造题
Kids in a Friendly Class 题目连接: http://codeforces.com/gym/100269/attachments Description Kevin resemb ...
- datatables .fnDraw is not a function
Datatables中文网:http://dt.thxopen.com/manual/api.html API DataTables 提供的可以操作表格数据的API,有下面六个关键部分: 表格(tab ...
- 【maven】maven项目移除Maven Dependencies后如何再添加进去
比较着急这几天弄一个项目,所以匆忙间把maven项目的Maven Dependencies给remove掉了 如下图: 这下可好,整个项目报错了 解决方法: 对比了有Maven Dependencie ...
- Linux 下的图形库介绍
在进行Linux下的图形系统编程时,我们常常会遇到以下这些概念: Framebuffer, X11, SDL,DFB, miniGUI, OpenGL,QT, GTK,KDE, GNOME等等. 一. ...