gpt4 book ai didi

c - 在 C 中读取 CSV 文件

转载 作者:太空宇宙 更新时间:2023-11-04 06:57:28 25 4
gpt4 key购买 nike

我必须在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com