How Can You Delete a Record from a Text File Using a Struct Field?

AI Thread Summary
To delete a specific record from a text file using a struct field, one must read each record into memory and check if it matches the field to be deleted. If a match is found, that record is skipped; otherwise, it is written to a new file. After processing all records, the original file is deleted, and the new file is renamed to replace it. This method requires understanding file handling and searching algorithms. Adhering to forum rules about homework assistance is also emphasized.
gruba
Messages
203
Reaction score
1

Homework Statement


How to delete specific record from a text file by some field from struct?

Homework Equations


-File handling
-Searching algorithms

The Attempt at a Solution


I know that one of the searching algorithms is needed, but could someone explain how to
delete found record from file?
 
Physics news on Phys.org
gruba said:

Homework Statement


How to delete specific record from a text file by some field from struct?

Homework Equations


-File handling
-Searching algorithms

The Attempt at a Solution


I know that one of the searching algorithms is needed, but could someone explain how to
delete found record from file?
Here's what I would do:
While the applicable field is not found:
1. Read each record from the file into memory.
2. If the field you're looking for does NOT match the value you're looking for, write the record to the file; otherwise (not a match) don't do anything.
 
  • Like
Likes sysprog
Hi,
You can do this by simply read the records from the file and start writing it to another file while doing that check read data matches the data to be deleted. If matched then ignore it continue reading the file and writing to another file. At the end delete the file we read and rename the file we written as old one.
For your references
Fopen in c [1] [https://fresh2refresh.com/c-programming/c-file-handling/fopen-fclose-gets-fputs-functions-c/]
fgetc [1] [2]
fputc[1][https://fresh2refresh.com/c-programming/c-file-handling/fputc-function-c/]
Fclose in c [1] [2]
Please reply If you want me to write the code for you.
Thanks regards.
 
  • Like
Likes sysprog
rusahupe said:
Please reply If you want me to write the code for you.
Hi @rusahupe, it's nice of you to want to help out ##-## I'm not a Mentor/Moderator here, but given that you're new here, I'd like to gently point out that there are rules on the Physics Forums that place some carefully-thought-out limits on how members may help with homework, and it seems to me that simply writing the code for someone, without first seeing a genuine attempt from that someone, might be out of bounds ##-## you can read the rules at the Terms link in the footer of every page, and if you're unsure about something, you can PM one of the Mentors . . .
 
  • Like
Likes rusahupe and berkeman
Back
Top