Delete a file using fscanf and fprintf | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Delete a file using fscanf and fprintf

The program that I have I want to delete a specific record. it is however not working how I want it.it deletes the intended record that I asked for but it also deletes all the other records after it.

9th Apr 2018, 1:24 AM
mya
mya - avatar
13 Answers
+ 5
Without seeing your code we are kind of playing a guessing game of how you screwed up. Odds are you are copying records from the old file to a new file, given your mention of fscanf & fprintf. If true, you most likely stop once you find the record you want to delete, which deletes the rest of the records. Instead, continue copying the rest of the records to skip only the one you deleted. If this doesn't fix your code, place it in the playground and provide a link so we can see what you are doing.
9th Apr 2018, 3:02 AM
John Wells
John Wells - avatar
+ 3
if anyone is wondering I got it to work but I just converted the whole thing to random access file and it works much better. and I just use fwrite and fseek
9th Apr 2018, 4:23 PM
mya
mya - avatar
+ 3
I haven't had time to put code on my laptop, but I didn't see anything obvious.
9th Apr 2018, 4:32 PM
John Wells
John Wells - avatar
+ 3
If you want, I'll play with it tonight, when I sit down with my laptop.
9th Apr 2018, 6:18 PM
John Wells
John Wells - avatar
+ 2
to test if your file has been deleted... isRemoved = remove(file_name);   if (isRemoved == 0) printf("%s file deleted successfully.\n",
9th Apr 2018, 1:30 AM
᠌᠌Brains[Abidemi]
᠌᠌Brains[Abidemi] - avatar
+ 1
Your code is pretty complex, and also has a lot of weird things in it. I considered you would understand it better if it kept your idea in it, so I only made minimal corrections where I thought it was too wrong. For example, it would recursively call a function when it needed to repeat its actions. I changed that, maybe not in the best way, but it was too much a heresy as it was, never do that. There was an undefined function there, login(), so I just copied some code in it to read records from the file when the program starts. So now the records are persistent, you don't have to input them from scratch every time. I "downgraded" the full name to only one word, because you would save it to file on the same line with other data and then read it with fscanf(), which stops at spaces. So to fix that, I only allow one word in the name. The program still has a lot of ways it could crash if the user enters wrong data in other places, I didn't fix that; it would have meant adding more code, and I didn't want to distract you. I put my suggestion where you ask to press 1 to try again or 2 to exit because it would make me exit by mistake. And probably some other minor changes I don't remember now. https://code.sololearn.com/cm5DCO3eHUYT/#c
9th Apr 2018, 7:50 PM
lion
lion - avatar
0
🇳🇬Brains I don't want to delete the whole file just a specific record
9th Apr 2018, 1:33 AM
mya
mya - avatar
0
🇳🇬Brains but my program is deleting that specific record but also other records that come after it
9th Apr 2018, 1:34 AM
mya
mya - avatar
0
sorry if this program is confusing. I'm still in high school just learning this via youtube and online
9th Apr 2018, 3:43 AM
mya
mya - avatar
0
here is my code. its only in its first stage i hope it helps to debug my problem/s Ace Martin Taylor John Wells https://code.sololearn.com/cd8mZfSI0epZ/?ref=app
9th Apr 2018, 10:28 AM
mya
mya - avatar
0
John Wells I didn't either but for some reason it wasn't working
9th Apr 2018, 6:12 PM
mya
mya - avatar
0
John Wells that would be great
9th Apr 2018, 10:40 PM
mya
mya - avatar
0
lion thank you so much. in was in the first stage of the program is put together something so I know it's not that robust
9th Apr 2018, 10:42 PM
mya
mya - avatar