+ 5

Alternatives for remove() and rename() from <cstdio>

In my student database program https://code.sololearn.com/cqfe6f4AzLw0/?ref=app I now decided to add an overwrite and a delete function. For the overwrite part, I just opened the file in append mode and wrote the new entry in the end. And for delete mode, I tried to use a seperate file and copied only those records whose roll numbers did not match the entered number. Finally I used remove and rename to change the file to one that I use. Is there any alternative for remove or rename? I dont want cstdio.

30th Jul 2017, 3:44 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
18 Respuestas
+ 8
For the delete record function have you tried the following steps? Read file contents into memory. remove record from memory delete file create file from records in memory as to remove i will check https://msdn.microsoft.com/en-us/library/aa363915(v=VS.85).aspx
30th Jul 2017, 3:51 AM
jay
jay - avatar
+ 7
ha. much easier way.. if you open the file with trunc option it will clear the contents of the file. http://www.cplusplus.com/reference/fstream/ofstream/open/ https://stackoverflow.com/questions/17032970/clear-data-inside-text-file-in-c
30th Jul 2017, 3:57 AM
jay
jay - avatar
+ 7
cool 😊 i think the only other options for rename and delete are the microsoft ones in fileapi.h/windows.h
30th Jul 2017, 4:05 AM
jay
jay - avatar
+ 7
yeah. thus i was suggesting doing the deletes in memory. (read the entire file, export only those that dont match primary id) either way it should work 😊 I assume this project is all about learning file management so I'd stick with what your are doing I guess 😊
30th Jul 2017, 4:08 AM
jay
jay - avatar
+ 7
from what i was reading it is only in cstdio
30th Jul 2017, 4:09 AM
jay
jay - avatar
+ 7
30th Jul 2017, 4:12 AM
jay
jay - avatar
+ 7
Oh just a thought. In the add student function. Have you considered searching the file for duplicate roll numbers before inserting the record?
30th Jul 2017, 4:21 AM
jay
jay - avatar
+ 7
hahaha yeah.. last version i saw had manual entry of that field 😅
30th Jul 2017, 4:24 AM
jay
jay - avatar
+ 7
nar its all good! i was just thinking of things to help. 😊
30th Jul 2017, 4:30 AM
jay
jay - avatar
+ 3
I simply open a temp file of the same extension, read only those objects from the original file whose entries do not match the roll number (a unique primary key) specified by the user to delete and write them to the temp file. Then I remove the original file and rename the temp file to the original one.
30th Jul 2017, 4:02 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 3
I don't think I should use ios::trunc as I have to preserve the data of other objects...
30th Jul 2017, 4:03 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 3
I first tried a way to remove a particular entry by shifting the newer entries to the back and then adding 0s to the last entry as It would be a copy. But that was a very slow method and It left a lot of corrupted bytes in the file. Due to that OverWrite wasn't able to write properly.
30th Jul 2017, 4:07 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 3
So the only cross-platform functions we have are from cstdio. Isn't there anything like this in fstream?
30th Jul 2017, 4:09 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 3
Hmm. Thanks for the info!
30th Jul 2017, 4:10 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 3
Boost seems a little complex to me for use. Ill see what Ill do. For now, Ill rely on cstdio and windows.h... Thank You!
30th Jul 2017, 4:14 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 3
The numbers are generated by rand() and the rand() function runs for atleast 100 times. I don't think there will still be a chance that the roll numbers are duplicated, but Ill add the checking part. Thanks!
30th Jul 2017, 4:23 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 3
Yeah I'm just updating that, adding the overwrite and delete functions as well. Sorry for the confusion...
30th Jul 2017, 4:26 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 3
Its updated now! Thanks for the help!
30th Jul 2017, 4:35 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar