C语言 错误:此处未声明(不在函数中)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13284463/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 04:22:01  来源:igfitidea点击:

error: undeclared here (not in a function)

cfile-iostruct

提问by Vbp

I am getting this error on the last line of the code, as I am trying to read data from an extra.txt file. Records are read properly from an input.txt but not sure why it is throwing error for extra file. Thanks!

我在代码的最后一行收到此错误,因为我正在尝试从 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); 

EDIT:

编辑:

extra.txt has records in this fashion:

extra.txt 有这种方式的记录:

4

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

and input.txt

和输入.txt

1

Aaaa
Aooo
1
20

采纳答案by alk

Your code needs go inside a function.

您的代码需要进入一个函数。

For example like this:

例如像这样:

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;
}