PDA

View Full Version : C help!! Prog Wont run!!


MasterKin
September 30th, 2002, 16:52
hi need help...

my teach asked me for a program to let me input names kinda record style with the ff options... input, delete, edit, search..


anyway got all working except the edit... can someone pls help me...


thanks..

here is the code i made

#include<stdio.h>
#include<string.h>
#include<conio.h>
struct my_project
{
char name[50],age[5],address[50],
phone[50];
} data[3];
int list_info=0;
int c;
char ch;
void ADD_ENTRY(void);
void SEARCH(void);
void DELETE(void);
void EDIT(void);
void main(void)
{
FILE*fptr;
fptr=fopen("tc.txt","a+");
do
{
clrscr();
gotoxy (25,6);printf("Main Menu");
gotoxy (25,8);printf("[1] Add Entry");
gotoxy (25,9);printf("[2] Edit");
gotoxy (25,10);printf("[3] Search By Name");
gotoxy (25,11);printf("[4] Delete");
gotoxy (25,12);printf("[5] Exit");
gotoxy (18,14);printf("Press The Corresponding # of your choice:");
ch=getch();
switch(ch)
{
case '1': ADD_ENTRY();
getch(); break;
case '2': EDIT();
getch(); break;
case '3': SEARCH();
getch(); break;
case '4': DELETE();
getch(); break;
}
}while (ch!='5');
fscanf(fptr,"%s%s%s%s",data[list_info].name,data[list_info].address,data[list_info].age,data[list_info].phone);
fclose(fptr);
list_info++;

}
void ADD_ENTRY(void)
{
FILE*fptr;
fptr=fopen("tc.txt","a+");
clrscr();
gotoxy (25,5);printf(" Add Your Entry Here");
gotoxy (25,7);printf("Name:");
gets(data[list_info].name);
gotoxy (25,8);printf("Age:");
gets(data[list_info].age);
gotoxy (25,9);printf("Address:");
gets(data[list_info].address);
gotoxy (25,10);printf("Phone:");
gets(data[list_info].phone);
fprintf(fptr,"%s%s%s%s",data[list_info].name,data[list_info].address,data[list_info].age,data[list_info].phone);
list_info++;
fclose(fptr);
}
void SEARCH(void)
{
char find=0,count=0,ngalan[10];
clrscr();
gotoxy (25,7);printf("Searching For Somebody's File?:");
gotoxy (25,9);printf("Enter The Name:");
gets(ngalan);
clrscr();
for (count=0;count<list_info;count++)
{
if (strcmp(ngalan,data[count].name)==0)
{
find=1;
break;
}
}
if (find==1)
{
gotoxy (25,8);printf("Name: %s",data[count].name);
gotoxy (25,9);printf("Age: %s",data[count].age);
gotoxy (25,10);printf("Address: %s",data[count].address);
gotoxy (25,11);printf("Phone: %s",data[count].phone);
}
else
{
gotoxy (25,7);printf("Sorry...");
gotoxy (25,8);printf("File Not Found!");
}
}
void DELETE(void)
{
char find=0,count=0,x,ngalan[10];
clrscr();
gotoxy (5,3);
printf("Delete Entry");
gotoxy (12,5);
printf("Enter the name:");
gets(ngalan);
for (count=0;count<list_info;count++)
{
if (strcmp(ngalan,data[count].name)==0)
{
find=1;
break;
}
}
if (find==1)
{
printf("\nName: %s",data[count].name);
printf("\nAge: %s",data[count].age);
printf("\nAddress: %s",data[count].address);
printf("\nPhone: %s",data[count].phone);
printf("\nPress any key to delete...");
getch();
for (x=count;x<list_info-1;x++)
{
strcpy(data[x].name,data[x+1].name);
strcpy(data[x].age,data[x+1].age);
strcpy(data[x].address,data[x+1].address);
strcpy(data[x].phone,data[x+1].phone);
}
list_info--;
printf("\n\n%s's file has been successfully deleted!",ngalan);
}
else
{
printf("File not found!");
}
}
void EDIT(void)
{
char find=0,count=0,pangan[20];
FILE*fptr;
fptr=fopen("project.txt","a+");
clrscr();
for (count=0;count<list_info;count++)
{
gotoxy (25,8+count);printf("Name: %s",data[count].name);
}
gotoxy (25,15);printf("Type first his/ her name.");
gotoxy (25,16);printf("Enter name: ");
gets(pangan);
for (count=0;count<list_info;count++)
{
if (strcmp(pangan,data[count].name)==0)
{
find=1;
break;
}
}
if (find==1)
{
clrscr();
gotoxy (25,8);printf("Name: %s",data[count].name);
gotoxy (25,9);printf("Age: %s",data[count].age);
gotoxy (25,10);printf("Address: %s",data[count].address);
gotoxy (25,11);printf("Phone: %s",data[count].phone);
gotoxy (25,13);printf("Press any key to edit...");
ch=getch();
gotoxy (31,8);gets(data[count].name);
gotoxy (30,9);gets(data[count].age);
gotoxy (33,10);gets(data[count].address);
gotoxy (32,11);gets(data[count].phone);
gotoxy (25,13);printf("%s's file has been successfully editted...",pangan);

}
else
{
clrscr();
gotoxy (25,7);printf("Sorry...");
gotoxy (25,8);printf("File Not Found!");
}

fscanf(fptr,"%s%s%s%s",data[list_info].name,data[list_info].address,data[list_info].age,data[list_info].phone);
list_info++;
fclose(fptr);
}

