Hello Everybody,

I need help. I have found some problem with my code and I could not seem to make it right. I need to sort data in a text file.

this is my data in the text file

Hitachi Ariff 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/8/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

and this is my code

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define ARRAY_SIZE 20
#define STRUCT_SIZE 100

typedef struct {
	char machine[ARRAY_SIZE];
	char employee[ARRAY_SIZE];
	char date[ARRAY_SIZE];
	double test_value;
	int qualification_rate;
}factory;

int main()
{
	FILE *data_txt;
	int loop;
	int sort;
	int i=0;
	int a=0;
	int b=0;
	int p[ARRAY_SIZE],q[ARRAY_SIZE],r[ARRAY_SIZE],s[ARRAY_SIZE],t[ARRAY_SIZE];

	factory factory_info[STRUCT_SIZE]={"","","",0.0,0};

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

	printf("\n");

	data_txt = fopen("data.txt","r");
	loop = fscanf(data_txt,"%s" "%s" "%s" "%lf" "%d",factory_info[i].machine,factory_info[i].employee,factory_info[i].date,
&factory_info[i].test_value,&factory_info[i].qualification_rate);

	if(data_txt == NULL)
	{
		printf("The text file is not found\n\n");
	}

	else
	{
		while(loop != EOF)
		{
			loop = fscanf(data_txt,"%s" "%s" "%s" "%lf" "%d",factory_info[i].machine,factory_info[i].employee,factory_info[i].date,
&factory_info[i].test_value,&factory_info[i].qualification_rate);
			
			if(sort == 1)
			{
				for(a=0;a<=STRUCT_SIZE-2;a++)
				{
					for(b=0;b<=STRUCT_SIZE-1;b++)
					{
						if(factory_info[a].machine<factory_info[b].machine)
						{
							strcpy(p,factory_info[a].machine);
							strcpy(factory_info[a].machine,factory_info[b].machine);
							strcpy(factory_info[b].machine,p);
							strcpy(q,factory_info[a].employee);
							strcpy(factory_info[a].employee,factory_info[b].employee);
							strcpy(factory_info[b].employee,q);
							strcpy(r,factory_info[a].date);
							strcpy(factory_info[a].date,factory_info[b].date);
							strcpy(factory_info[b].date,r);
							strcpy(s,factory_info[a].test_value);
							strcpy(factory_info[a].test_value,factory_info[b].test_value);
							strcpy(factory_info[b].test_value,s);
							strcpy(t,factory_info[a].qualification_rate);
							strcpy(factory_info[a].qualification_rate,factory_info[b].qualification_rate);
							strcpy(factory_info[b].qualification_rate,t);
						}
					}
				}
			}

			else if(sort == 2)
			{
				for(a=0;a<=STRUCT_SIZE-2;a++)
				{
					for(b=0;b<=STRUCT_SIZE-1;b++)
					{
						if(factory_info[a].employee<factory_info[b].employee)
						{
							strcpy(p,factory_info[a].machine);
							strcpy(factory_info[a].machine,factory_info[b].machine);
							strcpy(factory_info[b].machine,p);
							strcpy(q,factory_info[a].employee);
							strcpy(factory_info[a].employee,factory_info[b].employee);
							strcpy(factory_info[b].employee,q);
							strcpy(r,factory_info[a].date);
							strcpy(factory_info[a].date,factory_info[b].date);
							strcpy(factory_info[b].date,r);
							strcpy(s,factory_info[a].test_value);
							strcpy(factory_info[a].test_value,factory_info[b].test_value);
							strcpy(factory_info[b].test_value,s);
							strcpy(t,factory_info[a].qualification_rate);
							strcpy(factory_info[a].qualification_rate,factory_info[b].qualification_rate);
							strcpy(factory_info[b].qualification_rate,t);
						}
					}
				}
			}

			else if(sort == 3)
			{
				for(a=0;a<=STRUCT_SIZE-2;a++)
				{
					for(b=0;b<=STRUCT_SIZE-1;b++)
					{
						if(factory_info[a].qualification_rate<factory_info[b].qualification_rate)
						{
							strcpy(p,factory_info[a].machine);
							strcpy(factory_info[a].machine,factory_info[b].machine);
							strcpy(factory_info[b].machine,p);
							strcpy(q,factory_info[a].employee);
							strcpy(factory_info[a].employee,factory_info[b].employee);
							strcpy(factory_info[b].employee,q);
							strcpy(r,factory_info[a].date);
							strcpy(factory_info[a].date,factory_info[b].date);
							strcpy(factory_info[b].date,r);
							strcpy(s,factory_info[a].test_value);
							strcpy(factory_info[a].test_value,factory_info[b].test_value);
							strcpy(factory_info[b].test_value,s);
							strcpy(t,factory_info[a].qualification_rate);
							strcpy(factory_info[a].qualification_rate,factory_info[b].qualification_rate);
							strcpy(factory_info[b].qualification_rate,t);
						}
					}
				}
			}

			else
			{
				printf("This sort does not exist!!!\n\n");
			}

			for(i=0;i<=STRUCT_SIZE;i++)
			{
				printf("%s %s %s %g %d\n",factory_info[i].machine,factory_info[i].employee,factory_info[i].date,
factory_info[i].test_value,factory_info[i].qualification_rate);
			}
		}
	}

	printf("\n");

	fclose(data_txt);

	return 0;
}

