I'm still confused with file i/o here and needs a bit of help on fixing the codes I have. Can anyone offer some help?
I have two codes here that sort of work together. One is a database type of code that is supposed to get input form the user about some information and then save them in a text file, while the other is supposed to retrieve that saved file and show the information that was recorded. These information also needs to be sorted alphabetically, so somehow I feel that I need a few more functions to work with to make the program work in Dev C++
Here are the codes so far:
Database:
#include <stdio.h>
void fileChecker (char filename[20]);
char choice, menu, restart, filename[20], option, buffer;
int i, j, n, m, cnt = 0, flag;
FILE *fp;
struct record{
char lastName[20];
char firstName[20];
char cacNum[10];
} , tmp;
void main(int argc, char *argv[])
{
clrscr();
printf("Welcome to the Student Database!");
printf("\n\nPlease enter the following information:");
n = 0;
do{
printf("\n\nLastname: ");
printf("Firstname: ");
gets(student[n].firstname);
printf("CAC Number: ");
do{
printf("\nDo you wish to add another entry? (y/n) ");
choice = tolower(());
if(choice) == 'y')
{
n++;
}
}
}while(choice != 'n');
n++;
if(argc > 1)
{
(filename, argv[1];
fileChecker(filename);
}
else
{
printf(\n\nYou have not provided the Record Filename...");
retrieverFilename();
}
fcloseall();
printf("\nSaving Completed!");
printf("\nThank You!");
do{
print("\n\nDo you wish to restart the program? (y/n) ");
restart = (getche());
}
}
else
{
printf("\nGoodbye!");
exit(1);
}
}
void retrieverFilename()
{
printf('\n\nEnter the Record Filename: ");
gets(filename);
fileChecker(filename);
}
void fileChecker (char filename[20])
{
if((fp = fopen(filename, "r")) == NULL)
{
printf("\nDo you wish to \n(a) enter filename, \n(b)create a new file, or \n(c)exit the program\n? (a/b/c) ");
option = tolower(getche());
if(option == 'a')
{
retrieverFilename();
}
else if(option == 'b')
{
printf("\nCreating a New Record File...");
fp = fopen(filename, "w+t");
sorter();
storer();
}
else if(option == 'c')
{
printf("Goodbye!");
exit(1);
}
}while(option != 'a' && option != 'b' && option != 'c');
}
else
{
printf("\n\nRecord File already exists!");
do{
printf("\nDo you wish to
menu = tolower(getche());
}while(menu != 'a' && menu != 'b' && menu != 'c' && menu != 'd');
if(menu == 'a')
{
}
else if(menu == 'b')
{
fclose(fp);
fp = =fopen(filename, "a+t");
printf("\nWill append to existing file...");
sorter();
while(cnt == 0)
{
buffer = fgetc(fp);
if(buffer == ';')
{
while(flag == 0)
{
buffer = fgetc(fp);
ifbuffer == ';')
{
student[m].lastName[i] = '\0';
i = 0;
flag = 1;
}
else if(buffer == EOF)
{
cnt = 1;
break;
}
{
student[m].firstname[i] = buffer;
i++;
}
}
while(flag == 2)
{
buffer = fgetc(fp);
if(buffer == '\n')
{
student[m].cacNum[i] = '\0';
i = 0;
flag = 0;
}
else if(buffer == EOF)
{
cnt = 1;
break;
}
else
{
student[m].cacNum[i] = buffer;
i++;
}
}
m++;
}
else
{
break;
}
}
fclose(fp);
n = m;
// sorter();
fp = fopen(t");
storer();
}
else if(menu == 'c)
{
retrieverFilename();
}
else
{
printf("\nGoodbye!");
exit(1);
}
}
}
void sorter()
{
for(i = 0; i < n; i++)
{
for(j = 0; j < n; j++)
{
if(strcmp(student[i].lastName, student[j].lastName) < 0)
{
strcpy( tmp.lastName, student[i].lastName);
strcpy(student[i].lastName, student[j].lastName);
strcpy(student[j].lastname, tmp.lastName);
}
}
}
}
}
Retriever:
#include <ctype.h>
void retrieverFilename(void);
void fileChecker(char filename[20]);
void retriever(void);
void searcher(void);
char restart, filename[20], option, buffer ,searchKey[20];
int i, j, n, cnt = 0, flag;
FILE *fp;
struct record{
char lastName[20];
char firstName[50];
char cacNum[10];
else
{
printf("\n\nYou have not not provided the Record Filename..");
retrieverFilename();
}
retriever();
do{
searcher();
printf("\n\nSearch completed!");
printf(\nThank You!");
do{
printf("\n\nDo you wish to \n(a)search another entry in the same file \n (b)use another file or \n(c)exit the program\n? (a/b/c) ");
restart - tolower(getche());
if(restart == 'a')
{
clrscr();
}
else if(restart == 'b')
{
main(0, '\0');
}
else if(restart == 'c')
{
printf("\nGoodbye!");
exit(1);
}
{
printf("\n\nEnter the Record Filename: ");
gets(filename);
fileChecker(filename);
}
void fileChecker(char filename[20])
{
}
else
{
return;
}
}
void retriever()
{
n = 0;
flag = 0;
i = 0;
while(cnt == 0)
{
buffer = fgetc(fp);
if(buffer == ';')
{
}
else
{
student[n].firstName[i] = buffer;
}
}
while(flag == 2)
{
buffer = fgetc(fp);
if(buffer == '\n')
{
i = 0;
flag = 0;
}
i++;
}
}
n++;
}
else
{
break;
}
}
}
if(j == 0)
{
printf("\nNo such record exists...");
}
else
{
print("\Retrieved %d records containing the family name %s.", j, searchKey);
}
}