•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 456,440 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,616 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Views: 1681 | Replies: 5 | Solved
![]() |
•
•
Join Date: Oct 2007
Posts: 31
Reputation:
Rep Power: 2
Solved Threads: 0
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:
Retriever:
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);
}
} So, apart from
- using gets()
- using void main
- it doesn't compile
- very poor indentation
What is your actual question?
Because it looks to me like you wrote the whole thing in one sitting, tried to compile it, got a whole raft of errors and have now dumped it on a message board for someone else to fix.
- using gets()
- using void main
- it doesn't compile
- very poor indentation
What is your actual question?
Because it looks to me like you wrote the whole thing in one sitting, tried to compile it, got a whole raft of errors and have now dumped it on a message board for someone else to fix.
Back to basics I think?
http://www.daniweb.com/tutorials/tutorial45806.html
>Sorry about the indentation. I got lazy putting the indentations because I usually fix the errors first before I fix the appearance of the code.
Honey, do you know how retarded that sounds? It is like saying "I write sentences without spaces, punctuation or grammar because I find it easier to do all at the end." No!
http://www.daniweb.com/tutorials/tutorial45806.html
>Sorry about the indentation. I got lazy putting the indentations because I usually fix the errors first before I fix the appearance of the code.
Honey, do you know how retarded that sounds? It is like saying "I write sentences without spaces, punctuation or grammar because I find it easier to do all at the end." No!
Last edited by iamthwee : Oct 14th, 2007 at 8:29 am.
... the hat of 'is this a cat in a hat?'
![]() |
•
•
•
•
•
•
•
•
DaniWeb C Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- Projects for the Beginner (Python)
- Dev-C++ - [Build Error] [main.o] Error 1 (C++)
- 2D Game code. All Source files. (Game Development)
- using Graphic.h (C++)
- Tips learning a programming language? (Software Developers' Lounge)
- How to use Dev C++ (C++)
- Newbie needs help with mortgage program (C++)
Other Threads in the C Forum
- Previous Thread: everyone here can i help me?
- Next Thread: Need Help for my assignment...



Linear Mode