Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C
#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]);
 }
}
Posted
Updated 19-Mar-21 23:26pm
v2
Comments
jeron1 19-Mar-21 15:32pm    
I must be missing something, the only reference to foo and bar is in the fscanf() call. Where might you be trying to print their values? and you're using data1 and data2;

column1[ num_rows ] = data1;
column2[ num_rows ] = data2;

without initializing them.
Member 15084336 19-Mar-21 15:39pm    
#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 size = 12;
for (int i = 0; i < size; ++i)
{
fscanf(fp, "%d", &dataarray[i].foo);
fscanf(fp, "%d", &dataarray[i].bar);
}

for (int i = 0; i < 12; ++i)
{
printf ("foo = %d bar = %d\n", dataarray[i].foo, dataarray[i].bar);
}


fclose(fp);
}

This was the original code but it had to be modified due to my professor not wanting the int size = 12; variable and rather representing the needed memory using the realloc function that I had here. Im trying to print the values from data_array.
Richard MacCutchan 20-Mar-21 4:33am    
The code you have posted above is wrong, as you are introducing a lot of extra variables that are not needed. Your professor's original code should work with only minor changes. For example, you could add a line at the beginning of the file that indicates how many mydata struct items need to be created. You could then have something like:
int num_rows = 0;
fscanf(fp, "%d", &num_rows);


struct mydata *dataarray = malloc(num_rows * sizeof(struct mydata));

for (int i = 0; i < num_rows; ++i)
{
    fscanf(fp, "%d", &dataarray[i].foo);
    fscanf(fp, "%d", &dataarray[i].bar);
}

You can then create a second similar loop that prints the values.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900