如何使用c从csv文件中获取具有精确数据的特定行? [英] how to get specific rows from csv file with exact data using c?

查看:313
本文介绍了如何使用c从csv文件中获取具有精确数据的特定行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的csv文件,我只想获取那些以字符 A开头的行,所以我得到了输出,但还有一些附加列为 0,请帮助我找出我出错了吗?

This is my csv file, i want to get only those row which start with character "A" so i got my output but with some addition column as '0' please help me to find were i went wrong?

还有另外一件事,我想删除面包,anName,ot等特定列

And one more thing i want to remove specific column like bread,anName,ot

Name,id,bread,anName,Ot,number
A,1,animal,tiger,op,8.1
M,2,animal,toper,ip,9.1
A1,7,animal,dog,cp,Na11
A2,9,animal,mouse,ap,0
A23,9,animal,pouch,gp,Na11




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

#define NUMLETTERS 100

typedef struct {
    char Name[100];
    int id;
    char number[100];
} record_t;

int main(void) {
    FILE *fp;
    record_t records[NUMLETTERS];
    int count = 0, i;

    fp = fopen("letter.csv", "r");
    if (fp == NULL) {
        fprintf(stderr, "Error reading file\n");
        return 1;
    }

    while (fscanf(fp, "%s,%d,%s", records[count].name, &records[count].id, records[count].number) == 1) 
        count++;

    for (i = 0; i < count; i++) {
        if(records[i].Name[0] == 'A'){
            printf("%s,%d,%s\n", records[i].Name, records[i].id, records[i].number);
        }
    }

    fclose(fp);

    return 0;
}

我希望输出为:

A,1,8.1
A1,7,Na11
A2,9,0
A23,9,Na11


推荐答案


i我只想删除那些以字符 A开头的行。

i want to get only those row which start with character "A"

我想删除A和老虎之间的数字,

i want to remove the number which coming between A and tiger,

如果我对您的理解是正确的,而您只想存储以'A'开头的行,那么我会调整您的用 fgets()读取每一行,然后检查缓冲区中的第一个字符是否为'A',如果是这样,请继续; 并获得下一行。对于那些以'A'开头的行,只需使用 sscanf 将数据解析为结构体数组记录

If I understand you correctly and you only want to store rows beginning with 'A', then I would adjust your approach to read each line with fgets() and then check whether the first character in the buffer is 'A', if so, continue; and get the next line. The for those lines that do start with 'A', simply use sscanf to parse the data into your array of struct records.

第二部分是删除'A'之间的数字 tiger ,您的存储和您的输出之间是有区别的(这在只存储以'A'开头的记录中起作用),但是对于那些存储在以'A'开始的行中的结构/ code>,您可以简单地不输出 pin 结构成员来获取所需的输出。

For your second part of removing the number between 'A' and "tiger", there is a difference between what you store and what you output (this comes into play in storing only records beginning with 'A' as well), but for those structs stored where the line starts with 'A', you can simply not-output the pin struct member to get the output you want.

一次读取一行的方法仅需要声明一个附加的字符数组(缓冲区),下面称为 buf ,以将每个行读入 fgets(),例如

The approach to reading a line at a time will simply require that you declare an additional character array (buffer), called buf below, to read each line into with fgets(), e.g.

    char buf[3 * NUMLETTERS] = "";
    ...
    /* read each line into buf until a max of NUMLETTERS struct filled */
    while (count < NUMLETTERS && fgets (buf, sizeof buf, fp)) {   
        record_t tmp = { .Refdes = "" };    /* temporary struct to read into */
        if (*buf != 'A')                    /* if doesn't start with A get next */
            continue;
        /* separate lines beginning with 'A' into struct members */
        if (sscanf (buf, " %99[^,],%d,%99[^\n]",
                    tmp.Refdes, &tmp.pin, tmp.NetName) == 3)
            records[count++] = tmp;         /* assign tmp, increment count */
        else
            fprintf (stderr, "%d A record - invalid format.\n", count + 1);
    }

使用它的简短示例,(因为我们不确定删除 的目的),我们包含了一个预处理器条件,该条件只会输出 .Refdes .NetName 成员默认情况下,但是如果您 #define WITHPIN 或在编译字符串中包含定义(例如, -DWITHPIN ),它也将输出 .pin 成员。

A short example putting that to use and (since we are not sure what "remove" is intended to be), we have included a pre-processor conditional that will only output the .Refdes and .NetName members by default, but if you either #define WITHPIN or include the define in your compile string (e.g. -DWITHPIN) it will output the .pin member as well.

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

#define NUMLETTERS 100

typedef struct {
    char Refdes[NUMLETTERS];
    int pin;
    char NetName[NUMLETTERS];
} record_t;

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

    record_t records[NUMLETTERS];
    char buf[3 * NUMLETTERS] = "";
    int count = 0, i;
    /* use filename provided as 1st argument (stdin by default) */
    FILE *fp = argc > 1 ? fopen (argv[1], "r") : stdin;

    if (!fp) {  /* validate file open for reading */
        perror ("file open failed");
        return 1;
    }

    /* read each line into buf until a max of NUMLETTERS struct filled */
    while (count < NUMLETTERS && fgets (buf, sizeof buf, fp)) {   
        record_t tmp = { .Refdes = "" };    /* temporary struct to read into */
        if (*buf != 'A')                    /* if doesn't start with A get next */
            continue;
        /* separate lines beginning with 'A' into struct members */
        if (sscanf (buf, " %99[^,],%d,%99[^\n]",
                    tmp.Refdes, &tmp.pin, tmp.NetName) == 3)
            records[count++] = tmp;         /* assign tmp, increment count */
        else
            fprintf (stderr, "%d A record - invalid format.\n", count + 1);
    }

    if (fp != stdin)   /* close file if not stdin */
        fclose (fp);

    for (i = 0; i < count; i++)
#ifdef WITHPIN
        printf ("%-8s %2d     %s\n",
                records[i].Refdes, records[i].pin, records[i].NetName);
#else
        printf ("%-8s     %s\n", records[i].Refdes, records[i].NetName);
#endif
}

示例用法/输出

$ ./bin/getaonly dat/getasonly.txt
A            tiger
A1           dog
A2           mouse
A23          pouch

如果定义 -DWITHPIN 在编译字符串中,那么您将获得所有三个输出:

If you define -DWITHPIN in your compile string, then you will get all three outputs:

$ ./bin/getaonly dat/getasonly.txt
A         1     tiger
A1        7     dog
A2        9     mouse
A23       9     pouch

注意:,使用存储在数组中的数据,您可以将输出格式调整为所需的任何格式)

(note: with the data stored in your array, you can adjust the output format to anything you need)

由于存在不确定性,您是否要存储全部并仅输出以'A'开头的记录,还是只想存储以<$ c开头的记录$ c>'A'-让我知道是否需要进行更改,很高兴为您提供进一步的帮助。

Since there is some uncertainty whether you want to store all and output only records beginning with 'A' or only want to store records beginning with 'A' -- let me know if I need to make changes and I'm happy to help further.

这篇关于如何使用c从csv文件中获取具有精确数据的特定行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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