0 Replies - 2177 Views - Last Post: 23 September 2008 - 10:36 PM Rate Topic: -----

#1 Nisha Rahmat  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 23-September 08

Need help in reading data in text file into an array and sort it

Posted 23 September 2008 - 10:36 PM

I have some problem reading my data from text file into an array. I just have no idea how to do it. My task is to write a C program that uses an array of structures to store related data. I have to create a function that reads each line of the data.txt and stores the corresponding data into the structure. Once all the data has been stored, prompt the user with the option to sort the data based on the following

Machine
Employee
Qualification Rate

I can read the data in the text file but I don't know how to do it in array. I also have no idea how to sort strings. Please help me.

This is my program:
#include<stdio.h> 
#include<stdlib.h>
int main()
{
	FILE *datatxt;
	FILE *data1txt;

	int results;
	int sort;
	char machine[20];
	char employee[20];
	char date[20];
	double test_value;
	int qualification_rate;

	datatxt = fopen("data.txt","r");
	data1txt = fopen("data1.txt","w");
	results = fscanf(datatxt,"%s" "%s" "%s" "%lf" "%d",machine,employee,date,&test_value,&qualification_rate);

	printf("Menu\n");
	printf("****\n");
	printf("1.Machine\n");
	printf("2.Employee\n");
	printf("3.Qualification Rate\n");
	printf("Choose the data you want to sort : ");
	scanf_s("%d",&sort);

	printf("\n");

	if(datatxt == NULL)
	{
		printf("data.txt not found\n\n");
	}

	else
	{
		while(results != EOF)
		{
			printf("%s %s %s %g %d\n",machine,employee,date,test_value,qualification_rate);
			fprintf(data1txt,"%s %s %s %g %d\n",machine,employee,date,test_value,qualification_rate);
			results = fscanf(datatxt,"%s" "%s" "%s" "%lf" "%d",machine,employee,date,&test_value,&qualification_rate);
		}
	}

	printf("\n");

	fclose(datatxt);
	fclose(data1txt);

	return 0;
}


and this is the data.txt that i have to sort:
Machine Employee Date Test Value Qualification Rate
Hitachi Arifff 12/5/08 12.567 2
Motorola Raju 12/5/08 15.658 2
Siemens Daniel 14/5/08 16.747 2
Borsch Matthew 21/5/08 25.536 3
LKT Ali 25/5/08 30.224 4
Motorola Bahrom 26/5/08 9.452 1
Siemens Suman 27/8/08 21.453 2
VDO William 30/8/08 50.252 6
Motorola Rahman 31/8/08 122.134 14
Siemens Edmund 27/5/08 155.145 16
Motorola Esther 31/8/08 67.353 7
Borsch Daniel 23/6/08 87.553 9
Hitachi Matthew 24/7/08 79.646 8
VDO Francis 25/7/08 90.224 10

MOD EDIT: Please :code:
Thanks, gabehabe :)

Attached File(s)

  • Attached File  data.txt (453bytes)
    Number of downloads: 78


Is This A Good Question/Topic? 0
  • +

Page 1 of 1