Undefined symbol _system in module in c - c

i have this code. I am getting linker error. I don't know what it is this linker error. please help me. I am using turbo c++ compiler for this code.
#include<string.h>
#include<process.h>
#include<stdlib.h>
#include<dos.h>
struct contact
{
long ph;
char name[20],add[20],email[30];
};
struct contact list;
char query[20],name[20];
FILE *fp, *ft;
int i,n,ch,l,found;
void main()
{
start:
system("cls"); /* ************Main menu *********************** */
printf("\n\t **** Welcome to a0 contact Manager ****");
printf("\n\n\n\t\t\tMAIN MENU\n\t\t=====================\n\t\t[1] Add a new Contact\n\t\t[2] List all Contacts\n\t\t[3] Search for contact\n \t\t[4] Edit a Contact\n\t\t[5] Delete a Contact\n\t\t[0] Exit\n\t\t=================\n\t\t");
printf("Enter the choice:");
scanf("%d",&ch);
switch(ch)
{
case 0:
printf("\n\n\t\tAre you sure u want to exit?");
break;
/* *********************add new contacts************ */
case 1:
system("cls");
fp=fopen("contact.dll","a");
for(;;)
{
fflush(stdin);
printf("To exit enter blank space in the name input\nName (Use identical):");
scanf("%[^\n]",&list.name);
if(stricmp(list.name,"")==0 || stricmp(list.name," ")==0)
break;
fflush(stdin);
printf("Phone:");
scanf("%ld",&list.ph);
fflush(stdin);
printf("address:");
scanf("%[^\n]",&list.add);
fflush(stdin);
printf("email address:");
gets(list.email);
printf("\n");
fwrite(&list,sizeof(list),1,fp);
}
fclose(fp);
break;
/* *********************list of contacts************************* */
case 2:
system("cls");
printf("\n\t\t================================\n\t\t\tLIST OF CONTACTS\n\t\t================================\n\nName\t\tPhone No\t Address\t\tE-mail ad.\n=================================================================\n\n");
for(i=97;i<=122;i=i+1)
{
fp=fopen("contact.dll","r");
fflush(stdin);
found=0;
while(fread(&list,sizeof(list),1,fp)==1)
{
if(list.name[0]==i || list.name[0]==i-32)
{
printf("\nName\t: %s\nPhone\t: %ld\nAddress\t: %s\nEmail\t: %s\n",list.name,list.ph,list.add,list.email);
found++;
}
}
if(found!=0)
{
printf("=========================================================== [%c]-(%d)\n\n",i-32,found);
getch();
}
fclose(fp);
}
break;
/* *******************search contacts********************** */
case 3:
system("cls");
do
{
found=0;
printf("\n\n\t..::CONTACT SEARCH\n\t===========================\n\t..::Name ofcontact to search: ");
fflush(stdin);
scanf("%[^\n]",&query);
l=strlen(query);
fp=fopen("contact.dll","r");
system("cls");
printf("\n\n..::Search result for '%s' \n===================================================\n",query);
while(fread(&list,sizeof(list),1,fp)==1)
{
for(i=0;i<=l;i++)
name[i]=list.name[i];
name[l]='\0';
if(stricmp(name,query)==0)
{
printf("\n..::Name\t: %s\n..::Phone\t: %ld\n..::Address\t: %s\n..::Email\t:%s\n",list.name,list.ph,list.add,list.email);
found++;
if(found%4==0)
{
printf("..::Press any key to continue...");
getch();
}
}
}
if(found==0)
printf("\n..::No match found!");
else
printf("\n..::%d match(s) found!",found);
fclose(fp);
printf("\n ..::Try again?\n\n\t[1] Yes\t\t[0] No\n\t");
scanf("%d",&ch);
}
while(ch==1);
break;
/* *********************edit contacts************************/
case 4:
system("cls");
fp=fopen("contact.dll","r");
ft=fopen("temp.dat","w");
fflush(stdin);
printf("..::Edit contact\n===============================\n\n\t..::Enter the name of contact to edit:");
scanf("%[^\n]",name);
while(fread(&list,sizeof(list),1,fp)==1)
{
if(stricmp(name,list.name)!=0)
fwrite(&list,sizeof(list),1,ft);
}
fflush(stdin);
printf("\n\n..::Editing '%s'\n\n",name);
printf("..::Name(Use identical):");
scanf("%[^\n]",&list.name);
fflush(stdin);
printf("..::Phone:");
scanf("%ld",&list.ph);
fflush(stdin);
printf("..::address:");
scanf("%[^\n]",&list.add);
fflush(stdin);
printf("..::email address:");
gets(list.email);
printf("\n");
fwrite(&list,sizeof(list),1,ft);
fclose(fp);
fclose(ft);
remove("contact.dll");
rename("temp.dat","contact.dll");
break;
/* ********************delete contacts**********************/
case 5:
system("cls");
fflush(stdin);
printf("\n\n\t..::DELETE A CONTACT\n\t==========================\n\t..::Enter the name of contact to delete:");
scanf("%[^\n]",&name);
fp=fopen("contact.dll","r");
ft=fopen("temp.dat","w");
while(fread(&list,sizeof(list),1,fp)!=0)
if (stricmp(name,list.name)!=0)
fwrite(&list,sizeof(list),1,ft);
fclose(fp);
fclose(ft);
remove("contact.dll");
rename("temp.dat","contact.dll");
break;
default:
printf("Invalid choice");
break;
}
printf("\n\n\n..::Enter the Choice:\n\n\t[1] Main Menu\t\t[0] Exit\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
goto start;
case 0:
break;
default:
printf("Invalid choice");
break;
}
}
I am facing this error.
Undefined symbol _system in module in c.

