下面的代码是一个C代码,t是一个txt文档的字符串,这里有10000+个字符串,就像我在代码中打印的一样printf("%s\n", t);
,我可以看到很多str。但是,当我使用 atof 使 t 变为 int 时,只有 72 str 变为 double,有谁知道出了什么问题?
int f;
char buf[BUFSIZ], *p, *t;
struct dataset *s;
double d;
int line;
int i;
int reading;
if (n == NULL) // n is a number from global
{
f = STDIN_FILENO;
n = "<stdin>";
}
else if (!strcmp(n, "-"))
{
f = STDIN_FILENO;
n = "<stdin>";
}
else
{
f = open(n, O_RDWR);
}
if (f == -1)
err(1, "Cannot open %s", n);
s = NewSet();
s->name = strdup(n);
while ((reading = read(f, buf, sizeof(buf) - 1)) > 0)
{
i = strlen(buf);
char *ptr = strdup(buf); //duplicate of pointed to by buf
char *ptr_copy = ptr; //copy ptr, let strsep work on prt_copy
for (i = 1, t = strsep(&ptr_copy, delim);
t != NULL && *t != '#';
i++, t = strsep(&ptr_copy, delim))
{
if (i == column)
break;
}
if (t == NULL || *t == '#')
continue;
printf("%s\n", t);
d = atof(t);
printf("%f\n", d);
free(ptr);}