I need help in this c program
i need to have a delete function that will delete a specific string(or a line) in my FILE
Is reading the string from the file and putting them in an array, okay?

#include <stdio.h>
#include <string.h>
struct
{
char title[100];
char author[100];
char location[100];
int status;
}book;
char temp[99];
int enterChoice(void);
char ch;


main( void )
{
FILE *open, *readPtr;
int choice, Found; /* user's choice */
char Target[99], stats[5];


open = fopen( "libraryInformation.txt", "a+" );


/* enable user to specify action */
while ( ( choice = enterChoice() ) != 5) {
switch ( choice ) {

/* update record */
case 1:
if((readPtr=fopen("libraryInformation.txt","r"))==NULL)
printf(" ! The File is Empty...\n\n");
else
{
printf("\nEnter The Name Of Book : ");
scanf("%s", &Target);
while(!feof(open) && Found==0)
{
fscanf(readPtr,"%s %s %s %d",book.title,book.author,book.location,book.status);
strcpy(temp, Target);
if (strstr(book.title, temp))
puts("String found");
Found=1;
}
if(Found)
{
if(book.status == 1)
strcpy(stats,"IN");
else
strcpy(stats,"OUT");
printf("\nThe Name of Book is: %s\nThe Author is: %s\nThe Location is: %s\nThe Status: %s",book.title,book.author,book.location, stats);
getch();
}
else if(!Found)
printf("! There is no such Entry...\n");
}
break;
/* create record */
case 2:
//view
while(1)
{
ch=fgetc(open);
if(ch==EOF)break;
printf("%c",ch);
}
getch();


fclose( open );



break;
/* delete existing record */
case 3:
//add
if ((open) == NULL )
{
printf( "File could not be opened...\n" );
} /* end if */

else {
printf( "Enter the title, author, location and status\n");
printf( "Enter EOF to end input.\n");
printf( "Input book entries: ");
scanf( "%s%s%s%d", book.title, book.author, book.location, &book.status);

/* write account, name and balance into file with fprintf */
while( !feof(stdin))
{
fprintf( open, "%s %s %s %d\n", book.title, book.author, book.location, book.status);
printf( "Input another book entries: ");

scanf( "%s%s%s%d", book.title, book.author, book.location, &book.status);

} /* end while */
} //end else
break;
case 4:
puts("Hello4");
// del(open);
break;
case 5:
exit(0);
break;

/* display message if user does not select valid choice */
default:
printf( "Incorrect choice\n" );
getch();
system("cls");
break;
} /* end switch */
} /* end while */

fclose(open); /* fclose closes the file */


return 0;
}




int enterChoice()
{
int menuChoice; /* variable to store user's choice */
system("cls");
/* display available options */
printf( "\nEnter your choice\n"
"1 - Search book entries\n"
"2 - View all book entries\n"
"3 - Add new book entries\n"
"4 - Delete old book entries\n"
"5 - Exit\nChoice: " );

scanf( "%d", &menuChoice ); /* receive choice from user */
return menuChoice;
} /* end function enterChoice */

any help is appreciated.

Recommended Answers

All 2 Replies

I am using Dev-C++ btw

Yes, working with the data inside an array is preferred. Once you start changing data in a file, it's really easy to muck up the file - and then garble the data.

The general technique is:

1) put the data into an array
2) change the data as you want - or mark it as "to be deleted". This might be changing a key number to 0, or -1, or changing the name string to "" or name[0]='\0'.

3) write out the data to a the data file, overwriting the old data.

For critical data, you'd write out the data to a temp file, check it for accuracy, and then remove the old file, and rename the temp file, to the regular data file name. Not necessary here, but having a backup file made first before any changes, would be good.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.