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

Click For Summary

Discussion Overview

The discussion centers around the problem of deleting a specific record from a text file using a field from a struct in a programming context. Participants explore methods related to file handling and searching algorithms, focusing on how to implement the deletion process effectively.

Discussion Character

  • Homework-related
  • Technical explanation
  • Exploratory

Main Points Raised

  • One participant asks for clarification on how to delete a found record from a file, indicating that a searching algorithm is necessary.
  • Another participant suggests reading each record into memory and writing only those that do not match the specified field to the file, implying a method for deletion.
  • A third participant proposes a method of reading records from the original file and writing them to a new file while checking for matches, stating that matched records should be ignored.
  • There is an offer to provide code assistance, but a reminder is given about the forum's rules regarding homework help and the importance of showing a genuine attempt first.

Areas of Agreement / Disagreement

Participants generally agree on the need for a searching algorithm and the approach of using a temporary file for deletion, but there is no consensus on the specific implementation details or the appropriateness of providing complete code solutions.

Contextual Notes

There are limitations regarding the assumptions made about the structure of the records and the specific field used for deletion. The discussion does not resolve the exact steps or methods for file handling and deletion.

Who May Find This Useful

Individuals interested in programming, particularly in file handling and data manipulation, may find this discussion relevant.

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   Reactions: 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   Reactions: 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   Reactions: rusahupe and berkeman

Similar threads

  • · Replies 2 ·
Replies
2
Views
1K
Replies
1
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 15 ·
Replies
15
Views
3K
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 3 ·
Replies
3
Views
6K
  • · Replies 5 ·
Replies
5
Views
4K