fscanf() 过滤器 [英] fscanf() filter

查看:71
本文介绍了fscanf() 过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

名称 WeekDay Month day, Year StartHour:StartMin 距离 Hour:Min:Sec

示例:约翰星期一 2011 年 9 月 5 日 09:18 5830 0:26:37

我想把它扫描成一个结构体:

typedef struct {字符名称[20];字符 week_day[3];字符月[10];国际日;整数年;int startHour;int startMin;整数距离;整数小时;整数分钟;整数秒;} 列表;

我使用 fscanf():

List listarray[100];for(int i = 0; ch = fgetc(file) != 'EOF'; ch = fgetc(file), i++){if(ch != '\0'){fscanf(file, "%s %s %s %d %d %d %d %d %d %d %d", &listarray[i].name...等)}}

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

月日*,* 年 <- 逗号在所有条目中都是一致的.我只想要 char 数组中的月份,int 中的日期.

和时间戳:

startHour:startmin 和 hour:min:sec <- 这里我想过滤掉冒号.

我需要先把它放入一个字符串然后再做一些拆分,还是我可以在 fscanf 中处理它?<​​/p>

更新:

好的,我现在一直在努力让它工作,但我根本做不到.我真的不知道这是什么问题.

#include /*为每个跑步者条目保存数据的结构*/类型定义结构{字符名称[21];字符 week_day[4];字符月[11];国际日期,年,开始小时,开始_分钟,距离,end_hour,结束_分钟,end_sec;} runnerData;int main (int argc, const char * argv[]){FILE *dataFile = fopen("/Users/dennisnielsen/Documents/Development/C/Afleveringer/Eksamen/Eksamen/runs.txt", "r");字符 ch;int i, 行 = 0;//加载文件如果(!数据文件)printf("\n错误:无法打开文件!");//将数据加载到结构体中.ch = getc(dataFile);//求总行数//查找struct数组的大小while(ch != EOF){if(ch == '\n')线++;ch = getc(dataFile);}//分配内存runnerData *list = malloc(sizeof(runnerData) * lines);//加载数据到结构体for(i = 0; i < 行; i++){fscanf(dataFile, "%s %s %s %d, %d %d:%d %d %d:%d:%d %[\n]",列表[i].name,列表[i].week_day,列表[i].月,列表[i].日期,列表[i].年,列表[i].start_hour,列表[i].start_min,列表[i].距离,列表[i].end_hour,列表[i].end_min,列表[i].end_sec);printf("\n#%d:%s", i, list[i].name);}fclose(数据文件);返回0;}

我被告知在 fscanf(); 中,只有字符串不需要 & 在它们前面;"但我尝试了带和不带&符号都无济于事.

解决方案

将噪音"放入格式字符串中.

您也可能希望限制字符串的大小.

并去掉数组的 &.

并测试scanf的返回值!

//John Mon 2011 年 9 月 5 日 09:18 5830 0:26:37if (scanf("%19s%2s%9s%d,%d%d:%d%d%d:%d:%d", ...) != 11)/* 错误 */;//^^^ 错误:空间不足

注意 week_day 有 2 个字符的空格和零终止符.

I have a file with data in this format:

Name WeekDay Month day, Year StartHour:StartMin Distance Hour:Min:Sec

Example: John Mon September 5, 2011 09:18 5830 0:26:37

I want to scan this into a struct:

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;

i use fscanf():

List listarray[100];
for(int i = 0; ch = fgetc(file) != 'EOF'; ch = fgetc(file), i++){
    if(ch != '\0'){
        fscanf(file, "%s %s %s %d %d %d %d %d %d %d %d", &listarray[i].name...etc)
    }
}

My issue is that I want to filter out the noise in the input string, that being:

Month day*,* year <- the comma is consistent in all entries. I just want the month in the char array, the day in int.

And the time stamps:

startHour:startmin and hour:min:sec <- here I want to filter out the colon.

Do I need to put it into a string first and then do some splitting, or can I handle it in fscanf?

Update:

Okay, så I've been trying to get this to work now, but I simply cannot. I literally have no idea what the issue is.

#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;
}

I've been told that "only strings to do not require & in front of them in fscanf();" but I tried both with and without ampersand to no avail.

解决方案

Put the "noise" in the format string.

Also you might like to limit the size of strings.

And get rid of the & for arrays.

And test the return value from 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

Notice week_day has space for 2 characters and the zero terminator only.

这篇关于fscanf() 过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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