我有一个 csv 文件,看起来像这样:
- 1;53453;45847865
- 1;37567;53687686
- .
- .
- .
- . n. 1;999768;5645644
我想打开文件,阅读它,然后将每一行拆分为 3 个标记,这些标记将与分号隔开....
例如
1;35435;75675
token1 = 1;
token2 = 35435;
token3 = 75675;
我拥有的代码是我打开并读取文件的主要代码和我手动获取字符串并将其拆分的函数...
我想知道是否有更简单的方法来实现这一点以及跳过文件第一行的方法!!!!!!
#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);
}