User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Oct 2007
Posts: 31
Reputation: gallantmon1 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
gallantmon1 gallantmon1 is offline Offline
Light Poster

Tips in File I/O in Dev C++

  #1  
Oct 14th, 2007
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);
                }
                }
Attached Files
File Type: cpp Database.cpp (3.0 KB, 2 views)
File Type: cpp Retriever.cpp (2.2 KB, 1 views)
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2005
Posts: 3,834
Reputation: Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of 
Rep Power: 23
Solved Threads: 436
Colleague
Salem's Avatar
Salem Salem is offline Offline
banned

Re: Tips in File I/O in Dev C++

  #2  
Oct 14th, 2007
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.
Reply With Quote  
Join Date: Oct 2007
Posts: 31
Reputation: gallantmon1 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
gallantmon1 gallantmon1 is offline Offline
Light Poster

Re: Tips in File I/O in Dev C++

  #3  
Oct 14th, 2007
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.

My question is what other things do I have to add or delete so that the program will compile?
Reply With Quote  
Join Date: Aug 2005
Posts: 4,832
Reputation: iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light 
Rep Power: 17
Solved Threads: 324
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Industrious Poster

Re: Tips in File I/O in Dev C++

  #4  
Oct 14th, 2007
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!
Last edited by iamthwee : Oct 14th, 2007 at 8:29 am.
... the hat of 'is this a cat in a hat?'
Reply With Quote  
Join Date: Sep 2004
Posts: 6,515
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 31
Solved Threads: 488
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: Tips in File I/O in Dev C++

  #5  
Oct 14th, 2007
>I usually fix the errors first before I fix the appearance of the code.
The appearance of the code helps you find errors, genius.
I'm here to prove you wrong.
Reply With Quote  
Join Date: Oct 2007
Posts: 31
Reputation: gallantmon1 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
gallantmon1 gallantmon1 is offline Offline
Light Poster

Re: Tips in File I/O in Dev C++

  #6  
Oct 14th, 2007
Sorry about that. I'm just really confused and stressed when I typed that, my bad.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 1:43 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC