commit 68eec1a0852cab55c6ebbfc30c58bd3041346ddc Author: Jack-Benny Persson Date: Sun Jul 9 06:07:34 2017 +0200 First commit diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..11064c6 --- /dev/null +++ b/Makefile @@ -0,0 +1,8 @@ +CC=cc +CFLAGS=-Wall -pedantic + +on-stock: on-stock.o + ${CC} ${CFLAGS} on-stock.o -o on-stock + +clean: + rm -f on-stock.o on-stock diff --git a/on-stock.c b/on-stock.c new file mode 100644 index 0000000..5987d57 --- /dev/null +++ b/on-stock.c @@ -0,0 +1,287 @@ +#include +#include +#include +#include + +#define FILEMAXLENGTH 50 +#define NAMEMAXLENGTH 30 + +struct myData +{ + char name[NAMEMAXLENGTH]; + int quantity; + float price; +}; + +void list(struct myData *datap, int numRec); +void search(struct myData *datap, int numRec); +void modify(struct myData *datap, int numRec); +void delete(struct myData *datap, int numRec); +int new(struct myData *datap, int numRec); + +int main(int argc, char* argv[]) +{ + int numRec; + int create; + int choice; + int opt; + char filename[FILEMAXLENGTH] = "alloctest.bin"; + + if (argc != 2) + { + fprintf(stderr, "Usage: %s [-l] [-s] [-m [-m [-n] [-f filename]\n" + "-l = list the articles in the database\n" + "-s = search for an article in the database\n" + "-m = modify a article\n" + "-r = remove a article\n" + "-n = create a new article\n" + "-f = specifiy a filename for the database\n", argv[0]); + return 1; + } + + /* Process command line arugments */ + while ((opt = getopt(argc, argv, "lsmrnf:")) != -1) + { + switch (opt) + { + case 'l': + choice = 'l'; + break; + case 's': + choice = 's'; + break; + case 'm': + choice = 'm'; + break; + case 'r': + choice = 'r'; + break; + case 'n': + choice = 'n'; + break; + case 'f': + strncpy(filename, optarg, FILEMAXLENGTH-1); + break; + default: + fprintf(stderr, "Usage: %s [-l] [-s] [-m [-m [-n] [-f filename]\n" + "-l = list the articles in the database\n" + "-s = search for an article in the database\n" + "-m = modify a article\n" + "-r = remove a article\n" + "-n = create a new article\n" + "-f = specifiy a filename for the database\n", argv[0]); + return 1; + } + } + + + /* Check if file exists, if it dosen't, create it */ + if ( access(filename, R_OK|W_OK) != 0 ) + { + printf("Kan inte öppna %s\n", filename); + printf("Skapa filen och lägg till artiklar? (j/n): "); + create = getchar(); + if ( create == 'j' ) + { + numRec = 1; + struct myData *data; + data = calloc(numRec, sizeof(struct myData)); + new(data, numRec); + free(data); + } + else + return 1; + } + + struct myData *data; + + if ( choice == 'l' || choice == 's' || choice == 'm' || choice == 'r' ) + { + /* Open file in read-mode */ + FILE *fp = fopen("alloctest.bin", "rb"); + + /* Read in the content from the file to the structure */ + fseek(fp, 0, SEEK_END); + numRec = ftell(fp) / sizeof(struct myData); + data = calloc(numRec, sizeof(struct myData)); + rewind(fp); + fread(data, sizeof(struct myData), numRec, fp); + fclose(fp); + + + if ( choice == 'l' ) + list(data, numRec); + else if ( choice == 's' ) + search(data, numRec); + else if ( choice == 'm' ) + modify(data, numRec); + else if ( choice == 'r' ) + delete(data, numRec); + + } + else if ( choice == 'n' ) + { + data = calloc(1, sizeof(struct myData)); + new(data, numRec); + } + + free(data); + return 0; +} + +void list(struct myData *datap, int numRec) +{ + for (int i = 0; iname, NAMEMAXLENGTH, stdin); + datap->name[strcspn(datap->name, "\n")] = '\0'; + + if ( strcmp(datap->name, "klar") == 0 ) + { + fclose(fp); + return 0; + } + printf("Quantity: "); scanf("%d", &datap->quantity); + printf("Price: "); scanf("%f", &datap->price); + bytes = fwrite(datap, sizeof(struct myData), 1, fp); + if (bytes != 1) + { + printf("Kunde inte skriva till filen!\n"); + return 1; + } + } +} + +