How can I manage and a csv file in c?

蹲街弑〆低调 提交于 2019-12-13 09:45:44

问题


i have a csv file and which look like this :

  1. 1;53453;45847865
  2. 1;37567;53687686
  3. .
  4. .
  5. .
  6. . n. 1;999768;5645644

and i want to open the file , read it and then split each line to 3 tokens which will be seperated from the semicolon ....

e.g

1;35435;75675

 token1 = 1;
 token2 = 35435;
 token3 = 75675;

the code that i have is a main which i open and read the file and a function which i take manually a string of characters and split it ...

I want to know if there is an easier way to implement this and a way to skip the first line of the file !!!!!

    #include<stdio.h>

    int main(){

    char c;
    FILE *fp;
    char line;



    float x;
    float y;

    if((fp=fopen("test.csv","r"))==NULL){
            printf("cannot open the file");
    }else{

      do{

    c =  fscanf (fp, "%c", &line);
    printf("%c" , line);



     }while(c!=EOF);    
     fclose(fp);
    }

  }

________________________________-

         int TokenX(char line) {

char *id;
char *x;
char *y;

char line[] = "1;345345;765767";
char *search = ";";


     id = strtok(line , search);
    // printf(id);

     x = strtok(NULL , search);
     printf(x);

     y = strtok(NULL , search);
     printf(y);



    return(x);  

    }

    int TokenY(char line) {

  char *id;
  char *x;
  char *y;

  char line[] = "1;345345;765767";
  char *search = ";";


     id = strtok(line , search);
    // printf(id);

     x = strtok(NULL , search);
     printf(x);

     y = strtok(NULL , search);
     printf(y);



    return(y);  

    }

回答1:


Trivial state machine:

#include <stdio.h>

int main(void)
{
char token1[123];
char token2[123];
char token3[123];
unsigned nline;
int ch,state;
char *dst=token1;

nline=0;
for (state=0; state >=0; ) {
        ch = getc(stdin);
        // fprintf(stderr, "Line=%u State=%d Ch=%c\n", nline, state, ch);
        switch(ch) {
        case EOF :
                state = -1; break;
        case ';' :
                if (dst) *dst = 0;
                switch(state++) {
                case 0: dst = token2; break;
                case 1: dst = token3; break;
                default: dst = NULL; break;
                }
                break;
        case '\n' :
                nline++;
                if (dst) *dst = 0;
                // if you want to skip the first line
                if (state>=2 && nline> 1) printf("%s:%s:%s\n"
                        , token1, token2 ,token3);
                state =0; dst = token1; break;
        default: *dst++ = ch; break;
                }
        }

return 0;
}



回答2:


You can use fscanf() with while. each iteration in the while loop will read a line from the file.

int token1, token2, token3;
while(fscanf(fp, " %*d . %d ; %d ; %d", &token1, &token2, &token3)>0)
{

  printf("%d  %d   %d\n",token1, token2, token3);
}

if you wantto skip the first line then add an integr parametr and initiate to 0. When you enter in the while loop check if it's equal to 0

int token1, token2, token3;
int check = 0; 
while(fscanf(fp, " %*d . %d ; %d ; %d", &token1, &token2, &token3)>0)
{
  if(!check) {check++; continue;}
  printf("%d  %d   %d\n",token1, token2, token3);
}
  1. If the string format of each line in your csv file looks like that "1. 25;78;547" than use the follwing format " %*d . %d ; %d ; %d" in the fscanf()
  2. If the string format of each line in your csv file looks like that "25;78;547" than you have to use the follwing format " %d ; %d ; %d" in the fscanf()


来源:https://stackoverflow.com/questions/15668787/how-can-i-manage-and-a-csv-file-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!