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; ...
随机推荐
- 【数据结构】Not so Mobile (6-9)
[UVA839]Not so Mobile 算法入门经典第6章6-9(P157) 题目大意:输入一个树状天平,根据力矩相等原则判断是否平衡. 试题分析:貌似没有什么难点…… #include<i ...
- 【bfs+优先队列】POJ2312-Battle City
[思路] 题目中的“可以沿直线发射打破砖墙”可能会迷惑到很多人,实际上可以等价理解为“通过砖墙的时间为2个单位”,这样题目就迎刃而解了.第一次碰到时可能不能很好把握,第二次基本就可以当作水题了. [错 ...
- 微信小程序 Session 失效
微信小程序 Session 失效 微信小程序,前端请求后端,中间多了个微信服务器,所以请求的流程就是 页面--微信服务器--目标服务器 这就导致了一个问题 session 每次请求都是一个新的会话 解 ...
- AIM Tech Round (Div. 1) D. Birthday 数学 暴力
D. Birthday 题目连接: http://www.codeforces.com/contest/623/problem/D Description A MIPT student named M ...
- Java反射机制的简单学习
今天看了一下Java的反射机制,就此记录一下. 首先,我们要先了解一下什么是反射? 反射的概念是由Smith在1982年首次提出的,主要是指程序可以访问.检测和修改它本身状态或行为的一种能力. JAV ...
- How do I find what queries were executing in a SQL memory dump?-----stack
https://blogs.msdn.microsoft.com/askjay/2010/10/03/how-do-i-find-what-queries-were-executing-in-a-s ...
- winform窗体MaximizeBox
如果MaximizeBox为false会导致Form2窗体底部不显示. =>解决办法TopMost属性为true. Form2 _frm2 = new Form2(); _frm2.Maximi ...
- mac清理磁盘方法
1.清除缓存文件 cd ~/Library/Caches/ rm -rf ~/Library/Caches/* 2.删除所有系统日志 sudo rm -rf /private/var/log/*3.删 ...
- iOS: 复选框使用---第三方框架SSCheckBoxView-master
在iOS开发中对应用程序进行设置时一般都用UISwitch,偶尔显得单调,这时候你可以选择使用第三方开源类库SSCheckBoxView . SSCheckBoxView是一个可用在iOS上一个复选框 ...
- saltstack之mount和umount的用法
线上很多服务器都需要挂载存储上的公共目录,并实现开机启动(/etc/fstab),比如web的静态文件共享目录,日志远程集中收集等. 一.批量挂载部分. 1.在node1上配置nfs服务器,有关nfs ...