我必須在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
請提供[MCVE] :強調MINIMAL – KevinDTimm
請發佈您的'listess.csv'文件的5-6首行。 –
...和'fopen(「listess.csv」,「a」);'應該是'fopen(「listess.csv」,「r」);'。 –