Please help by pointing out my mistakes and help me to solve it... Really would appreciate any help i can get....

line 61 (as shown in your post). You can not copy a character array into an int array. I guess same problem with the other lines that follow that one.

line 60: >>if(factory_info[a].machine < factory_info.machine)
You can't compare two C character arrays like that -- use strcmp() instead if( strcmp( factory_info[a].machine, factory_info[b].machine) < 0) After that, all you have to do is copy the entire structure -- not necessary to copy each field

if( strcmp( factory_info[a].machine, factory_info[b].machine) < 0)
{
    factory temp;
    temp = factory_info[a];
    factory_info[a] = factory_info[b];
    factory_info[b] = temp; 
}

You can also use memcpy() to copy those structures.

I have made some adjustments to my code but I still have problem sorting it.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define ARRAY_SIZE 25
#define STRUCT_SIZE 25

typedef struct{
    char machine[ARRAY_SIZE];
    char employee[ARRAY_SIZE];
    char date[ARRAY_SIZE];
    char test_value[ARRAY_SIZE];
    int qualification_rate;
}factory;

int main()
{
    FILE *data_txt;
    int loop;
    int sort;
    int i=0;
    int a=0;
    int b=0;
    char temp[1000];
    int temp2;

    factory factory_info[STRUCT_SIZE]={"","","","",0};

    data_txt = fopen("data.txt","r");
    loop = fscanf(data_txt,"%s" "%s" "%s" "%s" "%d",factory_info[i].machine,factory_info[i].employee,factory_info[i].date,
factory_info[i].test_value,&factory_info[i].qualification_rate);

    printf("Factory Information before sorting\n");
    printf("**********************************\n");

    while(loop!=EOF)
    {
        printf("%s %s %s %s %d\n",factory_info[i].machine,factory_info[i].employee,factory_info[i].date,
factory_info[i].test_value,factory_info[i].qualification_rate);
        loop = fscanf(data_txt,"%s" "%s" "%s" "%s" "%d",factory_info[i].machine,factory_info[i].employee,factory_info[i].date,
factory_info[i].test_value,&factory_info[i].qualification_rate);
    }

    printf("\n");

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

    printf("\n");

    printf("Factory Information after sorting\n");
    printf("*********************************\n");

    if(sort == 1)
    {
        for(a=0;a<STRUCT_SIZE;a++)
        {
            for(b=0;b<STRUCT_SIZE;b++)
            {
                if(strcmp(factory_info[a].machine,factory_info[b].machine)<0)
                {
                    strcpy(temp,factory_info[a].machine);
                    strcpy(factory_info[a].machine,factory_info[b].machine);
                    strcpy(factory_info[b].machine,temp);
                    strcpy(temp,factory_info[a].employee);
                    strcpy(factory_info[a].employee,factory_info[b].employee);
                    strcpy(factory_info[b].employee,temp);
                    strcpy(temp,factory_info[a].date);
                    strcpy(factory_info[a].date,factory_info[b].date);
                    strcpy(factory_info[b].date,temp);
                    strcpy(temp,factory_info[a].test_value);
                    strcpy(factory_info[a].test_value,factory_info[b].test_value);
                    strcpy(factory_info[b].test_value,temp);
                    temp2=factory_info[a].qualification_rate;
                    factory_info[a].qualification_rate=factory_info[b].qualification_rate;
                    factory_info[b].qualification_rate=temp2;
                }
            }
        }   
        for(i=0;i<STRUCT_SIZE;i++)
        {
            printf("%s %s %s %s %d\n",factory_info[i].machine,factory_info[i].employee,factory_info[i].date,
factory_info[i].test_value,factory_info[i].qualification_rate);
        }
    }
    else if(sort == 2)
    {
        for(a=0;a<STRUCT_SIZE;a++)
        {
            for(b=0;b<STRUCT_SIZE;b++)
            {
                if(strcmp(factory_info[a].employee,factory_info[b].employee)<0)
                {
                    strcpy(temp,factory_info[a].machine);
                    strcpy(factory_info[a].machine,factory_info[b].machine);
                    strcpy(factory_info[b].machine,temp);
                    strcpy(temp,factory_info[a].employee);
                    strcpy(factory_info[a].employee,factory_info[b].employee);
                    strcpy(factory_info[b].employee,temp);
                    strcpy(temp,factory_info[a].date);
                    strcpy(factory_info[a].date,factory_info[b].date);
                    strcpy(factory_info[b].date,temp);
                    strcpy(temp,factory_info[a].test_value);
                    strcpy(factory_info[a].test_value,factory_info[b].test_value);
                    strcpy(factory_info[b].test_value,temp);
                    temp2=factory_info[a].qualification_rate;
                    factory_info[a].qualification_rate=factory_info[b].qualification_rate;
                    factory_info[b].qualification_rate=temp2;
                }
            }
        }   
        for(i=0;i<STRUCT_SIZE;i++)
        {
            printf("%s %s %s %s %d\n",factory_info[i].machine,factory_info[i].employee,factory_info[i].date,
factory_info[i].test_value,factory_info[i].qualification_rate);
        }
    }
    else if(sort == 3)
    {
        for(a=0;a<STRUCT_SIZE;a++)
        {
            for(b=0;b<STRUCT_SIZE;b++)
            {
                if(factory_info[a].qualification_rate<factory_info[b].qualification_rate)
                {
                    strcpy(temp,factory_info[a].machine);
                    strcpy(factory_info[a].machine,factory_info[b].machine);
                    strcpy(factory_info[b].machine,temp);
                    strcpy(temp,factory_info[a].employee);
                    strcpy(factory_info[a].employee,factory_info[b].employee);
                    strcpy(factory_info[b].employee,temp);
                    strcpy(temp,factory_info[a].date);
                    strcpy(factory_info[a].date,factory_info[b].date);
                    strcpy(factory_info[b].date,temp);
                    strcpy(temp,factory_info[a].test_value);
                    strcpy(factory_info[a].test_value,factory_info[b].test_value);
                    strcpy(factory_info[b].test_value,temp);
                    temp2=factory_info[a].qualification_rate;
                    factory_info[a].qualification_rate=factory_info[b].qualification_rate;
                    factory_info[b].qualification_rate=temp2;
                }
            }
        }   
        for(i=0;i<STRUCT_SIZE;i++)
        {
            printf("%s %s %s %s %d\n",factory_info[i].machine,factory_info[i].employee,factory_info[i].date,
factory_info[i].test_value,factory_info[i].qualification_rate);
        }
    }
    else
    {
        printf("\nThis sort does not exist!!!\n\n");
    }

    printf("\n");

    fclose(data_txt);

    return 0;
}

