Hey im kinda new in file handling so can you guys help me out im having a hard time delete data from my .txt file the one here in my code workes but when I example
printf("Enter Record to be deleted: ");
scanf("%s",del);
if(strcmp(line, del) !=0)
the data that i want to delete dosent get deleted by if i use
if(strcmp(line, "popo") !=0)
it gets deleted can you guys help me out and give me some tips on how to improve my program tnx in advance ^^
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
char add ='y';
char name[50], address[50], gender, birthDay[20];
int schoolYear;
int choice;
FILE *pFile;
char user[10], pass[10];
int c, d, i;
printf("Username: ");
gets(user);
printf("Password: ");
gets(pass);
d=strcmp(pass,"popo");
c=strcmp(user,"ako");
if ((c==0)&&(d==0))
{
printf(" Welcome To This Student Database \n\n");
}
else
{
return 0;
}
do
{
printf(" [1] ADD a Student \n");
printf(" [2] View a Student \n");
printf(" [3] Delete a Student \n");
printf(" [4] Exit \n");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
pFile = fopen("student.txt", "a");
if(pFile != NULL)
{
while(add =='y')
{
fflush(stdin);
printf("Name: ");
gets(name);
printf("Address: ");
gets(address);
fflush(stdin);
printf("Birthday: ");
gets(birthDay);
fflush(stdin);
printf("Gender: ");
scanf("%c",&gender);
printf("School Year: ");
scanf("%d",&schoolYear);
fflush(stdin);
fprintf(pFile,"%s\n",name);
fprintf(pFile,"%s\n",address);
fprintf(pFile,"%s\n",birthDay);
fprintf(pFile,"%c\n",gender);
fprintf(pFile,"%d\n",schoolYear);
printf(" Add More Students? [y/n]\n");
add = getch();
}
fclose(pFile);
}
else
{
printf("Could not open the file. \n");
}
break;
}
case 2:
{
pFile = fopen("student.txt", "r");
if(pFile != NULL)
{
while(!feof(pFile))
{
fscanf(pFile,"%s %s %s %c %d",name,address,birthDay,&gender,&schoolYear);
printf("%s",name);
printf("%s",address);
printf("%s",birthDay);
printf("%c",gender);
printf("%d",schoolYear);
}
fclose(pFile);
}
else
{
printf("Could not open the file. \n");
}
break;
}
case 3:
{
FILE *pFile;
char line[21];
char *buffer;
char *ptr;
buffer = (char *)malloc(1000*sizeof(char));
memset(buffer,0,1000*sizeof(char));
ptr = buffer;
pFile = fopen("student.txt","r");
if(pFile != NULL)
{
while(!feof(pFile))
{
fgets(line, 21, pFile);
if(strcmp(line, "anjo") !=0)
{
strcpy(ptr, line);
ptr += strlen(line);
}
}
fclose(pFile);
pFile =fopen("student.txt","w");
fprintf(pFile, "%s",buffer);
fclose(pFile);
}
else
{
printf("Could not open the file. \n");
}
}
break;
}
system("pause");
system("cls");
}while(choice !=4);
getch();
return 0;
}