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

时间:2012-11-08 07:31:41

标签: c file-io struct

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

和input.txt

1

Aaaa
Aooo
1
20

1 个答案:

答案 0 :(得分:1)

您的代码需要进入函数。

例如:

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