简体   繁体   English

从此测试文件中读取输入并测试EOF的最佳方法

[英]C best way to read input from this test file and test for EOF

Hey everyone im very new to the C programming language and i am trying to read from a simple .txt file which contains this information: 大家好,我是C编程语言的新手,我正试图从一个简单的.txt文件中读取该文件,其中包含以下信息:

  13 11 2011 13 10 00 GS452 45 20
  13 11 2011 15 14 23 EI597 60 30
  13 11 2011 15 34 35 EI600 20 15

currently i am using fscaf to read in the whole line then store them in correct variables in my structure. 目前,我正在使用fscaf读取整行,然后将它们存储在结构中的正确变量中。 I looked up online and it seems that checking for EOF isnt as straight forward as it seems using fscanf as you it returns the amount of "items" read. 我在网上查询了一下,看来检查EOF并不像使用fscanf那样简单,因为它返回读取的“项目”数量。

Using my below code and the above file: 使用下面的代码和上面的文件:

1) which is the best way to read and store the information from the file in the correct place 1)这是从文件中正确位置读取和存储信息的最佳方法

2) the best way to check for EOF so it stops and the end of file/doesnt read empty files. 2)检查EOF的最佳方法,使其停止并且文件结尾/不读取空文件。

Header file: 头文件:

  #ifndef MAYDAY_STRUCT_H
  #define   MAYDAY_STRUCT_H

  #ifdef    __cplusplus
   extern "C" {
   #endif

typedef struct {
    /* unsigned because these values cannot be negative*/

    unsigned int day;
    unsigned int month;
    unsigned int year;
    unsigned int hour;
    unsigned int mins;
    unsigned int secs;
    char ais[5];
    unsigned int l_boat_time;
    unsigned int heli_time;


} mayday_call;

void read_may_day_file();

#ifdef  __cplusplus
}

main.c main.c

  #include <stdio.h>
  #include <stdlib.h>
  #include "mayday_struct.h"


 int main(int argc, char** argv) {


read_may_day_file();


return (EXIT_SUCCESS);
 }

void read_may_day_file() {

char locof[30];
char eofTest;
mayday_call mday;



printf("please enter the location of the input file \n");
scanf("%s", locof);

FILE *fp;
fp = fopen(locof, "r");


if (fp) {



        fscanf(fp, "%d %d %d %d %d %d %s %d %d", &mday.day, &mday.month, &mday.year, &mday.hour, &mday.mins, &mday.secs, mday.ais, &mday.l_boat_time, &mday.heli_time);


        printf("reading file mayday_1.txt \n"
                "day %d \n"
                "month %d \n"
                "yr %d \n"
                "hour % d\n"
                "mins %d \n"
                "sec %d \n"
                "ais %s \n"
                "lBoattime %d\n"
                "helitime %d \n", mday.day, mday.month,
                mday.year, mday.hour, mday.mins, mday.secs, mday.ais, mday.l_boat_time, mday.heli_time);

        fclose(fp);
    }

}

So I would suggest using fgets it will read from a file line by line, and return NULL when there is nothing left to read. 因此,我建议使用fgets它将逐行地从文件中读取文件,并在没有剩余读取内容时返回NULL。 If everything in your case is on the same line I would probably stick with fscanf , but you could also use strtok and read in the whole line and then parse through it using strtok 如果您的所有情况都在同一行上,我可能会坚持使用fscanf ,但是您也可以使用strtok并读取整行,然后使用strtok对其进行解析

Take a look here for some more info: http://www.tutorialspoint.com/c_standard_library/c_function_fgets.htm 在这里查看更多信息: http : //www.tutorialspoint.com/c_standard_library/c_function_fgets.htm

If you were looking to use fscanf you were saying that it returns the amount that was read, so if nothing was read, then you have reached the end of the file. 如果您要使用fscanf ,则是说它返回已读取的数量,因此,如果未读取任何内容,则您已到达文件末尾。

Don't forget you also have to open the files that you wish to read and close them when you are done. 不要忘记,您还必须打开要阅读的文件,并在完成后将其关闭。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从C中的stdin读取不可预测且不确定(即没有EOF)大小的输入的最佳方法是什么? - What is the best way to read input of unpredictable and indeterminate (ie no EOF) size from stdin in C? 如何从C中的测试用例文件中读取输入 - How To Read Input From Test Case File in C C-从输入文件中读取,将测验分数转换为字母等级,计算平均值,提取每个测验的最小值和最大值 - C - Read from Input File, Convert Test Score to Letter Grade, Calculate Average, Extract Minimum and Maximum of each test 用户输入以读取文件并测试其是否有效? 在C中 - User input to read file and test that its worked? In C C:仅固定某些字段时,读取输入文件的最佳方法 - C: Best way to read input file if only certain fields are fixed 从文件到文件的读写,直到linux下的c中的EOF - read and write from file to file until EOF in c under linux C:I / O - 从文件读取整数的最快/最佳方式 - C: I/O - Quickest /best way to read ints from file 从C中的文本文件读取解析数据的最佳方法? - Best way to read a parse data from text file in C? 如何从文件中读取数字直到C中的EOF - How to read only numbers from file until EOF in C 在C中检查单元测试:测试静态方法的最佳方法 - Check Unit testing in C: Best way to test static methods
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM