Reputation: 1
I have to read a file in the console in C, the file is a CSV file. My code is the following :
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);
}
But I do have any result in the console except the two first lines. The file is composed of 13 columns that I declared in a .h file.
The 5 first lines of the csv:
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
Upvotes: 0
Views: 276
Reputation: 50775
Your code is overly complicated. Throw it away and base your new code on this:
#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);
}
BTW: instead of writing
(pSites + iSites)->Commune
you should write the more readable variant:
pSites[iSites]->Commune
Upvotes: 2
Reputation: 399833
This:
if (champSites == 0) {
strcpy((pSites + iSites)->Commune, temp);
champSites++;
i = 0;
}
else if (champSites == 0) {
strcpy((pSites + iSites)->Insee, temp);
champSites++;
i = 0;
}
makes no sense, note that both conditions are else if(champSites == 0)
which will clearly mean that most of the code is dead (won't ever execute).
Should perhaps be if 0 ... else if 1 ... else if 2
and so on.
Upvotes: 1