c - Record add/show/delete for file functions not working -
the question's title pretty explains situation. not used using files , associated commands (such fseek, fwrite, etc).
anyhow, following program must simulate menu, , present user choice of either adding, editing, deleting or showing records file. rather simplistic task, can't figure out i'm doing wrong.
#include <stdio.h> typedef struct{ int code; char description[100]; int volume; }product; int main() { int pick =0, i; product *p; file *fileptr; void add(file *fileptr, product* p); void removal(file *fileptr, product* p); void show(file *fileptr, product* p); void edit(file *fileptr, product* p); void description(file *fileptr, product* p); if ( ( fileptr = fopen( "stock.dat", "rb+")) == null ) { printf("the file not read"); } else { do{ printf(" ************ main menu ************\n"); printf(" ** -------------welcome------------- **\n"); printf(" ** ** \n"); printf("* please select 1 of commands below *\n"); printf("********************************************\n"); printf("* press 1 remove products list *\n"); printf("* press 2 change product's quantity *\n"); printf("* press 3 add new product list *\n"); printf("* press 4 view product list *\n"); printf("* press 5 exit programm *\n"); printf("********************************************\n"); scanf("%d", &pick); switch ( pick ) { case 1: removal(fileptr, p); break; case 2: edit(fileptr, p); break; case 3: add(fileptr, p); break; case 4: show(fileptr, p); break; default: printf("error! please enter correct number"); break; } }while (pick != 5); } fclose(fileptr); return 0; } void removal(file *fileptr, product * p) { int code; product empty = { 0, " ", 0 }; printf("\n\nyou delete record, please enter product's code"); scanf("%d", &code); fseek( fileptr, (code - 1) * sizeof(p), seek_set); fread( &p->code, sizeof(p), 1, fileptr); if (p->code == 0){ printf("product code not found, please retry"); } else { fseek( fileptr, (code -1) * sizeof(p), seek_set); fwrite( &empty, sizeof(p), 1, fileptr); printf("code found, product removed successfully\n\n"); } } void add(file *fileptr, product * p){ int code, quantity; printf("you add new product in list\n please input product's code\n"); scanf("%d", &code); fseek( fileptr, (code-1) * sizeof(p), seek_set); fread(&code, sizeof(p), 1, fileptr); fseek(fileptr, (code-1) * sizeof(p), seek_set); fwrite( &code, sizeof(p), 1, fileptr); printf("now enter product's remaining quantity"); scanf("%d", &quantity); fseek(fileptr, (quantity-1) * sizeof(p), seek_set); fwrite( &quantity, sizeof(p), 1, fileptr); } void edit(file *fileptr, product * p){ int code, volume; printf("\nyou edit existing product's quantity\n please enter product's code\n"); scanf("%d", &code); fseek(fileptr, (code -1) * sizeof(p), seek_set); fread(&code, sizeof(p), 1, fileptr); if (p->code == 0){ printf("\nthe code entered not in list, please retry\n"); }else{ printf("\ncode found!\n product's current quantity %d", &p->volume); printf("\n please enter new quantity"); scanf("%d", &volume); p->volume = volume; fseek(fileptr, (volume -1) * sizeof(p), seek_set); fwrite(&volume, sizeof(p), 1, fileptr); } } void show(file *fileptr, product * p){ int i=1; printf("\n\n\n ****showing products****\n\n\n"); printf("product code quantity in stock\n"); while (i<100 && !feof(fileptr)){ fscanf( fileptr, "%d%d", &p->code, &p->volume); printf("%d %d\n", &p->code, &p->volume); fseek(fileptr, (p->code + 1) * sizeof(p), seek_set); fscanf( fileptr, "%d%d", &p->code, &p->volume); i++; } printf("you may scroll view list\n\n"); }
to give idea of happening: if user enters option 4, program, instead of showing no records, shows random values, if user chooses add product (by picking choice 3, add function), won't shown later , result, can't deleted if user chooses so. again, i'm rather new @ programming (i sure hear lot :p). had posted similar question before, did not solution.
edit edited big portion of code, file opens in binary , now, struct product p, passes each function. problem supposedly fixed, program run once showing correct values , records, crashes uknown reason. additional great
there's problem show
function...
for starters, make following changes (marked using comment):
void show(file *fileptr){ typedef struct{ int code; char description[100]; int volume; } product; product p; int i=1; // removed re-opening of file printf("\n\n\n ****showing products****\n\n\n"); printf("product code quantity in stock\n"); // moved first fscanf() while loop while (i<100 && !feof(fileptr)){ fscanf( fileptr, "%d%d", &p.code, &p.volume); printf("%d %d\n", &p.code, &p.volume); fseek(fileptr, (p.code + 1) * sizeof(p), seek_set); i++; } printf("you may scroll view list\n\n"); }
Comments
Post a Comment