ammoQ
September 30th, 2002, 23:11
Uh-Oh, this doesn't look like you know what you are doing... Especially the file operations are a big mess.
You keep the database in the variable "data", and also in a file - but I think only inserting new records is done correctly in the database. Unfortunately, you never read the file in a good place. Reading is done in EDIT (missplaced) and at the end of main, after the main loop (missplaced). But this doesn't matter since you always open the file with the "a+" mode which positions the stream at the end of the file, where you will never find data to read.

To fix this mess, you could make a function "read_data" that reads all data into the field "data"; and another function "write_data" that writes the whole contents of "data" to the file.
Call "read_data" at the beginning of main, before the "do{}while()"-loop; call write_data after every operation (insert, edit, delete). (you keep ADD_ENTRY the way it is, since it seems to work).

I'll try some code snipplets for read_data und write_data, but since I did neighter compile nor test them, expect bugs...

void read_data() {
list_info=0;
FILE*fptr;
fptr=fopen("project.txt","r");
do {
fscanf(fptr,"%s%s%s%s",data[list_info].name,data[list_info].address,data[list_info].age,data[list_info].phone);
if (!feof(fptr))
list_info++;
} while (!feof(fptr));
fclose(fptr);
}

void write_data() {
int i;
FILE*fptr;
fptr=fopen("project.txt","w");
for (i=0; i&lt;list_info; i++) {
fprintf((fptr,"%s%s%s%s\n",data[i].name,data[i].address,data[i].age,data[i].phone);
}
fclose(fptr);
}


Be aware that the whole stuff is unsafe, e.g. if you enter blanks into a data field, fscanf() will not read it back correctly, but split the string instead, so your fields get messed up. It would be better to write the fields seperated with tabs ("\t") and improve read_data() to read whole strings using fgets() and seperate the fields at the tabs. One more hint: The whole program contains so many possibilities for buffer overflows that it means a lot of work to fix them all.

NickK
September 30th, 2002, 23:32
I added a couple of comments where you might have some problems. The reason why edit doesn't work is because you open the file 'project.txt' with the append option which puts the file pointer at the end of the file and not the beginning. So when you make the call to fscanf (at the bottom of the edit function), you'll get an error and nothing will be modified.

Also try to avoid opening the same file more than once. You really shouldn't do that, and on some systems trying to do that will result in an error.

<pre><font size="2">
struct
{
char name[50],age[5],address[50],
phone[50];
} data[3];

int list_info=0;
int c;
char ch;

void ADD_ENTRY(FILE *fptr);
void SEARCH(void);
void DELETE(void);
void EDIT(void);

void main(void)
{
FILE*fptr;
fptr=fopen("tc.txt","a+");
do
{
clrscr();
gotoxy (25,6);
printf("Main Menu");
gotoxy (25,8);
printf("[1] Add Entry");
gotoxy (25,9);
printf("[2] Edit");
gotoxy (25,10);
printf("[3] Search By Name");
gotoxy (25,11);
printf("[4] Delete");
gotoxy (25,12);
printf("[5] Exit");
gotoxy (18,14);
printf("Press The Corresponding # of your choice:");

ch = getch();
switch (ch)
{
case '1':
ADD_ENTRY(fptr);
break;

case '2':
EDIT();
break;

case '3':
SEARCH();
break;

case '4':
DELETE();
break;
}

getch();

} while (ch != '5');

fscanf(fptr, "%s%s%s%s", data[list_info].name,data[list_info].address,data[list_info].age,data[list_info].phone);

fclose(fptr);
list_info++;
}

void ADD_ENTRY(FILE *fptr)
{
clrscr();
gotoxy (25,5);
printf(" Add Your Entry Here");
gotoxy (25,7);
printf("Name:");
gets(data[list_info].name);
gotoxy (25,8);
printf("Age:");
gets(data[list_info].age);
gotoxy (25,9);
printf("Address:");
gets(data[list_info].address);
gotoxy (25,10);
printf("Phone:");
gets(data[list_info].phone);

fprintf(fptr,"%s %s %s %s\n",data[list_info].name,data[list_info].address,data[list_info].age,data[list_info].phone);
list_info++;
}

/* Gets the index in the data table where an entry contains
'name.' Returns 0 on failure and non zero on success. */

int getEntryIndex(char *name, int *index)
{
int found = 0;
int i;

for (i = 0; i &lt; list_info && !found; i++)
{
if (strcmp(name, data[i].name)==0)
{
found = 1;
*index = i;
}
}

return found;
}

void SEARCH(void)
{
int found = 0;
int count;
char ngalan[10];

clrscr();
gotoxy (25,7);
printf("Searching For Somebody's File?:");
gotoxy (25,9);
printf("Enter The Name:");
gets(ngalan);
clrscr();

if (!getEntryIndex(ngalan, &count))
{
gotoxy (25,7);
printf("Sorry...");
gotoxy (25,8);
printf("File Not Found!");
} else
{
gotoxy(25,8);
printf("Name: %s",data[count].name);
gotoxy(25,9);
printf("Age: %s",data[count].age);
gotoxy(25,10);
printf("Address: %s",data[count].address);
gotoxy(25,11);
printf("Phone: %s",data[count].phone);
}
}

void DELETE(void)
{
int count;
int x;
char ngalan[10];

clrscr();
gotoxy (5,3);
printf("Delete Entry");
gotoxy(12,5);
printf("Enter the name:");
gets(ngalan);

if (getEntryIndex(ngalan, &count))
{
printf("\nName: %s",data[count].name);
printf("\nAge: %s",data[count].age);
printf("\nAddress: %s",data[count].address);
printf("\nPhone: %s",data[count].phone);
printf("\nPress any key to delete...");
getch();

for (x = count; x >= 0; x--)
{
if (x + 1 < list_info)
{
strcpy(data[x].name,data[x+1].name);
strcpy(data[x].age,data[x+1].age);
strcpy(data[x].address,data[x+1].address);
strcpy(data[x].phone,data[x+1].phone);
}
}

list_info--;
printf("\n\n%s's file has been successfully deleted!",ngalan);
} else
printf("File not found!");
}

/* Did you want to open project.txt or tc.txt here? */
void EDIT(void)
{
int count;
char pangan[20];
FILE*fptr;

fptr=fopen("project.txt","a+");
clrscr();
for (count = 0; count &lt; list_info; count++)
{
gotoxy (25,8 + count);
printf("Name: %s",data[count].name);
}

gotoxy (25,15);
printf("Type first his/ her name.");
gotoxy (25,16);
printf("Enter name: ");
gets(pangan);

if (getEntryIndex(pangan, &count))
{
clrscr();
gotoxy(25,8);
printf("Name: %s",data[count].name);
gotoxy(25,9);
printf("Age: %s",data[count].age);
gotoxy(25,10);
printf("Address: %s",data[count].address);
gotoxy(25,11);
printf("Phone: %s",data[count].phone);
gotoxy(25,13);
printf("Press any key to edit...");
ch = getch();
gotoxy(31,8);
gets(data[count].name);
gotoxy(30,9);
gets(data[count].age);
gotoxy(33,10);
gets(data[count].address);
gotoxy(32,11);
gets(data[count].phone);
gotoxy(25,13);
printf("%s's file has been successfully editted...",pangan);
} else
{
clrscr();
gotoxy (25,7);
printf("Sorry...");
gotoxy (25,8);
printf("File Not Found!");
}

/************
Here's a problem. When you open 'project.txt' with
the append option, it starts reading at the end of the
file. I'm not sure where you wanted to read this info
from, so I'll let you fix it with a fseek or something.
****/

fscanf(fptr,"%s%s%s%s",data[list_info].name,data[list_info].address,data[list_info].age,data[list_info].phone);

list_info++;

fclose(fptr);
}
</font>
</pre>

MasterKin
October 1st, 2002, 05:08
thanks a lot!!!

:)

neway still having probs...

the reason i was trying to open tc was because i was trying to put all the records in that file...

so when i quit turbo c when i open the tc file i can get the records and view them with another prog.

can you help me with that?