Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
fscanf()过滤器_C_Scanf - Fatal编程技术网

fscanf()过滤器

fscanf()过滤器,c,scanf,C,Scanf,我有一个包含以下格式数据的文件: 名称工作日月日,年开始小时:开始分钟距离小时:分钟:秒 例如: John Mon 2011年9月5日09:18 5830 0:26:37 我想将其扫描到结构中: typedef struct { char name[20]; char week_day[3]; char month[10]; int day; int year; int startHour; int startMin; int d

我有一个包含以下格式数据的文件:

名称工作日月日,年开始小时:开始分钟距离小时:分钟:秒

例如: John Mon 2011年9月5日09:18 5830 0:26:37

我想将其扫描到结构中:

typedef struct {
    char name[20];
    char week_day[3];
    char month[10];
    int day;
    int year;
    int startHour; 
    int startMin;
    int distance;
    int hour;
    int min;
    int sec;
} List;
我使用fscanf():

我的问题是我想过滤掉输入字符串中的噪声,即:

月日*,*年您可以将此噪波放入
scanf
格式字符串中

还要注意,对于日期/时间字符串,可以使用
strtime
。它执行与scanf相同的工作,但在日期/时间上是专用的。您将能够使用
%Y
%M
。。。其他的。将“noise”放在格式字符串中

此外,您可能希望限制字符串的大小

并去掉数组的
&

并从
scanf
测试返回值

// John Mon September 5, 2011 09:18 5830 0:26:37
if (scanf("%19s%2s%9s%d,%d%d:%d%d%d:%d:%d", ...) != 11) /* error */;
//             ^^^ error: not enough space
请注意,
week\u day
只能容纳2个字符,并且只能使用零终止符。

只有数组(字符串)不需要在scanf调用中使用
&
int
变量执行:
scanf(…,chararray,&integer)
。计算行数后,需要将文件重置为开头(或者,读取一次,并根据需要继续重新分配);提示:使用
倒带
。最后一件事:别忘了释放你分配的内存。最后一件事(lol):提高编译器的警告级别,注意警告。
#include <stdio.h>

/*
 Struct to hold data for each runners entry
 */
typedef struct {

    char name[21];
    char week_day[4];
    char month[11];
    int date,
    year,
    start_hour,
    start_min,
    distance,
    end_hour,
    end_min,
    end_sec;

} runnerData;

int main (int argc, const char * argv[])
{
    FILE *dataFile = fopen("/Users/dennisnielsen/Documents/Development/C/Afleveringer/Eksamen/Eksamen/runs.txt", "r");
    char ch;
    int i, lines = 0;

    //Load file
    if(!dataFile)
        printf("\nError: Could not open file!");

    //Load data into struct.
    ch = getc(dataFile);

    //Find the total ammount of lines
    //To find size of struct array
    while(ch != EOF){
        if(ch == '\n')
            lines++;

        ch = getc(dataFile);
    }

    //Allocate memory
    runnerData *list = malloc(sizeof(runnerData) * lines);

    //Load data into struct
    for(i = 0; i < lines; i++){

        fscanf(dataFile, "%s %s %s %d, %d %d:%d %d %d:%d:%d %[\n]",
               list[i].name,
               list[i].week_day,
               list[i].month,
               list[i].date,
               list[i].year,
               list[i].start_hour,
               list[i].start_min,
               list[i].distance,
               list[i].end_hour,
               list[i].end_min,
               list[i].end_sec);

        printf("\n#%d:%s", i, list[i].name);
    }  

    fclose(dataFile);


    return 0;
}
// John Mon September 5, 2011 09:18 5830 0:26:37
if (scanf("%19s%2s%9s%d,%d%d:%d%d%d:%d:%d", ...) != 11) /* error */;
//             ^^^ error: not enough space