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; ...
随机推荐
- 【点分治】【FFT】Gym - 101234D - Forest Game
存个求树上每种长度(长度定义为路径上点数)的路径条数的模板:num数组中除了长度为1的以外,都算了2次. 不造为啥FFT数组要开八倍. #include<cstdio> #include& ...
- 【计算几何】【凸包】【极角排序】【二分】Gym - 101128J - Saint John Festival
平面上n个红点,m个黑点,问你多少个黑点至少在一个红三角形内. 对红点求凸包后,转化为询问有多少个黑点在凸包内. 点在凸多边形内部判定,选定一个凸包上的点作原点,对凸包三角剖分,将其他的点极角排序之后 ...
- [LOJ500]ZQC的拼图
题目大意: 给你一个m*m的格子,让你往里面放给定的直角三角形,直角顶点必须放在右上角且不能翻转,但是可以把所有给定的三角形放大一个整数倍k,问至少放大几倍能使格子的左下角和右上角连起来?(可以超出边 ...
- Tomcat上java.lang.IllegalStateException: Optional int parameter 'id' is not present
今日, 本人在tomcat+spring mvc平台的服务器上遇到java.lang.IllegalStateException: Optional int parameter 'id' is not ...
- 20172333 2017-2018-2 《Java程序设计》第4周学习总结
20172333 2017-2018-2 <Java程序设计>第4周学习总结 教材学习内容 1.类结构的定义与概念 2.利用实例数据建立对象状态的概念 3.描述可见性修饰符作用在方法和数据 ...
- linux图机界面机制
1.X WindowX Window 是由麻省理工学院(MIT)推出的窗口系统,简称X,它旨在建立不依赖于特定硬件系统的图形和文字显示窗口系统的标准.1987 年9 月,MIT 推出了X 系统的11 ...
- Windows环境下32位汇编语言程序设计(典藏版)
<Windows环境下32位汇编语言程序设计(典藏版) > 基本信息 作者: 罗云彬 出版社:电子工业出版社 ISBN:9787121207594 上架时间:2013-7-8 出版日期:2 ...
- iOS:UIView、UIControl、UIButton、UILabel简单的属性和方法常识
常见属性和方法 一 .UIVIew 常见属性 1.frame 位置和尺寸(以父控件的左上角为原点(0,0)) 2.center 中点 (以父控件的左上角为原点(0,0)) 3.bounds 位置和尺寸 ...
- linux:ls、ls -l、ls -al区别 示例
linux:ls.ls -l.ls -al区别 示例 比如test文件夹下有一个test文件.一个.文件夹.一个..文件夹. 则,执行三个命令后,显示效果如下: [root@linuxserver t ...
- springmvc怎么重定向,从一个controller跳到另一个controller
第一种情况,不带参数跳转: 方法一:使用ModelAndView return new ModelAndView("redirect:/toList"); 这样可以重定向到toL ...