gpt4 book ai didi

c - 错误 : undeclared here (not in a function)

转载 作者:太空宇宙 更新时间:2023-11-03 23:53:49 25 4
gpt4 key购买 nike

我在代码的最后一行收到此错误,因为我正在尝试从 extra.txt 文件中读取数据。从 input.txt 中正确读取记录,但不确定为什么它会为额外文件抛出错误。谢谢!

typedef struct {
char* fname;
char* lname;
int id;
int age;
} data;

typedef struct {
data** array;
int len;
int cap;
}vector;

vector* vector_read(FILE* in_file)
{
int i;
vector *v = (vector*)malloc(sizeof(vector));
fscanf(in_file,"%d",&v->len);
if(in_file=NULL)
{
return NULL;
}
printf("%d",v->len);
data** array = (data**)malloc(sizeof(data*)*(v->len));
v->array = array;
data *temp;

for(i=0;i<(v->len);i++)
{
temp = data_read(in_file);
v->array[i] = temp;
}

return v;
}

vector *v = vector_read(input);
printf( "initial state of vector v\n");
vector_print(v);
vector *v_add = vector_read(extra);

编辑:

extra.txt 以这种方式记录:

4

Barak Obama 101 50
Joe Biden 102 55
Joe Plumber 10293 45
Wayne Gretzky 99 56

和输入.txt

1

Aaaa
Aooo
1
20

最佳答案

您的代码需要放在一个函数中。

例如像这样:

int read_input_and_extra(FILE * input, FILE * extra)
{
vector *v = vector_read(input);
if (!v)
return -1;

printf( "initial state of vector v\n");
vector_print(v);

vector *v_add = vector_read(extra);
if (!v_add)
return -2;

return 0;
}

关于c - 错误 : undeclared here (not in a function),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13284463/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com