You are missing the "i" increment in the while loop...

while(loop!=EOF)
	{
		printf("%s %s %s %s %d\n",factory_info[i].machine,factory_info[i].employee,factory_info[i].date,
factory_info[i].test_value,factory_info[i].qualification_rate);

/* You must increment i */
i++;

		loop = fscanf(data_txt,"%s" "%s" "%s" "%s" "%d",factory_info[i].machine,factory_info[i].employee,factory_info[i].date,
factory_info[i].test_value,&factory_info[i].qualification_rate);
	}

To improve the code...

You are using STRUCT_SIZE (which is #defined to probably the max no of records) in the for loop... if the no of records in the text file is not equal to STRUCT_SIZE, then for loop, simply loops unnecessarily... So find the no of records and then use that count for the loop...

If you are using a structure to write/read to/from a file, then use fread and fwrite... these functions come more handy... when you write, just populate the structure and write it... and when you read just pass the structure, it will read from the file and assign the values to the proper structure variables... so you dont need to use scanf, fscanf etc and can avoid errors in using the format specifier...

eg:

typedef struct _employee{
	int iNum;
	char acName[20];
}Employee, *Employee;
#define SIZEOFEMP sizeof(Employee)

...

FILE *pFile;
Employee sEmpStr;
sEmpStr.iNum = 10;
strcpy(sEmpStr.acName, "Ahamed");

/* Writing */
pFile = fopen("myfile.txt","a+");
if(pFile != NULL){
        /* You might want to check the return value of fwrite for robust code */
	fwrite(&(sEmpStr),SIZEOFEMP,1,pFile);
	fclose(pFile);
}
else{
	printf("\n Could not open file for writing...");
	return 1;
}

...

/* Reading from the file */
pFile = fopen("myfile.txt","r");
if(pFile != NULL){
        /* You might want to check the return value of fread for robust code */
	fread(&(sEmpStr),SIZEOFEMP,1,pFile);
	fclose(pFile);
}
else{
	printf("\n Could not open file for reading...");
	return 1;
}
/* Print the calues directly */
printf("\n Number : %d",sEmpStr.iNum);
printf("\n Name   : %s",sEmpStr.acName);

...

Use memcpy for copying structure or blocks of data... You can avoid too many line if you use this...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.