Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You are reading only 5 characters at a a time. While this will work (because fgets will stop at the end of a line), it's very inefficient and means you are comparing the users input to every 6 characters of a file, even when those file contents are not the student id.</p> <p>If you do want to continue with the approach of your program, when you do get a match with the user input, you need to read (and discard) the rest of the line before continuing examining further lines.</p> <p>For lines that don't match, you should read (and copy into the new file) the remainder of the line without comparing it to the user input (since you know it is not the student id).</p> <p>I suspect the person who wrote the assignment expected you to read an entire line in, split it (by looking for the commas) into the various fields and put the information into your studentinfo structures. Then process the studentinfo in whatever way the assignment requested, and finally write the new file with the modified data.</p> <p>Although you can make your approach work for deleting a record of a specified student id, it is very inflexible. Searching for a record, or adding a record would require a complete rewrite of your program. If you had code that could read the information into an array of studentinfo structs, and write that info out again, any processing you needed to do would just work on those structs and the changes would be much smaller.</p> <p>So, in pseudo code, you want something like this</p> <pre><code>allocate space for one line of the file allocate space for an array of struct studentinfos readinfo function: open the student info file for reading set the count of student records to 0 while not at eof read in a line split the line on commas copy the bit before the first comma to the 'id' field of the newly allocated studentinfo record copy the bit between first and second commas to the name field copy the bit from the second comma to the course field add one to the count of student records go back to read another line close the file writeinfo function: open the studentinfo file for writing loop over the studentinfo structs in order writeout the id, name and course strings of the current record, separated by comma and followed by new line close the file deletestudent function: read a course id from the user (or read it in your main program and pass it here as a parameter) loop over the studentinfo array compare the id to the one of the current record if a match shift all records after this down one by copying them over the top of the record before subtract one from the number of student records (since we've deleted one) return from the function indicating found and delete repeat for next record if you complete looking at all records, return from the function indicating no match found </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload