0
Basics of File Handling
Check the output of attached program. It seems that to overwrite the file's existing content, we can use any of the below method: 1) Open file using ios::ate. 2) Open file using ios::trunc. 3) Open file without using any Mode parameter. Is there any difference in the functionality of these mode parameters or can we always follow 3rd option in case of overwriting the file? https://code.sololearn.com/c8rxJOZf9yLb/?ref=app
3 Antworten
+ 4
Sure! Here is a sample code for the same. You can note the difference int the data in the file after each output operation done with the flags set one by one.
https://code.sololearn.com/ccfFjLooE5jU/?ref=app
Here is a similar code displaying the use of ios::ate:
https://code.sololearn.com/ch0b61Cxk90I/?ref=app
+ 2
There is a huge difference between the three.
ios::ate will just place the cursor at the end when you open the file but won't do so after the file has been opened. The flag, however, does not affect the data present. It is safe.
ios::trunc will open the file and delete all its written data so that you get an empty file. Then the cursor will be set at the beginning.
Not using anything will allow you to add the data, but it will literally overwrite it as the default cursor position will be set, which is the beginning of the file.
+ 1
Hello Kinshuk,
Thanks for replying to my query. Is it possible for you to explain the difference between "ios::ate" and "ios::trunc" by providing any small code logic???
May be that can help me (or others who are having similar query) in understanding basics of these mode parameters.