繁体   English   中英

我如何获取fscanf来读取.csv文件,将逗号之间以及每一行中的每一节读入字符串?

[英]How do i get fscanf to read from a .csv file, reading each section in between the commas and on each line into a string?

我正在编写一个程序以从.csv文件中读取地图,并且我想读取文件的每个部分,然后将其信息写入2D数组中的相关结构中。 我使用fscanf获取行数和列数以malloc分配结构数组,并且我可以读取第一个值直到逗号为止并对其进行处理。 但是,也可能有空白字段,它们出现在一行中,例如1只山羊,2只猫。我的程序读取1和山羊并对其进行处理,但是接下来扫描的只是猫。 我希望它阅读两个逗号,识别其中没有任何内容,然后继续阅读并阅读猫,然后再继续下一行

我不知道如何为fscanf做正确的格式规范。 目前,我有fscanf(fp, "%[^,]", animal);

/*scans file until comma, writes the scanned text into array*/

              fscanf(fp, "%[^,]", animal);
              printf("Scanned in string for y (%d) and x %d = (%s)\n", j, k, anima;l);
              if (strcmp(animal, "") != 0)
              {   
                  /*if first character is lowercase, change to upper)*/
                  if(animal[0]>90)
                  {
                      animal[0]=animal[0]-32;
                  }
                  /*Checks if item is cat goat or mouse by checking 1st letter of the scanned in array is C G or M*/                     
                  if(strncmp(animal,"C", 1) == 0)             
                  {
                      /*copies name of the animal into the struct*/
                      strcpy(animalarray[j][k].name, "Cat");

                      /*write animal name into struct*/
                      token =strtok(animal, spaceDelimeter);
                      /* The 1 is already dealt with, can be moved past*/
                      token = strtok(NULL, commaDelimeter);
                      printf("token = %s\n", token);


                      animalarray[j][k].number= atoi(token); 


                      printf("Animal is %s, number is %d\n", animalarray[j][k].name, animalarray[j][k].number);
                  }  

输入文件是~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

2,3 1只山羊,, 2只猫3只老鼠,5只山羊,

我运行时的输出是

rows  2, columns  3 
Scanned in string for row 0 and column 0 = 1 goat
token = goat
Animal is goat, number is 1
Scanned in string for row 0 and column 1 = 2
token = (null)
Segmentation fault (core dumped)

它应该是

...
Scanned in string for row 0 and column 1 = "blank"
Scanned in string for row 0 and column 1 = 2 cat
...

fscanf无法扫描空字符串,但是您可以使用返回值来检测是否扫描了所有内容:

FILE *f = fopen(filename, "r");
char buf[80];
int row = 0;
int col = 0;

while (1) {
    int n = fscanf(f, "%79[^,\n]", buf);

    if (n == EOF) break;
    if (n == 0) *buf = '\0';

    printf("[%d, %d] '%s'\n", row, col, buf);

    if (fgetc(f) == ',') {
        col++;
    } else {
        col = 0;
        row++;
    }
}

如果未设置任何内容,则n为0-在这种情况下,代码通过在第一个字符中写入空终止符来显式清除输入字符串。 通过读取逗号还是换行来检测行和列。 (下一个字符只能是逗号,换行符或文件结尾。)

另一种可能性是使用fgets逐行读取输入,然后扫描每一行。 字符串扫描函数sscanffscanf具有相同的限制,因此最好使用字符串函数,例如strchr 在这里,我使用strcspn来自<string.h> strcspn ,它对字符串中的字符进行计数,直到找到任何给定的字符或字符串的末尾。 这使其与fscanf%[^n ...]格式非常相似:

FILE *f = fopen(filename, "r");
char line[80];
int row = 0;

while (fgets(line, sizeof(line), f)) {
    int col = 0;
    int offset = 0;

    while (line[offset]) {
        int next = strcspn(line + offset, ",\n");

        printf("[%d, %d] '%.*s'\n", row, col, next, line + offset);
        offset += next + 1;

        col++;
    }

    row++;
}

同样,列和行检测是根据上下文进行的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM