如何使用 fscanf 扫描 C 中的文件 [英] How do I scan a file in C using fscanf

查看:59
本文介绍了如何使用 fscanf 扫描 C 中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用代码

while(fscanf(input, "%49[^@ ]@%49s -> %49[^@ ]@%49s", sender, trash, receiver, trash) != EOF){
   printf("%s " "%s\n", sender, reciever);
}

尝试读取和打印文件中的每一行但是,当我运行此代码时,它会陷入无限循环,一遍又一遍地打印文件的第一行.执行此代码后,如何将扫描仪向下移动到下一行.在此先感谢您的帮助.

to try to read and print every line from a file however when I run this code it gets stuck in an endless loop printing the first line of the file over and over again. How can I move the scanner down to the next line after it has executed this code. Thank you in advance for any help.

推荐答案

#include < stdio.h >

FILE *fr;

main()
{
    int n;
    char line[80];

    fr = fopen ("test.txt", "r");  /* open the file for reading */

    while(fgets(line, 80, fr) != NULL)
    {
    /* get a line, up to 80 chars from fr.  done if NULL */
    sscanf  ( line );
    }
    fclose(fr);  /* close the file prior to exiting the routine */
}

做你想做的事情的基本方法...

The basic way to do what you want...

这篇关于如何使用 fscanf 扫描 C 中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