c - 在 C 中读取 CSV 文件

标签 c csv

我必须在 C 的控制台中读取一个文件,该文件是一个 CSV 文件。我的代码如下:

printf("Ajouter un site internet \n");
        printf("------------------------------------------\n");
        FILE * curseur = fopen("listess.csv", "a");
        SITES * pSites = calloc(100000, sizeof(SITES));
        int i = 0;
        int iSites = 0;
        int champSites = 0;
        char temp[1000];
        if (curseur != NULL) {
            char c = fgetc(curseur);
            while (c != EOF) {
                printf("%s", (pSites + iSites)->url);
                if (c != '\n' && c != ';') {
                    temp[i] = c;
                    i++;
                }

                else if (c == ';') {
                    temp[i] = '\0';
                    if (champSites == 0) {
                        strcpy((pSites + iSites)->Commune, temp);
                        champSites++;
                        i = 0;
                    }
                    else if (champSites == 1) {
                        strcpy((pSites + iSites)->Insee, temp);
                        champSites++;
                        i = 0;
                    }
                    else if (champSites == 2) {
                        strcpy((pSites + iSites)->url, temp);
                        champSites++;
                        i = 0;
                    }
                    else if (champSites == 3) {
                        strcpy((pSites + iSites)->Population, temp);
                        champSites++;
                        i = 0;
                    }
                    else if (champSites == 4) {
                        strcpy((pSites + iSites)->https, temp);
                        champSites++;
                        i = 0;
                    }
                    else if (champSites == 5) {
                        strcpy((pSites + iSites)->Serveur, temp);
                        champSites++;
                        i = 0;
                    }
                    else if (champSites == 6) {
                        strcpy((pSites + iSites)->Version, temp);
                        champSites++;
                        i = 0;
                    }
                    else if (champSites == 7) {
                        strcpy((pSites + iSites)->Application, temp);
                        champSites++;
                        i = 0;
                    }

                    else if (champSites == 8) {
                        strcpy((pSites + iSites)->VersionApplication, temp);
                        champSites++;
                        i = 0;
                    }
                    else if (champSites == 9) {
                        strcpy((pSites + iSites)->Langage, temp);
                        champSites++;
                        i = 0;
                    }
                    else if (champSites == 10) {
                        strcpy((pSites + iSites)->VersionLangage, temp);
                        champSites++;
                        i = 0;
                    }
                    else if (champSites == 11) {
                        strcpy((pSites + iSites)->Latitude, temp);
                        champSites++;
                        i = 0;
                    }
                    else if (champSites == 12) {
                        strcpy((pSites + iSites)->Longitude, temp);
                        champSites++;
                        i = 0;
                    }
                }
                else {
                    iSites++;
                }
                c = fgetc(curseur);
            }
            system("pause");
            fclose(curseur);
        }

但除了前两行,我在控制台中确实有任何结果。 该文件由我在 .h 文件中声明的 13 列组成。

csv 的前 5 行:

Commune;Code Insee;url;Population;https;Serveur;Version du serveur;Application;Version de l'application;Langage;Version du langage;Latitude;Longitude
Argentat;19010;argentat.fr;3042;non;SiteW;2;Inconnue;Inconnue;php;5.3.29;45.100801186828598;1.934640270901890
Canenx-et-Réaut;40064;mairie-info.com;175;non;SiteW;2;Inconnue;Inconnue;php;5.3.29;43.999060134922502;-0.464769980981436
Chaussan;69051;chaussan.fr;972;non;SiteW;2;Inconnue;Inconnue;Inconnue;Inconnue;45.637283899086498;4.634069843807340
Étrez;1154;etrez.fr;803;non;SiteW;2;Inconnue;Inconnue;Inconnue;Inconnue;46.338283686023097;5.192873875680920
Gray ;70279;ville-gray.fr;6016;non;SiteW;2;Inconnue;Inconnue;php;5.2.10;47.432262030641297;5.610925314619960

最佳答案

您的代码过于复杂。扔掉它并以此为基础编写新代码:

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

int main()
{
  // declare and initialize your pSites stuff here
  ....
  //

  if (curseur == NULL)
  {
    printf("Can't open file\n");
    return 1;
  }

  int linenumber = 1;
  char buffer[1000];

  while (fgets(buffer, sizeof buffer, curseur))
  {
    char *token = strtok(buffer, ";");
    printf("Line %d\n", linenumber++);

    int column = 0;
    while (token != NULL)
    {
      printf("%2d    %s\n", column, token);

      switch (column)
      {
        case 0:
          strcpy((pSites + iSites)->Commune, token);
          break;

        case 1:
          strcpy((pSites + iSites)->Insee, token);
          break;

        case 2:
         .... etc.
      }

      token = strtok(NULL, ";");
      column++;
    }

    iSites++;
  }

  fclose(curseur);
}

顺便说一句:而不是写

(pSites + iSites)->Commune

您应该编写更具可读性的变体:

pSites[iSites]->Commune

关于c - 在 C 中读取 CSV 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42418094/

相关文章:

c - 如何使用 FILE 来理解给定的代码?

c - Mac OS X 中 C 变量的奇怪地址

c - C90 中的可变长度结构

c# - 使用 Filehelpers 处理 DelimitedRecord 中的换行符

c++ - 克服下载 CSV 文件进行解析时的问题。 Linux 与 Windows 换行符

sql-server-2005 - 当某些字段有换行符时,如何从 CSV 批量插入?

c - 在 vs code 中哪里添加 -g 标志以进行调试

c - 如何在不同程序之间使用管道?

html - PowerShell 电子邮件 - 使用 HTML 发送正文中的 .csv

Python - 计算csv文件中每一列的平均值