C: Question on file handling

In summary: I hope you stick around and have a great learning and helping experience ##-## it's a really great site!In summary, the conversation discusses how to delete a specific record from a text file using a field from a struct. The suggested solution involves using file handling and searching algorithms. One member suggests reading and writing the records to a new file while checking for matches, and another member offers to write the code if needed. However, it is important to follow the rules of the forum regarding homework help.
  • #1
gruba
206
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
  • #2
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
  • #3
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
  • #4
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

1. What is C: Question on file handling?

C: Question on file handling refers to the topic of handling files in the C programming language. It involves reading, writing, and manipulating files on a computer's file system.

2. How do I open a file in C?

To open a file in C, you can use the fopen() function. This function takes in two arguments: the name of the file and the mode in which you want to open the file (e.g. read, write, append). It returns a file pointer which can be used to perform operations on the file.

3. How do I read from a file in C?

To read from a file in C, you can use the fscanf() or fgets() functions. These functions take in the file pointer and the data type of the variable you want to read the data into. They will read data from the file and store it in the specified variable.

4. How do I write to a file in C?

To write to a file in C, you can use the fprintf() or fputs() functions. These functions take in the file pointer and the data you want to write to the file. They will write the data to the file at the current position of the file pointer.

5. How do I close a file in C?

To close a file in C, you can use the fclose() function. This function takes in the file pointer and closes the file. It is important to close files after you are done using them to avoid any data loss or corruption.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
827
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
15
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
14
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
3K
  • Programming and Computer Science
Replies
1
Views
347
  • Engineering and Comp Sci Homework Help
Replies
3
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
Back
Top