文件数据以“;”分割然后在 C 中通过 ","
File data split by ";" and then by "," in C
基本上我需要从文件中读取输入,文件中的数据如下所示:
A422TCRE234VD3KJ349D;2000,Gasoleo;Vermelho,;17,200,Platinum;17,200,45;
3KJ349DA422TCRE234VD;,diesel;Azul,Minivan;17,200,45;10,20,30;
DA422TC3KJ349RE234VD;,;Vermelho,;,,;,,;
现在我接下来需要做的是读取这些数据并用“;”分隔。我正在使用 sscanf,像这样:
sscanf(line," %[^;];%[^;];%[^;];%[^;];%[^;]", campos[0],campos[1], campos[2] ,campos[3],campos[4]);
“行”变量保存从文件中读取的整行。到目前为止一切都很好。现在我需要用“,”拆分这些数组 (campos[x]) 中的每一个,因为我需要将每个数据保存在我创建的不同结构中。我试图通过使用辅助数组“输出”来实现这一点,我在其中组合了所有先前的字段,如下所示:
strcpy(output,campos[1]);
strcat(output,",");
strcat(output,campos[2]);
strcat(output,",");
strcat(output,campos[3]);
strcat(output,",");
strcat(output,campos[4]);
然后我再次使用sscanf尝试拆分它们:
sscanf(output, " %[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,]",helper[0],helper[1],helper[2],helper[3],helper[4],helper[5],helper[6],helper[7],helper[8], helper[9]);
虽然幸运,但它不起作用,可能是因为正如您在输入文件中看到的那样,一些属性是空的,问题是我需要它们保持为空,因为之后我需要向用户格式与输入文件相同。
我试过使用 strtok()
,这里的代码有很多变体,但似乎没有任何效果。
只是为了让您有一个更好的主意,我将在此处写下我当前的输出:
puts(output); -> 2000,Gasoleo,Vermelho,,17,200,Platinum,17,200,45
,diesel,Azul,Minivan,17,200,45,10,20,30
,,Vermelho,,,,,,,
printf("%s,%s\n", helper[0],helper[1]); -> 2000,Gasole
,
,
另外我可以给出一个更好的视角,这个文件代表一个汽车工厂的工单,所以每一行如下:
Word Order Id;Horsepower,Fuel Type;Colour,Model;Diameter,Width,Colour;Diameter,Width,Height;
Example: Horsepower and Fuel Type are engine attributes, each ";" separates between car parts.
我该如何解决这个问题?
由于您要解析的字段之间没有空格,因此很难使用 sscanf
或 strtok
。
也许自定义算法是最好的方法:
int main() {
FILE *f = fopen("file.txt", "r");
if (f == NULL) {
perror("File");
}
else {
size_t i = 0, j = 0, it = 0;
char line[100];
char helper[12][100];
while (fgets(line, sizeof(line), f)) {
int c;
i = j = it = 0;
while ((c = line[it]) != '\n' && c != '[=10=]'){ //read and assign cycle
if (c != ';' && c != ',') { //if character is not, or ; assign
helper[i][j++] = c;
}
else {
helper[i][j] = '[=10=]'; //else terminate string
i++; //go to next helper line
j = 0; //reset column iterator
}
it++;
}
for (size_t a = 1; a < i; a++){ //test print
printf(",%s", helper[a]);
}
printf("\n");
}
}
}
输出:
,2000,Gasoleo,Vermelho,,17,200,Platinum,17,200,45
,,diesel,Azul,Minivan,17,200,45,10,20,30
,,,Vermelho,,,,,,,
根据要求。
基本上我需要从文件中读取输入,文件中的数据如下所示:
A422TCRE234VD3KJ349D;2000,Gasoleo;Vermelho,;17,200,Platinum;17,200,45;
3KJ349DA422TCRE234VD;,diesel;Azul,Minivan;17,200,45;10,20,30;
DA422TC3KJ349RE234VD;,;Vermelho,;,,;,,;
现在我接下来需要做的是读取这些数据并用“;”分隔。我正在使用 sscanf,像这样:
sscanf(line," %[^;];%[^;];%[^;];%[^;];%[^;]", campos[0],campos[1], campos[2] ,campos[3],campos[4]);
“行”变量保存从文件中读取的整行。到目前为止一切都很好。现在我需要用“,”拆分这些数组 (campos[x]) 中的每一个,因为我需要将每个数据保存在我创建的不同结构中。我试图通过使用辅助数组“输出”来实现这一点,我在其中组合了所有先前的字段,如下所示:
strcpy(output,campos[1]);
strcat(output,",");
strcat(output,campos[2]);
strcat(output,",");
strcat(output,campos[3]);
strcat(output,",");
strcat(output,campos[4]);
然后我再次使用sscanf尝试拆分它们:
sscanf(output, " %[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,]",helper[0],helper[1],helper[2],helper[3],helper[4],helper[5],helper[6],helper[7],helper[8], helper[9]);
虽然幸运,但它不起作用,可能是因为正如您在输入文件中看到的那样,一些属性是空的,问题是我需要它们保持为空,因为之后我需要向用户格式与输入文件相同。
我试过使用 strtok()
,这里的代码有很多变体,但似乎没有任何效果。
只是为了让您有一个更好的主意,我将在此处写下我当前的输出:
puts(output); -> 2000,Gasoleo,Vermelho,,17,200,Platinum,17,200,45
,diesel,Azul,Minivan,17,200,45,10,20,30
,,Vermelho,,,,,,,
printf("%s,%s\n", helper[0],helper[1]); -> 2000,Gasole
,
,
另外我可以给出一个更好的视角,这个文件代表一个汽车工厂的工单,所以每一行如下:
Word Order Id;Horsepower,Fuel Type;Colour,Model;Diameter,Width,Colour;Diameter,Width,Height;
Example: Horsepower and Fuel Type are engine attributes, each ";" separates between car parts.
我该如何解决这个问题?
由于您要解析的字段之间没有空格,因此很难使用 sscanf
或 strtok
。
也许自定义算法是最好的方法:
int main() {
FILE *f = fopen("file.txt", "r");
if (f == NULL) {
perror("File");
}
else {
size_t i = 0, j = 0, it = 0;
char line[100];
char helper[12][100];
while (fgets(line, sizeof(line), f)) {
int c;
i = j = it = 0;
while ((c = line[it]) != '\n' && c != '[=10=]'){ //read and assign cycle
if (c != ';' && c != ',') { //if character is not, or ; assign
helper[i][j++] = c;
}
else {
helper[i][j] = '[=10=]'; //else terminate string
i++; //go to next helper line
j = 0; //reset column iterator
}
it++;
}
for (size_t a = 1; a < i; a++){ //test print
printf(",%s", helper[a]);
}
printf("\n");
}
}
}
输出:
,2000,Gasoleo,Vermelho,,17,200,Platinum,17,200,45
,,diesel,Azul,Minivan,17,200,45,10,20,30
,,,Vermelho,,,,,,,
根据要求。