2 Replies - 197 Views - Last Post: 08 May 2019 - 07:36 AM Rate Topic: -----

#1 SanBuck   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 05-May 19

Reading from file and store in a struct

Posted 08 May 2019 - 04:48 AM

Hi Can someone please help in explaining how to read from a file and declare an array of C structs and store the values in the struct.

see below code not running trying to get the Fillstruct function to work?? An explanation would be greatly appreciated.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


//function declaration

const int Size = 128;
const int size = 10;

FILE *srcFile;
FILE *outFile;

// declaration of struct 
const int NAME_SIZE = 50;
const int MAX_ITEMS = 10; 

  struct Wages
  
		{
                char name[NAME_SIZE];
				int age;
				float wage;
                 
		};
		
		typedef struct Wages; 
		
		


void findName(char argv[],char line[size][Size],int age[size],float wage[size])
{// find name in the array if not found print error message

int z;
int y;
int ret;
char str1[size];
char str2[size];
char sLine[size];


printf("Please enter a name: ");
scanf("%s", &sLine);

for(z=0; z < size ; z++)
{
	strcpy(str2,sLine);
	strcpy(str1,line[z]);

	ret=strcmp(str2,str1);
	y=z;
	
	if(ret == 0)
		break;
}
	if(ret == 0) 
	{
		printf("Name: %s, age %d, Salary %.2f\n", line[y],age[y], wage[y]);
	}	
	else
	{
		printf("Name not found");
	}	
}

int checkArg(int argc)
{
	if(argc == 1)
	{
		printf("No command line arguments given!\n");
		exit(0);
	}
	else
	{
		printf("Command line arguments given!\n");
		return(0);
	}
}

void checkOp(char *argv[])
{
	int y;
		
	srcFile = fopen (argv[1], "r");

	if(srcFile == NULL)
	{
		printf("File not found");
		exit(0);
	}
	else
	{
		printf("file opened succesfully\n");
	}
	
	}
void Fillstruct(char** argv,Wages[], int NAME_SIZE)
{ //trying to read in from file and fill structure char,int,float
	     
		FILE *srcFile;
        
	    srcFile = fopen(srcFile,"r");
		
		if(srcFile == NULL)
		{
			printf("Error failed to open file\n");
		}
		//load data into struct do I have to have the struct in the function?
		fscanf(srcFile,"%s, %d, %f", &Wages.name, &Wages.age, &Wages.wage);
		
	
		

}
void Outputfile(char* argv[],char line [size][Size],int age[size],float wage[size])
{
	
	int y;
	
	outFile = fopen ("output.txt", "w");

	if(outFile == NULL)
	{
		printf("File not opened");
	}
	else
	{
		printf("Output file opened succesfully\n");
	}

	for(y=0; y < size; y++)
	{
		fprintf(outFile," %s, %d, %.2f\n", line[y],age[y], wage[y]);
		
	}
		printf("Number of lines processed: %d", y);
}
int main(int argc, char *argv[])
{
//declare arrays as local to the main function
char line[size][Size];
int age[size];
float wage[size];

Wages [MAX_ITEMS];
			
//function calling	
checkArg(argc);	
checkOp(argv);
Fillstruct(argv,Wages,NAME_SIZE);
Outputfile(argv,line,age,wage);	

fclose(srcFile);
fclose(outFile);



return(0);
}
	



Is This A Good Question/Topic? 0
  • +

Replies To: Reading from file and store in a struct

#2 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6913
  • View blog
  • Posts: 23,497
  • Joined: 05-May 12

Re: Reading from file and store in a struct

Posted 08 May 2019 - 06:17 AM

What do you mean by "not running"? What behavior are you seeing? What behavior are you expecting to see? What have you done to try to debug the issue? Does the code even compile?
Was This Post Helpful? 0
  • +
  • -

#3 jimblumberg   User is offline

  • member icon

Reputation: 5731
  • View blog
  • Posts: 17,558
  • Joined: 25-December 09

Re: Reading from file and store in a struct

Posted 08 May 2019 - 07:36 AM

How much code did you write before you tried to compile this program?

You have so many errors it looks like you wrote the entire thing before you even tried to compile the program, not wise. You should compile early and often, usually after just a couple of lines of code. This way you will have fewer errors to deal with at a time making it easier to find and fix the problems.

Now to start in C you don't use the const keyword to create global constants, you use define.

Next using variables of any kind that only differ in the case of one character is usually unwise as it leads to confusion.

Next using global variables is usually considered a bad practice, try to eliminate the use of non-constant global variables and learn to pass variables to and from your functions.


Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1