求问这个错误代码的原因
求问为什么会有这样的七个错误代码啊{:10_266:} {:10_266:} 代码目的是要用C语言把CSV文件导入到二维数组里面{:10_266:} {:10_266:} 谢谢啦#include <stdio.h>
#include <string.h>
char *trim(char *str)
{
char *p = str;
while (*p == ' ' || *p == '\t' || *p == '\r' || *p == '\n')
p++;
str = p;
p = str + strlen(str) - 1;
while (*p == ' ' || *p == '\t' || *p == '\r' || *p == '\n')
--p;
*(p + 1) = '\0';
return str;
}
int main()
{
FILE *fp = fopen("test.csv", "r");
if (fp == NULL) {
return -1;
}
char line;
while (fgets(line, sizeof(line), fp)) {
//printf("%s", line);
char *save_ptr;
char *name = strtok_r(line, ",", &save_ptr);
if (name == NULL) {
return -1;
}
char *age = strtok_r(NULL, ",", &save_ptr);
char *birthday = strtok_r(NULL, ",", &save_ptr);
printf("%s\t%s\t%s\n", trim(name), trim(age), trim(birthday));
}
return 0;
} 本帖最后由 bin554385863 于 2019-11-29 00:27 编辑
while (fgets(line, sizeof(line), fp)) {
//printf("%s", line);
char *save_ptr;-------请问这个指针指向哪里?
char *name = strtok_r(line, ",", &save_ptr);--------对一个野指针取地址,你是什么逻辑?
if (name == NULL) {
return -1;
}
char *age = strtok_r(NULL, ",", &save_ptr);
char *birthday = strtok_r(NULL, ",", &save_ptr);
printf("%s\t%s\t%s\n", trim(name), trim(age), trim(birthday));
}
return 0;
}
=============================
别的错误我没看出来
但是你后面的代码居然使用野指针,,,,,,还对野指针取地址,,,不得不对你写一个服字 bin554385863 发表于 2019-11-29 00:23
while (fgets(line, sizeof(line), fp)) {
//printf("%s", line);
...
那要怎么修改啊{:10_266:}是我漏了什么吗?新手研究ing{:10_266:}
页:
[1]