【发布时间】:2018-11-16 19:48:52
【问题描述】:
我正在尝试解析 CSV 文件的一行并提取我想要的所需值。但是,我的函数正在切断字符串中的最后一个字符,我不知道为什么。我认为这与我分配空终止符的位置有关,但改变它并没有帮助。任何帮助,将不胜感激。谢谢!
char* findKey(char lineBuffer[], int columnNumber ){
char tempArray[strlen(lineBuffer)+2];
int commasCounted = 0;
int i =0;
for(i = 0; i < strlen(lineBuffer) - 1; i++){
if (commasCounted == columnNumber){
commasCounted = i;
break;
}
if (lineBuffer[i] == '\"'){
i++;
while(lineBuffer[i] && lineBuffer[i] != '\"'){
i++;
}
}
if (lineBuffer[i] == ','){
commasCounted++;
}
}
if(lineBuffer[commasCounted] == ','){
tempArray[0] = '0';
tempArray[1] = '0';
tempArray[2] = '0';
tempArray[3] = '0';
tempArray[4] = '\0';
}else{
int j = 0;
for(i = commasCounted; i < strlen(lineBuffer) - 1; i++){
if(lineBuffer[i] == '\"'){
i++;
while(lineBuffer[i] && lineBuffer[i] != '\"'){
tempArray[j] = lineBuffer[i];
i++;
j++;
}
break;
}else if(lineBuffer[i] == ','){
break;
}else
tempArray[j] = lineBuffer[i];
j++;
}
tempArray[j] = '\0';
}
char* tempString = strtok(tempArray, "\n");
//printf("tempString before returning in findKey: %s\n", tempString); //testing
return tempString;
}
CSV 文件中的某些列可以用引号括起来,这就是为什么要在其中检查引号的原因。这将传递要检查的字符串以及包含信息的列。因此,例如: 为 lineBuffer 传入这个:
30,beforeyoustartedgackinyouusedtohaveabrainbutnowyoudontgeteventhesimpliesofthingsdrawalittlepictureevenusemyhandstrytoexplainbutyoujustdontunderstandoh
列编号为 1
返回结果:
beforeyoustartedgackinyouusedtohaveabrainbutnowyoudontgeteventhesimpliesofthingsdrawalittlepictureevenusemyhandstrytoexplainbutyoujustdontunderstando
【问题讨论】:
-
您正在从临时/本地字符串中返回一个标记化的字符串:这是未定义的行为
-
您这样做是为了学习,还是实际应用的一部分?对于后者,您最好使用预先编写的 CSV 库。
-
@Barmar 学习
-
您是否尝试过复制该行:
char* tempString = strdup(strtok(tempArray, "\n")); -
我以为这是 python,我现在删除的第一条评论是“你不能使用
csv模块吗?哈哈……这里可能有很好的例子。研究它们。跨度>