The Undefined symbol _system linker error means that the linker cannot find any (static) library code to link against your system("cls") calls. In the case of Turbo C++ this probably means you have to specify an additional library in your project configuration.
If you would use a contemporary C compiler such as GCC or Clang (which are freely available for many systems), you will not have this problem.

Related

My c program keeps crashing on as it's starting

I'm making a c program for a simple library but for some reason the program keeps crashing right on startup. Like there's a menu that is displayed but it doesn't even appear it just crashes. can any one help me?
struct library
{
char name[50];
int id;
int qty;
}books[50],copy[50],delet[50],sort[50];
int i=0;
FILE *mybooks;
int main()
{
int choice; char ans;
int id;
int qty;
int s,o=0,j=0;
char name[50];
mybooks=fopen("D:\\mybooks.txt","r");
if (mybooks == NULL) printf("Error. File not found.");
else
{
while(!feof(mybooks))
{
fscanf(mybooks,"%[^\n] %d %d",books[i].name,&books[i].id,&books[i].qty);
strcpy(copy[i].name,books[i].name);
copy[i].id=books[i].id;
copy[i].qty=books[i].qty;
i++;
}
fclose(mybooks);
}
printf("Welcome to the Library.\n");
do
{
printf("Please choose an option:\n");
printf("1.Insert a book\n");
printf("2.Delete a book\n");
printf("3.Search a book by ID\n");
printf("4.Search a book by name\n");
printf("5.Display all books (sorted by name)\n");
printf("6.Display all books (unsorted)\n");
scanf("%d",&choice);
switch (choice){
case 1:
printf("You will need to enter a name, ID, and quantity of the book.\n");
printf("please enter book name:");
fflush(stdin);
fgets(name,sizeof name,stdin);
printf("please enter book ID:");
scanf("%d",&id);
printf("please enter book quantity:");
scanf("%d",&qty);
InsertBook(name,id,qty);
printf("your book has been added successfully\n");
break;
case 2:
printf("Please enter book ID:");
scanf("%d",&id);
DeleteBook(id);
printf("book successfully deleted.\n");
break;
case 3:
printf("Please enter ID of Book:");
scanf("%d",&id);
s=LinearSearch(id,j);
if (s>=0)
{
printf("Book Found.\n");
printf("Name:%s",books[s].name);
printf("ID:%d\n",books[s].id);
printf("Quantity:%d\n",books[s].qty);
}
else
printf("Sorry, the book doesn't exist.\n");
break;
case 4:
printf("Please enter name of book:");
fflush(stdin);
gets(name);
sorting();
s=BinarySearch(name,0,i);
printf("Book Found.\n");
printf("ID:%d\n",sort[s].id);
printf("Quantity:%d\n",sort[s].qty);
break;
case 5:
sorting();
while (o<i);
{
printf("%s\n",sort[o].name);
o++;
}
printf("\n");
break;
case 6:
while(o<i)
{
printf("%s",books[o].name);
o++;
}
break;
default:
printf("Invalid Choice. Please try again.\n");
break;
}
printf("do you want to choose another option?(y/n) ");
scanf(" %c",&ans);
}while(ans == 'y');
}
(I'm using functions for the library but I don't think they are causing any problem since i didn't call them yet.)
edited the question to add the structure
You have tested an end of file condition before performing any i/o on the input file. And then, when you perform the i/o with fscanf(), you did not test the result to see if the variables were successfully read.
What is happening is that scanf() is probably failing and you do not reach the end of the loop, i.e., the EOF condition. You get stuck in there until one of your assignments (strcpy(copy[i].name,books[i].name);, copy[i].id=books[i].id; or copy[i].qty=books[i].qty;) will finally cause an overflow.
To verify this, run the code in a debugger.
Using fscanf() is very tricky, always test it thoroughly.

C program compiles in xcode, but runs infinite loop in terminal

I've been working on a group program for a systems programming class which will run functions that someone at a bank would use. These functions include add bank customers, output balance of a customer, and so on. The problem lies with the add customer function.
This function will run in an infinite loop in terminal and Putty, but when I run it as a standalone program in XCode the loop quits. Is there anything wrong with the program that I, my group members AND our professor are overlooking?
#include <stdio.h>
#include <string.h>
#include "./bank.h"
void add(FILE *fp)
{
int i=0;
char firstName[20];
char lastName[20];
float camount;
int prompt;
float samount;
float mamount;
fseek(fp,0,2);
do
{
printf("\nEnter first name: ");
scanf("%s", firstName);
strcpy(bank[i].firstName, firstName);
printf("\nEnter last name: ");
scanf("%s",lastName);
strcpy(bank[i].lastName, lastName);
printf("\nEnter checking account balance: ");
scanf("%f", &camount);
bank[i].checking = camount;
printf("\nEnter savings account balance: ");
scanf("%f", &samount);
bank[i].savings = samount;
printf("\nEnter money market account balance: ");
scanf("%f", &mamount);
bank[i].moneyMarket = mamount;
fwrite(&bank[i],1, sizeof(struct BankInfo),fp);
i++;
printf("Enter 1 to enter another name and 0 to quit: ");
scanf("%d", &prompt);
printf("%d\n", prompt);
} while(prompt == 1);
fclose(fp);
return;
}
Screenshot of output once Makefile is executed:
Sidenote: This function does write onto the file that we are passing which is great but we still need for it to break out of the loop.
EDIT: code for the main function:
#include
#include
#include
#include "bank.h"
#include "definitions.h"
int main()
{
FILE *fp;
int selection;
fp=fopen("bankInfo.dat","ab+");
selection=menu();
while(selection !=6)
{
switch(selection)
{
case 1:
add(fp);
break;
case 2:
// outputBalance(fp);
break;
case 3:
delete(fp);
break;
case 4:
update(fp);
break;
case 5:
// display(fp);
break;
case 6:
exit(0);
break;
default:
printf("Invalid selection\n");
break;
}
}
fclose(fp);
return 0;
}
You are assigning selection only once. This should fix your issue:
while(true)
{
selection=menu();
switch(selection)
{
case 1:
add(fp);
break;
case 2:
// outputBalance(fp);
break;
case 3:
delete(fp);
break;
case 4:
update(fp);
break;
case 5:
// display(fp);
break;
case 6:
fclose(fp);
exit(0);
break;
default:
printf("Invalid selection\n");
break;
}
}
return 0;

What are my errors in this code in C? It's a library catalog that needs to use FILE handling and BST

1) I'm stuck with inserting and displaying the user inputs. I'm confused when to use fwrite, whether within the addRecord function or within the insert function.
2) Should I write it after I input or after the insertion?
Anyhow, my code looks like this:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#include<Windows.h>
struct book{
char title[30];
char author[20];
char isbn[14];
char genre[10];
char publisher[30];
int year;
char synopsis[150];
struct book *left;
struct book *right;
};
typedef struct book catalog;
catalog *root;
FILE *fx,*fy;
int structsize=sizeof(catalog);
void addBooks();
void removeBooks();
void modify();
void searchBook();
void perTitle();
void perAuthor();
void perISBN();
void perGenre();
void displayCatalog(catalog *root);
int ifexists(char *x);
void insert(catalog *root,catalog *x);
catalog remove(catalog *root,catalog *x);
void menu();
void gotoxy(int x,int y);
void main(){
root=NULL;
fx=fopen("CAT.dat","rb+");
if(fx==NULL){
fx=fopen("CAT.dat","wb+");
if(fx==NULL){
gotoxy(22,10);
printf("No records to show...");
getch();
menu();
}
}
menu();
}
void menu(){
int choice;
system("cls");
gotoxy(22,5);
printf("----------WELCOME TO THE CARD CATALOG----------");
gotoxy(22,7);
printf("1] Add a book");
gotoxy(22,8);
printf("2] Remove a book");
gotoxy(22,9);
printf("3] Modify a book");
gotoxy(22,10);
printf("4] Search a book");
gotoxy(22,11);
printf("5] Display the catalog");
gotoxy(22,12);
printf("6] Exit");
gotoxy(22,14);
printf("Enter your choice: ");
scanf("%d",&choice);
switch(choice){
case 1: addBooks();
break;
case 2: removeBooks();
break;
case 3: modify();
break;
case 4: searchBook();
break;
case 5: displayCatalog(root);
break;
case 6: exit(0);
}
}
void addBooks(){
char n;
catalog *ptr;
ptr=(catalog*)malloc(sizeof(catalog));
fx=fopen("CAT.dat","ab+");
fseek(fx,0,SEEK_END);
system("cls");fflush(stdin);
gotoxy(22,8);
printf("Title: ");
scanf("%[^\n]s",ptr->title);fflush(stdin);
gotoxy(22,9);
printf("Author: ");
scanf("%[^\n]s",ptr->author);fflush(stdin);
gotoxy(22,10);
printf("Genre: ");
scanf("%[^\n]s",ptr->genre);fflush(stdin);
gotoxy(22,11);
printf("ISBN: ");
scanf("%[^\n]s",ptr->isbn);fflush(stdin);
gotoxy(22,12);
printf("Publisher: ");
scanf("%[^\n]s",ptr->publisher);fflush(stdin);
gotoxy(22,13);
printf("Year: ");
scanf("%d",&ptr->year);fflush(stdin);
gotoxy(22,14);
printf("Synopsis: ");
scanf("%[^\n]s",ptr->synopsis);fflush(stdin);
ptr->left=NULL;
ptr->right=NULL;
if(root==NULL){
root=ptr;
} else {
insert(root,ptr);
}
gotoxy(22,18);
printf("Add another (y/n)? ");
scanf("%c",&n);
if(n=='y' || n=='Y'){
addBooks();
}
fclose(fx);
getch();
menu();
}
void insert(catalog *root,catalog *x){
fx=fopen("CAT.dat","ab+");
fseek(fx,0,SEEK_END);
if((x->isbn) < (root->isbn)){
if(root->left==NULL){
root->left=x;
} else {
insert(root->left,x);
}
}
if((x->isbn) > (root->isbn)){
if(root->right==NULL){
root->right=x;
} else {
insert(root->right,x);
}
}
fwrite(&x,structsize,1,fx);
fclose(fx);
getch();
}
void removeBooks(){
catalog *ptr,*temp;
int x;
char title[20],y;
system("cls");
if(root==NULL){
gotoxy(22,10);
printf("No records to show...");
getch();
menu();
}
gotoxy(22,10);
printf("Enter title of book to delete: ");
scanf("%s",title);
ptr=root;
while(ptr!=NULL){
if(strcmp(ptr->title,title)==0){
gotoxy(22,11);
printf("The book is in the catalog.");
gotoxy(22,12);
printf("Title: %s",root->title);
gotoxy(22,13);
printf("Author: %s",root->author);
} else {
gotoxy(22,11);
printf("No book with that title in the catalog.");
getch();
menu();
}
if(strcmp(ptr->title,title)==0){
gotoxy(22,14);
printf("Remove book record? (y/n) ");
if(getch()=='y'){
remove(root,ptr);
menu();
} else {
menu();
}
}
}
}
catalog remove(catalog *root,catalog *x){
catalog *temp;
char y;
if(x==root){
temp=root;
free(temp);
root=NULL;
} else if(x->isbn < root->isbn){
*root->left=remove(root->left,x);
} else if(x->isbn > root->isbn){
*root->right=remove(root->right,x);
} else if(root==NULL){
gotoxy(22,10);
printf("Nothing to remove...");
getch();
menu();
}
printf("The book has been removed.");
printf("Remove another book? (y/n) ");
scanf("%c",&y);
return *x;
}
void modify(){
catalog *ptr;
int num;
char title[20];
system("cls");
if(root==NULL){
gotoxy(22,10);
printf("No records exist...");
getch();
menu();
}
printf("Enter title of book to be modified: ");
scanf("%s",title);
ptr=root;
while(ptr!=NULL){
if(strcmp(ptr->title,title)==0){
printf("Input new information.");
printf("Title: ");
scanf("%s",root->title);
printf("Author: ");
scanf("%s",root->author);
printf("ISBN: ");
scanf("%d",root->isbn);
printf("Publisher: ");
scanf("%s",root->publisher);
printf("Year: ");
scanf("%d",root->year);
printf("Synopsis: ");
scanf("%s",root->synopsis);
printf("The book's information has been modified.");
} else {
printf("No book found.");
break;
}
}
getch();
menu();
}
void searchBook(){
int choice;
char title[20], author[20];
int num;
system("cls");
if(root==NULL){
printf("No records to show...");
getch();
menu();
}
gotoxy(22,10);
printf("-----SEARCH A BOOK-----");
gotoxy(22,12);
printf("1] By title");
gotoxy(22,13);
printf("2] By author");
gotoxy(22,14);
printf("3] By ISBN");
gotoxy(22,15);
printf("4] Back to menu");
gotoxy(22,18);
printf("Enter your choice: ");
scanf("%d",&choice);
switch(choice){
case 1: perTitle();
break;
case 2: perAuthor();
break;
case 3: perISBN();
break;
case 4: perGenre();
break;
case 5: menu();
}
}
void perTitle(){
catalog *ptr;
char title[20], temp[20], ans;
system("cls");
gotoxy(22,15);
printf("Enter book title: ");
scanf("%s",title);fflush(stdin);
ptr=root;
while(ptr!=NULL){
while(strcpy(temp,ptr->title)){
if(strcmp(temp,title)==0){
system("cls");
gotoxy(22,10);
printf("That book is in the catalog.");
gotoxy(22,11);
printf("Title: %s",ptr->title);
gotoxy(22,12);
printf("Author: %s",ptr->author);
gotoxy(22,13);
printf("ISBN: %d",ptr->isbn);
gotoxy(22,14);
printf("Genre: %s",ptr->genre);
gotoxy(22,15);
printf("Publisher: %s",ptr->publisher);
gotoxy(22,16);
printf("Year: %d",ptr->year);
gotoxy(22,17);
printf("Synopsis: %s",ptr->synopsis);fflush(stdout);
break;
} else {
gotoxy(22,10);
printf("No records to show...");
}
}
printf("Try another? (y/n) ");
scanf("%c",&ans);fflush(stdin);
switch(ans){
case 'y': searchBook();
break;
case 'n': menu();
}
}
}
void perAuthor(){
catalog *ptr;
char author[20],au[20],ans;
system("cls");
gotoxy(22,15);
printf("Enter author's name: ");
scanf("%s",author);fflush(stdin);
ptr=root;
while(ptr!=NULL){
while(strcpy(au,ptr->title)){
if(strcmp(au,author)==0){
system("cls");
gotoxy(22,10);
printf("That book is in the catalog.");
gotoxy(22,11);
printf("Title: %s",ptr->title);
gotoxy(22,12);
printf("Author: %s",ptr->author);
gotoxy(22,13);
printf("ISBN: %d",ptr->isbn);
gotoxy(22,14);
printf("Genre: %s",ptr->genre);
gotoxy(22,15);
printf("Publisher: %s",ptr->publisher);
gotoxy(22,16);
printf("Year: %d",ptr->year);
gotoxy(22,17);
printf("Synopsis: %s",ptr->synopsis);
} else {
gotoxy(22,10);
printf("No records to show...");
}
printf("Try another? (y/n) ");
scanf("%c",&ans);
switch(ans){
case 'y': searchBook();
break;
case 'n': menu();
}
}
}
}
void perISBN(){
catalog *ptr;
char isbn[20];
char ans;
system("cls");
gotoxy(22,15);
printf("Enter book ISBN: ");
scanf("%s",isbn);fflush(stdin);
ptr=root;
while(ptr->isbn==isbn){
ptr=ptr->left;
if(ptr==NULL)
menu();
}
system("cls");
if(ptr!=NULL){
gotoxy(22,10);
printf("That book is in the catalog.");
gotoxy(22,11);
printf("Title: %s",ptr->title);
gotoxy(22,12);
printf("Author: %s",ptr->author);
gotoxy(22,13);
printf("ISBN: %d",ptr->isbn);
gotoxy(22,14);
printf("Genre: %s",ptr->genre);
gotoxy(22,15);
printf("Publisher: %s",ptr->publisher);
gotoxy(22,16);
printf("Year: %d",ptr->year);
gotoxy(22,17);
printf("Synopsis: %s",ptr->synopsis);
} else {
gotoxy(22,10);
printf("No records to show...");
}
printf("Try another? (y/n) ");
scanf("%c",&ans);
if(ans=='y' || ans=='Y'){
searchBook();
}
getch();
menu();
}
void perGenre(){
catalog *ptr;
char genre[20],ans;
char gen[10];
system("cls");
gotoxy(22,15);
printf("Enter book title: ");
scanf("%s",genre);
ptr=root;
while(strcmp(ptr->genre,genre)!=0){
ptr=ptr->left;
if(ptr==NULL)
menu();
}
system("cls");
if(ptr!=NULL){
gotoxy(22,10);
printf("That book is in the catalog.");
gotoxy(22,11);
printf("Title: %s",ptr->title);
gotoxy(22,12);
printf("Author: %s",ptr->author);
gotoxy(22,13);
printf("ISBN: %d",ptr->isbn);
gotoxy(22,14);
printf("Genre: %s",ptr->genre);
gotoxy(22,15);
printf("Publisher: %s",ptr->publisher);
gotoxy(22,16);
printf("Year: %d",ptr->year);
gotoxy(22,17);
printf("Synopsis: %s",ptr->synopsis);
} else {
gotoxy(22,10);
printf("No records to show...");
}
printf("Try another? (y/n) ");
scanf("%c",&ans);
switch(ans){
case 'y': searchBook();
break;
case 'n': menu();
}
}
void displayCatalog(catalog *root){
catalog *ptr;
system("cls");
fx=fopen("CAT.dat","rb+");
rewind(fx);
ptr=root;
while(fread(&ptr,structsize,1,fx)==1){
if(root->left!=NULL){
displayCatalog(root->left);
}
printf("Title: %s\n",ptr->title);
printf("Author: %s\n",ptr->author);
printf("ISBN: %s\n",ptr->isbn);
printf("Genre: %s\n",ptr->genre);
printf("Publisher: %s\n",ptr->publisher);
printf("Year: %d\n",ptr->year);
printf("Synopsis: %s\n",ptr->synopsis);
if(root->right!=NULL){
displayCatalog(root->right);
}
break;
}
fclose(fx);
getch();
menu();
}
void gotoxy(int x,int y){
COORD coord;
coord.X=x;
coord.Y=y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
1) You have either turned off all the compiler warnings or you have elected to ignore about 100 compiler warnings.
You should start over, set compiler warning to maximum, or at least 4. Write the program so it compiles with zero warnings.
Few notable errors:
scanf("%d",root->isbn);
isbn is character array, so this should read scanf("%s",root->isbn);
scanf("%d", root->year);
scanf requires address of variable, not copy. It should be scanf("%d", &root->year);
You have many other errors, for example in void modify you try to access uninitialized pointer ptr.
2) You have this gotoxy function, it makes things look very nice, but you should remove it, given that your program does not work at all. If you get your program working without error then you can put back these decorative functions.
3) Try to avoid global variables. FILE *fx, *fy; specially cause problems. You have file handles which are left open, then you try to open them again in a different mode.
catalog *root; is not allocated. The whole program seems to rest on this variable, this will go nowhere. For example
void perTitle()
{
catalog *ptr;
...
ptr = root;
while (ptr != NULL)
{
while (strcpy(temp, ptr->title))
{
...
}
}
}
This may look like a search function but it's just while(0){}, it doesn't do anything.
4) It seems you are managing the records in file, and also storing them in a linked list. This gets very confusing. You should do one or the other. I recommend you store the records in file. Read them in to linked list. And then save them in to file when finished.
5) Use more functions to avoid typing the same thing. For example write a new function showBook
void showBook(catalog *ptr)
{
printf("Title: %s\n", ptr->title);
printf("Author: %s\n", ptr->author);
printf("ISBN: %s\n", ptr->isbn);
printf("Genre: %s\n", ptr->genre);
printf("Publisher: %s\n", ptr->publisher);
printf("Year: %d\n", ptr->year);
printf("Synopsis: %s\n", ptr->synopsis);
}
You can reuse this function in perTitle(), perAuthor(), perISBN(), perGenre() and elsewhere.
6) Initialize data to zero, example:
catalog *ptr = malloc(sizeof(catalog));
memset(ptr, 0, sizeof(catalog));
Start with a small program. Add a few simple function, test it, add more functions. You may want to write a separate test program to test linked lists. Example
void getBook(catalog *ptr)
{
printf("Title:\n");
scanf("%s", ptr->title);
printf("Author:\n");
scanf("%s", ptr->author);
printf("Genre:\n");
scanf("%s", ptr->genre);
printf("ISBN:\n");
scanf("%s", ptr->isbn);
printf("Publisher:\n");
scanf("%s", ptr->publisher);
printf("Year:\n");
scanf("%d", &ptr->year);
printf("Synopsis:\n");
scanf("%s", ptr->synopsis);
}
void addBooks()
{
catalog cat;
memset(&cat, sizeof(catalog), 0);
getBook(&cat);
FILE* fx = fopen("CAT.dat", "ab+");
fseek(fx, 0, SEEK_END);
fwrite(ptr, structsize, 1, fx);
fclose(fx);
printf("Add another (y/n)?\n");
char n;
scanf("%c", &n);
if (n == 'y' || n == 'Y')
addBooks();
_getch();
menu();
}
void displayCatalog()
{
system("cls");
FILE* fx = fopen("CAT.dat", "rb+");
catalog cat;
while (fread(&cat, sizeof(catalog), 1, fx) == 1)
{
showBook(&cat);
break;
}
fclose(fx);
_getch();
menu();
}
void menu()
{
int choice;
system("cls");
printf("----------WELCOME TO THE CARD CATALOG----------\n");
printf("1] Add a book\n");
printf("2] Remove a book\n");
printf("3] Modify a book\n");
printf("4] Search a book\n");
printf("5] Display the catalog\n");
printf("6] Exit\n");
printf("Enter your choice: \n");
scanf("%d", &choice);
switch (choice)
{
case 1: addBooks(); break;
case 2: removeBooks(); break;
case 3: modify(); break;
case 4: searchBook(); break;
case 5: displayCatalog(); break;
case 6: exit(0);
}
}
int main()
{
FILE *f = fopen("CAT.dat", "rb+");
if (f == NULL)
{
f = fopen("CAT.dat", "wb+");
if (f == NULL)
{
printf("Cannot create new file...\n");
return 0;
}
}
fclose(f);
menu();
return 0;
}

