c - 从 CSV 文件读取并解决 ";"问题

标签 c csv scanf delimiter

我有这个 CSV 文件,我必须读取它并打印每个不同的客户(每一行都是一个客户)

IDClient;Name;Surname;Address;City;State;Postal_Code
111A;Howard;Snyder;2732 Baker Blvd.;Eugene;OR;97403
222B;Yoshi;Latimer;City Center Plaza 516 Main St.;Elgin;OR;97827
333C;John;Steel;12 Orchestra Terrace;Walla Walla;WA;99362
444D;Jaime;Yorres;87 Polk St. Suite 5;San Francisco;CA;94117

我写了这段代码,尽管有一些警告,但似乎可以正常工作,除了 ; 的一些问题

int main(int argc, char const *argv[])
{   
    FILE *fp;
    fp = fopen ("clientes.txt", "rt");
    if (fp == NULL) {
        printf("Error.\n");
    }
    else {
        printf("File is open\n");
        printCustomers(fp);
        fclose(fp);
    }

    return 0;
}


void printCustomers (FILE *fp) 
{
    char id[5], first [8], last [10], adress[17], city[12], state[3], zip[6];
    printf("Printing.\n");
    fscanf(fp, "%*[^\n]\n", NULL); //Skip first line
    printf("-----------------------\n");
    while (fscanf(fp,"%4s %7s %9s %16[^\n] %11[^\n] %2s %5s", &id, &first, &last, &adress, &city, &state, &zip) == 7)
    {
        printf("IdClient: %s\nName: %s\nSurname: %s\nAdress: %s\nCity: %s\nState: %s\nZip: %s\n", id, first, last, adress, city, state, zip);
        printf("-----------------------\n");
    }

}

我的输出是这样的,正如您所看到的 ; 正在破坏输出。我认为这可以通过 fscanf 格式解决,但我不知道该怎么做。

 -----------------------
    IdClient: 111A
    Name: ;Howard
    Surname: ;Snyder;2
    Adress: 732 Baker Blvd.;
    City: Eugene;OR;9
    State: 74
    Zip: 03
    -----------------------
    IdClient: 222B
    Name: ;Yoshi;
    Surname: Latimer;C
    Adress: ity Center Plaza
    City: 516 Main St
    State: .;
    Zip: Elgin
    -----------------------
    IdClient: ;OR;
    Name: 97827
    Surname: 333C;John
    Adress: ;Steel;12 Orches
    City: tra Terrace
    State: ;W
    Zip: alla
    -----------------------
    IdClient: Wall
    Name: a;WA;99
    Surname: 362
    Adress: 444D;Jaime;Yorre
    City: s;87 Polk S
    State: t.
    Zip: Suite
    ----------------------

如果我没有很好地解释自己或遗漏了什么,请告诉我。

每个客户的输出必须是这样的,也许我目前的 fscanf 有点难以达到这个,因为并非每个客户的所有字段都具有相同的长度:

-----------------------
IdClient: 111A
Name: Howard
Surname: Snyder
Adress: 2732 Baker Blvd.
City: Eugene
State: OR
Zip: 97403
-----------------------

最佳答案

你可以改变你的代码

 fscanf(fp,"%4s %7s %9s %16[^\n] %11[^\n] %2s %5s",...

风格

fscanf(fp,"%4s;%7[^;];%10[^;];......

;作为格式字符串的一部分。那么它就不会被视为字符串输入的一部分。


注意:上述方法只是一种解决方法,如果格式发生变化,很容易出错。建议的通用方法以更强大的方式实现相同的目的:

  1. 使用 fgets() 读取整行
  2. 使用 strtok() 进行标记化使用 ; 作为分隔符。
  3. >>[可选]<< 如果需要,对于任何字段,使用 strtol() 将 token 转换为 int
  4. 打印结果。
  5. 继续,直到 strtok() 返回 NULL。

关于c - 从 CSV 文件读取并解决 ";"问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30400094/

相关文章:

c - 为什么在使用 Win32 ConsoleInput 函数时 scanf 不起作用?

c - 我不明白 fork() 的这个例子

c - 手动从简单的 .csv 文件将数据导入为 uint64_t

python - 如何在 Python 中将新行附加到旧的 CSV 文件?

c++ - 读取一个值并使用它而不用声明一个额外的变量,就像在 Java 的 sc.next() 中一样

c - 当数组大小固定时,sscanf 无法正确转换 float

c - 为什么编译器坚持在这里使用被调用者保存的寄存器?

c++ - 添加图像C++后发送文本到按钮

C - 如何在代码段中创建一个模式以在内存转储中识别它?

xml - 使用 XSL 解析递归 XML 以生成 csv 文件