I will need to complete the below
program to read each line of the file into an element of data_array where each element is a
data struct. The two numbers in the file will go into the foo and bar fields of the struct.
Finally, I then need to add the code to print out the contents of the data_array structs. I have an issue, as when I run my code I get Zeroes and none of the values that I have in my data array with none of my foo and bar fields.
What I have tried:
#include <stdio.h>
#include <stdlib.h>
struct mydata
{
int foo;
int bar;
};
int main(int argc, char const *argv[])
{
if(argc != 2)
{
printf("%s filename\n",argv[0]);
exit(0);
}
FILE *fp = fopen(argv[1], "r");
if(fp == NULL)
{
perror("Could not open file");
exit(0);
}
int num_rows = 0;
struct mydata *dataarray = malloc(12*sizeof(struct mydata));
int num_columns = 2;
int *column1 = malloc(10 * sizeof(int));
int *column2 = malloc(10 * sizeof(int));
int retval;
int data1;
int data2;
while (1)
{
int retval = fscanf(fp,"%d %d\n", &dataarray.foo,&dataarray.bar);
if(retval==EOF)
break;
column1[ num_rows ] = data1;
column2[ num_rows ] = data2;
num_rows++;
if(num_rows%10==0)
{
column1 = realloc( column1, (num_rows + 10)*sizeof(int) );
column2 = realloc( column2, (num_rows + 10)*sizeof(int) );
}
}
int i;
for (i=0; i<num_rows; i++) {
printf("%d ", column1[i]);
printf("%d\n", column2[i]);
}
}