I am having trouble adding two records one after the other. There’s no problem when I add the first record but when I try to add another I get that exception error message. When I close the program down and run it again and then try to add the record it works fine (that’s until I try to add the second record again), anyways this is the code I got so far…………
any help with be greatly appreciated, ThanxCode:#include <stdio.h> #include <conio.h> #include <string.h> #include <alloc.h> FILE *fSupplier; void DisplaySupplierMenu(); void SearchBySupplierCode(); void AddSupplierRecord(); void AmendSupplierRecord(); bool SearchSupplier(); bool SearchSupplierOne(); void DisplaySearchedSupplierRecord(); bool WriteNewSupplierRecord(); void LoadSupplierRecord(struct SupplierRefer **q); void AddSupplierNode(struct SupplierRefer **q, int num); void AssignSupplierRefer(struct SupplierRefer **p); bool FindSupplierName(); void OpenSupplierFile(); void CloseSupplierFile(); int CurrSupplierRecNum=0; int TotalSupplierNode=0; char SearchedSupplierCode[7]; char SearchedSupplierName[21]; char purpose[25]; struct Supplier{ char SupplierCode[7]; char SupplierName[21]; char SupplierAddress[21]; }; struct Supplier Supplier; struct SupplierRefer{ char SupplierCode[7]; char SupplierName[21]; struct SupplierRefer *Supplierlink; }; struct SupplierRefer *mySupplierrefer; int main() { mySupplierrefer=(struct SupplierRefer*)malloc(sizeof(struct SupplierRefer)); LoadSupplierRecord(&mySupplierrefer); bool isExit=false; while (!isExit) { DisplaySupplierMenu(); char ch; gets(&ch); switch (ch) { case '1': SearchBySupplierCode(); break; case '2': AddSupplierRecord(); break; case '3': AmendSupplierRecord(); break; case '0': isExit=true; } } return 0; } void DisplaySupplierMenu() { puts(" \n"); puts(" * 1 : View Supplier Record *\n"); puts(" * 2 : Add Supplier Record *\n"); puts(" * 3 : Amend Supplier Record *\n"); puts(" * 0 : Exit *\n"); puts(" \n"); } void OpenSupplierFile() { fSupplier=fopen("a:\\Supplier.txt", "r+"); if (fSupplier==0) { fSupplier=fopen("a:\\Supplier.txt", "w"); fclose(fSupplier); fSupplier=fopen("a:\\Supplier.txt", "r+"); } } void CloseSupplierFile() { fclose(fSupplier); } void SearchBySupplierCode() { OpenSupplierFile(); puts("Enter Supplier Code For The Record You Want To Search\n"); gets(SearchedSupplierCode); if (SearchSupplier()) { DisplaySearchedSupplierRecord(); CloseSupplierFile(); return; } else { printf("Supplier Code Wasn't Found"); return; } } void DisplaySearchedSupplierRecord() { long offset; printf("Searched Record Details\n"); offset=sizeof(Supplier)* CurrSupplierRecNum; fseek(fSupplier,offset,0); fread(&Supplier, sizeof(Supplier), 1, fSupplier); puts("\nSupplier Code"); puts(Supplier.SupplierCode); puts("\nSupplier Name"); puts(Supplier.SupplierName); puts("\nSupplier Address"); puts(Supplier.SupplierAddress); printf("\nSearched Successfully"); } void AddSupplierRecord() { puts("\nEnter New Supplier Code\n"); gets(SearchedSupplierCode); OpenSupplierFile(); while(true) { if (SearchSupplierOne()) { printf("\nEntered Supplier Code Already Exixts\n"); puts("\nEnter New Supplier Code\n"); gets(SearchedSupplierCode); } else { if (WriteNewSupplierRecord()) { AddSupplierNode(&mySupplierrefer, TotalSupplierNode); fclose(fSupplier); return; } else { fclose(fSupplier); return; } } } } bool WriteNewSupplierRecord() { long offset; char temp_name[21]; char temp_address[21]; offset=sizeof(Supplier)* TotalSupplierNode; fseek(fSupplier, offset, 0); fflush(stdin); if(strlen(SearchedSupplierCode)== 6) { strcpy(Supplier.SupplierCode, SearchedSupplierCode); puts("Enter Value For Supplier Name, Should Have Atleast Single Character\n"); gets(temp_name); if(strlen(temp_name)>0 & strlen(temp_name)<21) { strcpy(Supplier.SupplierName, temp_name); puts("Enter Value For Supplier Address\n"); gets(temp_address); if(strlen(temp_address)>0 & strlen(temp_address)<21) { strcpy(Supplier.SupplierAddress, temp_address); } else { puts("Enterd Supplier Address Is Not Valid, Should Be Between 1-20 Characters"); return false; } fwrite(&Supplier, sizeof(Supplier), 1, fSupplier); puts("New Record Added Successfully"); return true; } else { puts("Enterd Supplier Name Is Not Valid, Should Be Between 1-20 Characters, Should Have Atleast Single Character"); AddSupplierRecord(); } } else { puts("Enterd Supplier Code Is Not Valid, Should Be 6 Characters\n"); AddSupplierRecord(); } } void AmendSupplierRecord() { long offset; char temp_name[21]; char temp_address[21]; fflush(stdin); puts("\nEnter Supplier Code You Want To Amend\n"); gets(SearchedSupplierCode); OpenSupplierFile(); if (SearchSupplier()) { offset=sizeof(Supplier)* CurrSupplierRecNum; fseek(fSupplier, offset, 0); fread(&Supplier, sizeof(Supplier), 1, fSupplier); puts(Supplier.SupplierName); puts("\nEnter New Value For Supplier Name\n"); gets(temp_name); if(strlen(temp_name)>0 & strlen(temp_name)<21) strcpy(Supplier.SupplierName, temp_name); else { printf("Entered Supplier Name Is Not Valid, Should Be Between 1-20 Characters"); return; } puts(Supplier.SupplierAddress); puts("Enter New Value For Supplier Address\n"); gets(temp_address); if(strlen(temp_address)>0 & strlen(temp_address)<21) strcpy(Supplier.SupplierAddress, temp_address); else { printf("Entered Supplier Address Is Not Valid, Should Be Between 1-20 Characters"); return; } fseek(fSupplier, offset, 0); fwrite(&Supplier, sizeof(Supplier),1,fSupplier); fflush(stdin); puts("Record Successful Amended"); CloseSupplierFile(); LoadSupplierRecord(&mySupplierrefer); } else { printf("Searched Supplier Code Does Not Exist"); fflush(stdin); } } bool SearchSupplier() { int index=-1; struct SupplierRefer *temp; temp=mySupplierrefer; if (TotalSupplierNode==0 ) { puts("File Is Empty."); return false; } while (index<=TotalSupplierNode) { if (strcmp(SearchedSupplierCode,temp->SupplierCode)==0) { CurrSupplierRecNum=index; return true; } else { index+=1; temp=temp->Supplierlink; } } return false; } bool SearchSupplierOne() { int index=0; struct SupplierRefer *temp; temp=mySupplierrefer; if (TotalSupplierNode==0 ) { puts("File Is Empty."); return false; } while (index<=TotalSupplierNode) { if (strcmp(SearchedSupplierCode,temp->SupplierCode)==0) { CurrSupplierRecNum=index; return true; } else { index+=1; temp=temp->Supplierlink; } } return false; } bool FindSupplierName() { OpenSupplierFile(); return false ; } void LoadSupplierRecord(struct SupplierRefer **q) { long offset; struct SupplierRefer *temp; temp=*q; TotalSupplierNode=0; OpenSupplierFile(); while(true) { if(fread(&Supplier, sizeof(Supplier),1,fSupplier)==1) { if (temp==NULL) { temp=(struct SupplierRefer*)malloc(sizeof(struct SupplierRefer)); AssignSupplierRefer(&temp); } else { temp->Supplierlink=(struct SupplierRefer*)malloc(sizeof(struct SupplierRefer) ); temp=temp->Supplierlink; AssignSupplierRefer(&temp); } } else { CloseSupplierFile(); break; } } return; } void AssignSupplierRefer(struct SupplierRefer **p) { struct SupplierRefer *temp; temp=*p; long offset=sizeof(Supplier)*TotalSupplierNode; fseek(fSupplier, offset, 0); fread(&Supplier, sizeof(Supplier), 1,fSupplier); strcpy(temp->SupplierCode,Supplier.SupplierCode); strcpy(temp->SupplierName, Supplier.SupplierName); TotalSupplierNode+=1; return; } void AddSupplierNode(struct SupplierRefer **q, int num) { int index; struct SupplierRefer *temp; if (TotalSupplierNode==0 || *q==NULL) { *q=(struct SupplierRefer*)malloc(sizeof(struct SupplierRefer)); temp=*q; AssignSupplierRefer(&temp); return; } else { while(index<=TotalSupplierNode) { temp=temp->Supplierlink; } temp=(struct SupplierRefer*)malloc(sizeof(struct SupplierRefer) ); AssignSupplierRefer(&temp); } return; }



LinkBack URL
About LinkBacks