can you help me understand about fopen("contact.dll", "r");

I'm trying to implement phonebook in C using data structure.
I found some source code and I'm trying to understand this code but there is really big problem I've never seen. It is dll. I googled about dll but there is nothing related about this. I know the meaning and purpose of dll, but why do we use fopen contact.dll ?
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<process.h>
#include<stdlib.h>
#include<dos.h>
struct contact
{
long phone;
char name[20],add[20],email[30];
} list;
char query[20],name[20];
FILE *fp, *ft;
int i,n,ch,l,found;
int main()
{
main:
system("cls"); /* ************Main menu *********************** */
printf("\n\t **** Welcome to Contact Management System ****");
printf("\n\n\n\t\t\tMAIN MENU\n\t\t=====================\n\t\t[1] Add a new Contact\n\t\t[2] List all Contacts\n\t\t[3] Search for contact\n\t\t[4] Edit a Contact\n\t\t[5] Delete a Contact\n\t\t[0] Exit\n\t\t=====================\n\t\t");
printf("Enter the choice:");
scanf("%d",&ch);
switch(ch)
{
case 0:
printf("\n\n\t\tAre you sure you want to exit?");
break;
/* *********************Add new contacts************ */
case 1:
system("cls");
fp=fopen("contact.dll","a");
for (;;)
{
fflush(stdin);
printf("To exit enter blank space in the name input\nName (Use identical):");
scanf("%[^\n]",&list.name);
if(stricmp(list.name,"")==0 || stricmp(list.name," ")==0)
break;
fflush(stdin);
printf("phone:");
scanf("%ld",&list.phone);
fflush(stdin);
printf("address:");
scanf("%[^\n]",&list.add);
fflush(stdin);
printf("email address:");
gets(list.email);
printf("\n");
fwrite(&list,sizeof(list),1,fp);
}
fclose(fp);
break;
/* *********************list of contacts************************* */
case 2:
system("cls");
printf("\n\t\t================================\n\t\t\tLIST OF CONTACTS\n\t\t================================\n\nName\t\tphone No\t Address\t\tE-mail ad.\n=================================================================\n\n");
for(i=97; i<=122; i=i+1)
{
fp=fopen("contact.dll","r");
fflush(stdin);
found=0;
while(fread(&list,sizeof(list),1,fp)==1)
{
if(list.name[0]==i || list.name[0]==i-32)
{
printf("\nName\t: %s\nphone\t: %ld\nAddress\t: %s\nEmail\t: %s\n",list.name,
list.phone,list.add,list.email);
found++;
}
}
if(found!=0)
{
printf("=========================================================== [%c]-(%d)\n\n",i-32,found);
getch();
}
fclose(fp);
}
break;
/* *******************search contacts********************** */
case 3:
system("cls");
do
{
found=0;
printf("\n\n\t..::CONTACT SEARCH\n\t===========================\n\t..::Name of contact to search: ");
fflush(stdin);
scanf("%[^\n]",&query);
l=strlen(query);
fp=fopen("contact.dll","r");
system("cls");
printf("\n\n..::Search result for '%s' \n===================================================\n",query);
while(fread(&list,sizeof(list),1,fp)==1)
{
for(i=0; i<=l; i++)
name[i]=list.name[i];
name[l]='\0';
if(stricmp(name,query)==0)
{
printf("\n..::Name\t: %s\n..::phone\t: %ld\n..::Address\t: %s\n..::Email\t: %s\n",list.name,list.phone,list.add,list.email);
found++;
if (found%4==0)
{
printf("..::Press any key to continue...");
getch();
}
}
}
if(found==0)
printf("\n..::No match found!");
else
printf("\n..::%d match(s) found!",found);
fclose(fp);
printf("\n ..::Try again?\n\n\t[1] Yes\t\t[0] No\n\t");
scanf("%d",&ch);
}
while(ch==1);
break;
/* *********************edit contacts************************/
case 4:
system("cls");
fp=fopen("contact.dll","r");
ft=fopen("temp.dat","w");
fflush(stdin);
printf("..::Edit contact\n===============================\n\n\t..::Enter the name of contact to edit:");
scanf("%[^\n]",name);
while(fread(&list,sizeof(list),1,fp)==1)
{
if(stricmp(name,list.name)!=0)
fwrite(&list,sizeof(list),1,ft);
}
fflush(stdin);
printf("\n\n..::Editing '%s'\n\n",name);
printf("..::Name(Use identical):");
scanf("%[^\n]",&list.name);
fflush(stdin);
printf("..::phone:");
scanf("%ld",&list.phone);
fflush(stdin);
printf("..::address:");
scanf("%[^\n]",&list.add);
fflush(stdin);
printf("..::email address:");
gets(list.email);
printf("\n");
fwrite(&list,sizeof(list),1,ft);
fclose(fp);
fclose(ft);
remove("contact.dll");
rename("temp.dat","contact.dll");
break;
/* ********************delete contacts**********************/
case 5:
system("cls");
fflush(stdin);
printf("\n\n\t..::DELETE A CONTACT\n\t===============================\n\t..::Enter the name of contact to delete:");
scanf("%[^\n]",&name);
fp=fopen("contact.dll","r");
ft=fopen("temp.dat","w");
while(fread(&list,sizeof(list),1,fp)!=0)
if (stricmp(name,list.name)!=0)
fwrite(&list,sizeof(list),1,ft);
fclose(fp);
fclose(ft);
remove("contact.dll");
rename("temp.dat","contact.dll");
break;
default:
printf("Invalid choice");
break;
}
printf("\n\n\n..::Enter the Choice:\n\n\t[1] Main Menu\t\t[0] Exit\n");
scanf("%d",&ch);
switch (ch)
{
case 1:
goto main;
case 0:
break;
default:
printf("Invalid choice");
break;
}
return 0;
}
Judging by the way the file is used, contact.dll is not an actual Windows DLL, but a datafile that this application used to store phonebook entries.
The various cases add, remove, update, and retrieve list entries from the file. The name given to the file is misleading.

Menu not working properly

#include <stdio.h>
#include <stdlib.h> //for the clear screen function
#include <string.h>
struct customer
{
int custID;
char custName[50];
char custAddress[100];
};
typedef struct customer c;
void load_menu(void);
void customers_menu(void);
void createNew(void); //initialize your file
void add_Customer(c c1[30]); //add a new record to the file
FILE *fp;
int main(void)
{
load_menu();
return 0;
}
void load_menu(void)
{
int choice;
do
{
printf("Customer Orders Main Menu. \n\n");
printf("Please enter your choice: \n");
printf("1. Customer's Menu \n");
printf("2. Orders Menu\n");
printf("3. Product Stock Menu\n");
printf("4. Exit\n");
printf("\n");
if (scanf("%d",&choice)==1)
{
switch(choice)
{
case 1: system ("cls");
customers_menu();
printf("\n");
break;
case 2: system ("cls");
orders_menu();
printf("\n");
break;
case 3: system ("cls");
stock_menu();
printf("\n");
break;
case 4: printf("Quitting program!\n");
break;
default: printf("Invalid choice! Please try again\n");
printf("\n");
break;
}
}
else
{
fflush(stdin);
printf("Characters are invalid, please enter a number: \n ");
choice=0;
}
}while((choice !=4));
}
void createNew(void)
{
FILE *fp;
fp=fopen("Customer.dat", "w");
if (fp==NULL)
printf("File creation failed! \n");
else
{
printf("File created! \n");
fclose(fp);
}
}
void add_Customer (c c1[30])
{
int i, n , cc=0;
FILE *fp;
fp=fopen("Customer.dat", "a");
system("cls");
if(fp==NULL)
{
printf("File Creation Failed!");
}
system("cls");
printf("Enter the number of Customers: ");
scanf("%d", &n);
for(i=0;i<n;i++)
{
printf("Customer's ID (numbers only) : ");
scanf("%d", &c1[i].custID);
printf("Customer's Name : ");
gets(c1[i].custName);
printf("Customer's Address : ");
gets(c1[i].custAddress);
fwrite(&c1[i], sizeof(c), 1, fp);
}cc++;
fclose(fp);
}
void recordCount(c c1[30], int *count)
{
add_Customer(c1);
count=0;
count++;
}
void customers_menu(void)
{
int choice;
c c1[30];
int i;
do
{
printf("\n");
printf("Customers Menu \n\n");
printf("Please enter your choice: \n");
printf("1. Add Customer \n");
printf("2.\n");
printf("3.\n");
printf("4. Go back to Main Menu \n");
recordCount (c1, &i);
if (scanf("%d",&choice)==1)
{
switch(choice)
{
case 1: add_Customer(c1);
createNew();
printf("\n");
break;
case 2:
printf("\n");
break;
case 3:
printf("\n");
break;
case 4: printf("Going back to Main Menu\n");
system ("cls");
break;
default: printf("Invalid choice! Please try again\n");
printf("\n");
break;
}
}
else
{
fflush(stdin);
printf("Characters are invalid, please enter a number: \n ");
choice=0;
}
}while((choice !=4));
I have a problem since when I enter the Customers Menu it is staring to execute case 1 immediately (which still doesn't work properly). Can someone help me fix this error please because I tried everything I know and it is still in vain
I think your issue is that in customers_menu() you output the menu, but do not read the selection, instead you call recordCount() which directly calls addCustomer().
After addCustomer() we return the customers_menu() which then calls scanf() for the long gone menu.
A few other notes:
gets() is not good, I suggest you use scanf() (with %s) instead.
Doing a printf() then clearing the screen is a bit pointless.
Error messages should really go to stderr (fprintf(stderr,...)) rather than stdout (printf(...))
You code is a missing trailing }.
cc is added to, but not used.
This problem coming from if (scanf("%d",&choice)==1) because scanf will not return choice. If you enter valid answer (like number), then it returns 1 and switch case work with 1. I think that's the problem.
If you enter char instead of integer, scanf will return 0.

Resources