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.

How would I do this? Hope someone can help. Thanks.

>>Each record is for a company with the following fields:

It would be easier to help if you paste the relevant code here.

EDIT: @alexchen. Sorry, i didn't see your post.

Edited 5 Years Ago by myk45: n/a

Sorry my code is:

#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[i].name[0],&site[i].place[0],&site[i].east,&site[i].north);
        if(!feof(file))
        {
            printf("%s %s %f %f\n",site[i].name,site[i].place,site[i].east,site[i]. 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

Edited 3 Years Ago by mike_2000_17: Fixed formatting

#include <stdio.h>
#define MAX 20

/* prototype your struct in global scope, above main() */
 struct company
 {
    float east,north;
    char name[20],place[20];
 };


int main(void) {
    
    struct company site[MAX];  //instantiate it in main() - good

    FILE*file;
    int i=0;

    file = fopen("companies.txt" ,"r");

    while(fscanf(file,"%s %s %f %f",site[i].name[0],site[i].place[0], &site[i].east, &site[i].north)) == 4) { //while fscanf() returns 4
       //feof isn't what you'd like - don't use it.
       printf("%s %s %f %f\n",site[i].name,site[i].place,site[i].east,site[i]. north);
       i++;
    }
    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

Try that - I didn't compile and run - excuse any minor bug.

Edited 5 Years Ago by Adak: n/a

Thanks. It was working before but obviously it is better without feof. I am trying to create other functions as seen in my first post....
I would like to have a function to add a new company (add) to the array struct
A function to read the records of companies using the add function
A function to delete a company
A function to find the distance between two companies
All this would be done by user input from menu.
Im not sure how to do this. How do I add an element or delete an element of a struct array?

>>Im not sure how to do this. How do I add an element or delete an element of a struct array?

Well, you cannot add or delete elements from an array.

eg:

data_type var[100];

// Here, you cannot add an extra element. Also, you cannot try and free any memory(from the array).

Best option is, to use a Linked List. Or, if you're allowed to program in C++, try using vectors.

Edited 5 Years Ago by myk45: n/a

So say you have an array var[100]

and you have only filled the array up to var[50] ... you can not insert a new value into var[51] ? Im sure or I know you definitely can, because I have done it.....I just don't know how to do it for a struct array say if you have 4 different variables.

You are asking the wrong question. If you have an array of 100 structures then keep a counter to determine which elements have already been used. Its pretty simple concept -- really.

struct company
 {
    float east,north;
    char name[20],place[20];
 };

int main()
{
   struct company companies[100];
   int lastUsed = 0;
   
}

Another way to do this is to create a linked list of those structures so that the array can expand or contract as much as it needs to. Inserting into a linked list is more complex than inserting into unused spots of an array, but it has the advantage of not needing to know how may elements are needed when the program is compiled.

Edited 5 Years Ago by Ancient Dragon: n/a

So say you have an array var[100]

and you have only filled the array up to var[50] ... you can not insert a new value into var[51] ? Im sure or I know you definitely can, because I have done it.....I just don't know how to do it for a struct array say if you have 4 different variables.

fscanf(fp, "%s", structName[51].stringMember);

Is the general way to approach it, but consider that:

1) 51 would normally be a loop variabe, like i, instead of a hard coded number like 51
2) While strings just need a string name as their address, numbers and single char's would need the usual & prepended before the structName: &myStructName.myNumberOrChar

While linked lists are a lot more flexible, they are also a lot more difficult to work with, for beginners. Until you're really comfortable working with structs and C in general, I'd avoid trying to work with linked lists, except as learning exercises.

You need to re-read or watch another tutorial on-line, about using structs. You can't work successfully with an array of structs, if you don't know how to work with a single struct, and it's various members, with some competence.

Good luck!

This article has been dead for over six months. Start a new discussion instead.