Basics of File Handling | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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

25th Oct 2017, 10:28 AM
Sagar Sonavane
3 Answers
+ 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
25th Oct 2017, 5:41 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 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.
25th Oct 2017, 3:11 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 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.
25th Oct 2017, 4:23 PM
Sagar Sonavane