0

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;
}
2
Contributors
1
Reply
3
Views
7 Years
Discussion Span
Last Post by Adak
1

It's dicey to overwrite data in a file - any error, even a disk error, can ruin the entire file, until the file is rebuilt. That is a time consuming process, which may not fix all the data, anyway.

So what to do?

Most programs do it this way: When a record should be deleted, you don't actually delete it, you just mark the record ID number field (or maybe a name field), with a 0 (zero). The record space is still there, but it won't be shown on any searches or print outs. You make adjustments in those functions to ensure that is the case.

Also, your program should have logic to let it know to look for a zero'ed out record, when it needs to add a record. If the program detects too many empty records, (it counts them on start up each time), then it will compress them, at a convenient time (maybe the middle of the night, or when you try to close it down, next time). The idea is that instead of shifting all these records "in place", it will simply write all the good records into a new file, and then re-name that file, to it's current file name.

Do you see the difference? Instead of 25 overwriting operations, each involving moving say, half the records in the data file each time, you will have *maybe* one re-writing operation, which can take place at a time of your choosing.

Now, a few comments on your code:

1) gets() is very unsafe - never use it. Use fgets() and specify your maximum length
2) Your students fields: name, etc., should be part of a struct - that IS your record, in C, and keeps all students fields, organized and together.

3) fflush(stdin), works on output streams, only. Not input. Use getchar() to pull any unwanted newlines off the keyboard buffer. (like after every scanf().

run this snippet to see why:

int digit;
do {
  scanf("%d", &digit)

}while(digit < 0 || digit > 9);

Here, I want the user to give me a number from 1 to 9. But if the user puts in a LETTER, instead of a number, the loop will continue endlessly. :(

Now add this line right after the scanf() line of code:

getchar();

and see the difference - amazing! :) No more endless looping, because the getchar() is pulling the newline off the keyboard buffer.

Scanf() is not the best way to get input, but if you're going to use it, use a getchar(), right after it. It cures many ills, just by getting the newline off the input stream.

Votes + Comments
Excellent :)
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.