如何管理和使用C csv文件? [英] How can I manage and a csv file in c?

查看:174
本文介绍了如何管理和使用C csv文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个CSV文件,其中是这样的:


  1. 1; 53453; 45847865

  2. 1; 37567; 53687686





  3. ñ。 1; 999768; 5645644

和我要打开的文件,读取它,然后每条线分割为3令牌这将从分号分隔....

例如

1; 354​​35; 75675

  TOKEN1 = 1;
 token2 = 35435;
 token3 = 75675;

在code,我有一个主要的,我打开和读取文件和功能,我手动采取字符的字符串,分裂它...

我想知道是否有实现这种更简单的方法和一种方式跳过该文件的第一行!!!!!

 #包括LT&;&stdio.h中GT;    诠释主(){    焦炭℃;
    FILE * FP;
    焦线;    浮X;
    浮ÿ;    如果((FP = FOPEN(test.csv,R))== NULL){
            的printf(不能打开文件);
    }其他{      做{    C =的fscanf(FP,%C,&安培;线);
    的printf(%C,线);     }而(!C = EOF);
     FCLOSE(FP);
    }  }

________________________________-

  INT TokenX(焦线){字符* ID;
字符* X;
字符* Y;焦线[] =1; 345345; 765767;
字符*搜索=;
     ID = strtok的(行,搜索);
    //的printf(ID);     X =的strtok(NULL,搜索);
     的printf(X);     Y =的strtok(NULL,搜索);
     的printf(Y);    回报(X);    }


  INT TokenY(焦线){  字符* ID;
  字符* X;
  字符* Y;  焦线[] =1; 345345; 765767;
  字符*搜索=;
     ID = strtok的(行,搜索);
    //的printf(ID);     X =的strtok(NULL,搜索);
     的printf(X);     Y =的strtok(NULL,搜索);
     的printf(Y);    返回(Y);    }


解决方案

您可以使用的fscanf(),而。在while循环每次迭代将读取文件中的一行。

  INT TOKEN1,token2,token3;
而(的fscanf(FP,%* D%d个;%d个;%D,&安培; TOKEN1,&安培; token2,&安培; token3)0)
{  的printf(%d个%D \\ n,TOKEN1,token2,token3);
}

如果您wantto跳过前行,然后添加一个INTEGR parametr,当你在while循环检查进入启动为0,如果是等于0

  INT TOKEN1,token2,token3;
INT检查= 0;
而(的fscanf(FP,%* D%d个;%d个;%D,&安培; TOKEN1,&安培; token2,&安培; token3)0)
{
  如果(检查!){检查++;继续;}
  的printf(%d个%D \\ n,TOKEN1,token2,token3);
}


  1. 如果每一行的CSV文件中的字符串格式看起来像
    1月25日; 78; 547不是使用follwing格式%* D%d个;%d个;%D
    的fscanf()

  2. 如果每一行的CSV文件中的字符串格式看起来像
    25; 78; 547比你必须使用follwing格式%d个;%d个;%D
    的fscanf()

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);  

    }

解决方案

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()

这篇关于如何管理和使用C csv文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