拆分长字符串 [英] Splitting a long string

查看:40
本文介绍了拆分长字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个存档 results.csv,我需要阅读这个存档的前两行,拆分第二行并在 output.txt 上打印出来.不知何故它没有打印任何东西,但我不知道原因.

I have an archive results.csv and I need to read the first two lines of this archive, split the second one and print them out on output.txt. Somehow it's not printing anything, yet I don't know the reason.

我没有添加我确定没问题的功能.

I didn't add the functions that I'm sure are fine.

命令:a.c results.csv

第一行:date,home_team,away_team,home_score,away_score,Tournament,city,country,neutral

第二行:18721130,Scotland,England,0,0,Friendly,Glasgow,Scotland,FALSE

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

typedef struct
{
    char *line1;
    long int date;
    char *h_team;
    char *a_team;
    int gols_h_team;
    int gols_a_team;
    char *reason;
    char *city;
    char *country;
    char *neutral_field;

}Data;

void alloc_Data(Data *d, int size)
{
d->line1 = (char*)malloc(50*sizeof(char)); 
d->h_team = (char*)malloc(30*sizeof(char)); 
d->a_team = (char*)malloc(30*sizeof(char)); 
d->reason = (char*)malloc(30*sizeof(char)); 
d->city = (char*)malloc(30*sizeof(char)); 
d->country = (char*)malloc(30*sizeof(char)); 
d->neutral_field = (char*)malloc(9*sizeof(char)); 
}

void store(Data *d, FILE *input, FILE *output, int size)
{

    fscanf(input,  "%s", d[0].line1);
    fprintf(output,  "%s\n", d[0].line1);

    for(int i = 1; i < size; i++)
    {
        fscanf(input, "%li,%[^,]s%[^,]s%d,%d,%[^,]s%[^,]s%[^,]s%[^,]s", &d[i].date, d[i].h_team, d[i].a_team, &d[i].gols_h_team, &d[i].gols_a_team, d[i].reason, d[i].city, d[i].country, d[i].neutral_field );
        fprintf(output, "%li,%s,%s,%d,%d,%s,%s,%s,%s\n", d[i].date, d[i].h_team, d[i].a_team, d[i].gols_h_team, d[i].gols_a_team, d[i].reason, d[i].city, d[i].country, d[i].neutral_field );

    }

}

int main(int argc, char *argv[])
{
    FILE *input;
    FILE *output;
    char *string = "output.txt";
    int size = 2;

    open_input(argv, &input);   
    open_output(string, &output);   

    Data *d;
    d = (Data*)malloc(size*sizeof(Data)); 
    alloc_Data(d, size);

    store(d, input, output, size);

    free(d);

    return 0;
}

推荐答案

OP 的 fscanf() 格式在 %[^,] 和缺失的 ,.@Gem Taylor

OP's fscanf() format is messed up with an s after %[^,] and missing ,. @Gem Taylor

更好的选择是使用fgets() 读取所有 行.(包括第一个)

A better alternative is to read all lines using fgets(). (including the first)

// fscanf(input, "%li,%[^,]s%[^,]s%d,%d,%[^,]s%[^,]s%[^,]s%[^,]s",...d[i].neutral_field );

#define EXPECTED_MAX_BUFFER_SIZE 150
char buffer[EXPECTED_MAX_BUFFER_SIZE * 2];  // suggest using 2x expected max size
if (fgets(buffer, sizeof buffer, input)) {

然后使用 sscanf() 解析团队结果.在末尾使用 " %n" 是一种测试整个 sscanf() 是否成功且没有额外垃圾的简单方法.sscanf() 应该有宽度限制和复杂扫描的好处,有一些定义来管理说明符.注意 "%29[^,]"

Then parse the team results with sscanf(). Using a " %n" at the end is an easy way to test if the entire sscanf() succeeded with no extra junk. sscanf() deserve width limits and complex scans benefit with some defines to manage the specifiers. Notice no s after "%29[^,]"

    int n = 0;
    #define FDat "%li ,"
    #define FScr "%d ,"
    #define Ftxt " %29[^,],"
    #define Fneu " %8[^,]"
    sscanf(buffer, FDat Ftxt Ftxt FScr FScr Ftxt Ftxt Ftxt Fneu " %n", 
        &d[i].date, d[i].h_team, d[i].a_team, 
        &d[i].gols_h_team, &d[i].gols_a_team, 
        d[i].reason, d[i].city, d[i].country, d[i].neutral_field, &n);

    if (n > 0 && buffer[n] == '\0') {
      // success
      fprintf(output, ...
    } else {
      // bad input
    }

这篇关于拆分长字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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