I have to create a database with the data stored in a .txt file. Each record is for a company with the following fields:
Name, Place, East(km), North(km). Each record from the file needs to be read into a struct array. I have managed to read the records into a struct array but I need to create several other functions:
Add_Company() - To add a company to the struct array
Read_Companies() - Read each company record by calling the function Add_Company
List_Distance() - Distance Between two specified companies
Delete_Company() - Deletes a company record
A user menu would be needed calling the specific function when required. When the user quits the file must be updated.
I have posted before about this and did not get any straight answer or I did not really understand, so please could someone help?
Here is my code:
#include <stdio.h>
#define MAX 20
int main(void)
{
struct company
{
float east,north;
char name[20],place[20];
};
struct company site[MAX];
FILE*file;
int i=0;
file = fopen("companies.txt" ,"r");
while ( 1 /*!feof(file)*/ )
{
fscanf(file,"%s %s %f %f",&site.name[0],&site.place[0],&site.east,&site.north);
if(!feof(file))
{
printf("%s %s %f %f\n",site.name,site.place,site.east,site. north);
i++;
}
else
{
break;
}
}
fclose(file);
return 0;
}
My input file is:
Ikea Nottingham 0.1 0.1
Tesco Lincoln 0.2 0.2
Asda Leicester 0.3 0.4