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的组合使用的更多相关文章

  1. fopen\fread\fwrite\fscanf\fprintf\fseek\feof\rewind\fgets\fputc等系列函数使用总结

    转载自:http://blog.csdn.net/xidianzhimeng/article/details/23541289 1 fopen 函数原型:FILE * fopen(const char ...

  2. fscanf和fprintf

    fscanf和fprintf fscanf的字符串是在键盘的缓冲区,fprintf是在显示器的缓冲区. 1.函数原型: int fprintf(FILE *fp, const char *format ...

  3. 文件内容操作篇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); 函数说明 ...

  4. fscanf()函数基本用法

    FILE *fp; while(!feof(fp)) { fscanf(fp,"%s%d%lf",a,&b,&c);//这里%s对应的a不需要加上取地址符号& ...

  5. 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字符串包含欲打开的文件路径及文件名,参 ...

  6. 函数fgets和fputs、fread和fwrite、fscanf和fprintf用法小结 (转)

    函数fgets和fputs.fread和fwrite.fscanf和fprintf用法小结 字符串读写函数fgets和fputs 一.读字符串函数fgets函数的功能是从指定的文件中读一个字符串到字符 ...

  7. C++之函数fgetc和fputc、fgets和fputs、fread和fwrite、fscanf和fprintf用法小结

    #include <iostream> #include <cstdio> #include <cstdlib> using namespace std; int ...

  8. 用fscanf()从文件取数据时,如何判断文件结束

    例子:从键盘输入若干行字符(每行长度不等),输入后把它们存储到一磁盘文件中.再从该文件中读入这些数据,将其中小写字母转换成大写字母后再显示屏上输出. 有两种方法 1.使用feof()函数 #inclu ...

  9. fscanf的返回值未成功输入的元素个数 .xml

    pre{ line-height:1; color:#38ede1; background-color:#5b2814; font-size:16px;}.sysFunc{color:#008080; ...

随机推荐

  1. CodeForces - 1000E We Need More Bosses

    题面在这里! 依然一眼题,求出割边之后把图缩成一棵树,然后直接求最长链就行了2333 #include<bits/stdc++.h> #define ll long long using ...

  2. [拒绝毒瘤的小清新系列] give you a tree

    题面在这里! (小声)这其实是我读错某题之后自己出的一道题...... 正解也很简单啦,直接扫描线+线段树水过(考虑一个合法的区间正好有 siz-1 对树上相邻的点对)23333 #include&l ...

  3. Problem F: 加密程序2

    #include<stdio.h> int main() { int i; ]; while(gets(a)!=NULL) { ;a[i]!='\0';i++) if('A'<=a[ ...

  4. ob_flush()和flush()和ob_implicit_flush(true)

    http://www.jb51.net/article/16215.htm 第一: “ob_flush()和flush()的区别.前者是把数据从PHP的缓冲中释放出来,后者是把不在缓冲中的或者说是被释 ...

  5. BaseLoadDataForNetFragment

    /** * Fragment 加载网络请求 */ public abstract class BaseLoadDataForNetFragment extends Fragment { // 视图是否 ...

  6. MYSQL复习笔记11-排序分组

    Date: 20140223Auth: Jin 一.排序 order by作用:对查询结果进行排序1.基本语法SELECT column_name(s) FROM table_name ORDER B ...

  7. $stateProvider resovle 无法找到的原因

    $stateProvider 在写resolve的时候,也要写上controller,不能直接在页面上用ng-controller 来指定!!!! 不然的话,就是报依赖注入异常,找不到该provide ...

  8. python 协程的学习记录

    协程是个子程序,执行过程中,内部可中断,然后转而执行别的子程序,在适当的时候再返回来接着执行 从句法上看,协程与生成器类似,都是定义体中包含 yield 关键字的函数.可是,在协程中,yield 通常 ...

  9. 练oj时的小技巧(大多都在oj记录里,这是被忘记的部分)

    1. getline()函数,头文件为#include<string> getline(istream &in, string &s):从输入流读入一行到string s ...

  10. centos7安装后的防火墙问题

    centos7 默认使用firewall作为防火墙 停止并关闭开机自启动: systemctl stop firewalld.service #停止firewall systemctl disable ...