Scan file contents into an array of a structure.

Posted by ZaZu on Stack Overflow See other posts from Stack Overflow or by ZaZu
Published on 2010-05-08T21:18:34Z Indexed on 2010/05/08 21:48 UTC
Read the original article Hit count: 130

Filed under:
|
|

Hello,

I have a structure in my program that contains a particular array. I want to scan a random file with numbers and put the contents into that array.

This is my code : ( NOTE : This is a sample from a bigger program, so I need the structure and arrays as declared )

The contents of the file are basically : 5 4 3 2 5 3 4 2

#include<stdio.h>

#define first 500
#define sec 500


struct trial{
  int f;
  int r;
  float what[first][sec];
};

int trialtest(trial *test);

main(){
  trial test;
  trialtest(&test);
}

int trialtest(trial *test){
  int z,x,i;
  FILE *fin;
  fin=fopen("randomfile.txt","r"); 
  for(i=0;i<5;i++){
      fscanf(fin,"%5.2f\t",(*test).what[z][x]);
    }
  fclose(fin);
  return 0;
}

But the problem is, whenever this I run this code, I get this error : alt text

(25) : warning 508 - Data of type 'double' supplied where a pointer is required

I tried adding

do{
   for(i=0;i<5;i++){
      q=fscanf(fin,"%5.2f\t",(*test).what[z][x]);
    }
  }while(q!=EOF);

But that didnt work either, it gives the same error.

Does anyone have a solution to this problem ?

© Stack Overflow or respective owner

Related posts about c

    Related posts about file-io