+ 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.
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
+ 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
+ 7
cool 😊 i think the only other options for rename and delete are the microsoft ones in fileapi.h/windows.h
+ 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 😊
+ 7
from what i was reading it is only in cstdio
+ 7
there is the boost library. https://theboostcpplibraries.com/boost.filesystem-files-and-directories
but it isnt native
+ 7
Oh just a thought. In the add student function. Have you considered searching the file for duplicate roll numbers before inserting the record?
+ 7
hahaha yeah.. last version i saw had manual entry of that field 😅
+ 7
nar its all good! i was just thinking of things to help. 😊
+ 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.
+ 3
I don't think I should use ios::trunc as I have to preserve the data of other objects...
+ 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.
+ 3
So the only cross-platform functions we have are from cstdio. Isn't there anything like this in fstream?
+ 3
Hmm. Thanks for the info!
+ 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!
+ 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!
+ 3
Yeah I'm just updating that, adding the overwrite and delete functions as well. Sorry for the confusion...
+ 3
Its updated now! Thanks for the help!