Originally posted by Shadow
Edit your post and repost your code inside code tags.
Refer to Mr Hammer's signature on how to use them.
I appreciate the help Hammer. Learning out of a book can prove quite difficult at times. I have made some chages and I think I am real close to a working function. In my add func() I need a way to read the contents of client.dat to compare against the user input. I have changed to int format using a code to simplify my app. I was wondering if you could take a look at it and see if you know of a way to complete the function. I'm using a temp address to hold the user input then I need to read the file to make sure the code is not already used. If it isn't then the user may continue to input the rest of the required information. Thx.
Code:
/*
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//************************************************
//structure for writing to and reading from file**
struct fclient{
int client_code;
char clientname;
char street_address;
char city;
char pros;
char country;
char postalorzip_code;
int deleted; //used as a boolean (0=false, 1=true)
};
//************************************************
/* FUNCTION PROTOTYPES*********************/
char menu(void);
void add_record(void);
void delete_record(void);
void search(void);
char print_label(void);
void key_wait (void);
/* END OF FUNCTION PROTOTYPES**************/
int main(void)
{
FILE *dta_file;
char choice;
dta_file = fopen("client.dat", "r+b");
if (dta_file == NULL)
{
printf("\n\nFile does not exist: Creating file");
dta_file = fopen("client.dat", "w+b");
key_wait();
}
do
{
choice = menu();
switch (choice)
{
case '1': add_record(); break;
case '2': delete_record(); break;
case '3': search(); break;
case '4': display(); break;
case '5': print_label(); break;
}
}
while (choice != '6');
fclose(dta_file);
return 0;
}
/ **************************************************
**********/
char menu (void)
/*
Task: display a menu and return the choice of the user
*/
/ **************************************************
**********/
{
char choice;
printf("\t\t\tClient Inventory Management \n\n\n");
printf("\t1. Add a Client\n\n");
printf("\t2. Delete a Client\n\n");
printf("\t3. Search for a Client\n\n");
printf("\t4. Display all Clients\n\n");
printf("\t5. Print shipping label\n\n");
printf("\t6. Exit the program\n\n\n");
printf("\tPlease enter your selection: ");
do
{
choice = getch();
} while (choice < '1' || choice > '7');
return (choice);
}
/ **************************************************
**********/
void add_record(void)
/*
Task: Read from keyboard and add the record in the file
*/
/ **************************************************
**********/
{
FILE *dta_file;
int client_code;
char clientname;
char street_address;
char city;
char pros;
char country;
char postalorzip_code;
int clientunique=1;//flag for client code validation
int client_code2;
if (dta_file!=NULL)
{
dta_file = fopen("client.dat","r+b");
}
// Validate the client code************************
while(!clientunique)
{
clientunique=1;
printf("Client code: ");
scanf("%d", &client_code2);
while(dta_file != NULL)
{
******//fread( ???....this is where i don't know what to do*****
if (client_code2 != client_code)
{
clientunique = 0;
}
dta_file=dta_file;
fprintf(dta_file,"%d",client_code);
printf("Client name: ");
scanf("%s",&clientname);
fprintf(dta_file,"%s",clientname);
printf("\n");
printf("Street address: ");
scanf("%s",&street_address);
fprintf(dta_file,"%s",street_address);
printf("\n");
printf("City: ");
scanf("%s",&city);
fprintf(dta_file,"%s",city);
printf("\n");
printf("Province or State: ");
scanf("%s",&pros);
fprintf(dta_file,"%s",pros);
printf("\n");
printf("Country: ");
scanf("%s",&country);
fprintf(dta_file,"%s",country);
printf("\n");
printf("Postal or Zip code: ");
scanf("%s",&postalorzip_code);
fprintf(dta_file,"%s",postalorzip_code);
if (!clientunique)
{
printf("This client name is taken. Please enter a different name...Press a key\n");
key_wait();
}
fclose(dta_file);
}
}
*/