Right this is the program so far.
Code:
#include <stdio.h>
#include <stdlib.h> /* malloc, realloc, free */
#include <string.h>
struct record {
int ID;
char data[64];
};
int Total = 0;
struct record **create (char *string, struct record **rp) {
static int id = 1;
struct record *ptr = malloc(sizeof(struct record));
rp = realloc(rp,(Total+1)*sizeof(struct record*));
ptr->ID = id++;
strncpy(ptr->data,string,63);
ptr->data[63] = '\0';
rp[Total++] = ptr;
return rp;
}
void show (struct record *ptr) {
printf("ID: %d Data: %s\n",ptr->ID,ptr->data);
}
int main() {
int i, menu;
//Opens the database file inventory.txt, or creates it if it does not exist.
FILE *f;
f = fopen("inventory.txt","w+");
//Prints a message
printf("Welcome to the inventory system \nWhat would you like to do? \nRead the database (1) Write to the database (2)?\n");
scanf("%d", &menu);
if (menu == 1){
//Displays the database as ID: Data:
struct record **database = NULL;
database=create("first record",database);
database=create("second record",database);
for (i=0; i<Total; i++) show(database[i]);
return 0;
}
if (menu == 2){
printf("You have chosen to write to the database!");
return 0;
}
}
Which basically now opens or creates the text file "inventory" and also provides a simple menu for the user.
I'm now stuck on adding records to the database, as I'm not sure what this means.
Code:
database=create("first record",database);
Is create a function? Any example off how I could get the user input then write it to a new line on a text file?