Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

what does it mean ios::trunc and ios::ate in C++ file handling.

Status
Not open for further replies.

satellite03

IS-IT--Management
Dec 26, 2003
248
IN

what does it mean ios::trunc and ios::ate in C++ file handling ?

can you give one small example ...its not clear.

i got in a tutorial like....
trunc > Truncate existing file (default behavior)

ate > Opens the file without truncating, but allows data to be written anywhere in the file.

can you give some example code so that i can test and visualize the thing.




BTW, what is the full form of "ate" ?







 
...then what is the difference with "ate" and "app" ?


secondly, for this example...

ofstream file;
file.open ("example.bin", ios::eek:ut | ios::app | ios::binary);

here why OR operator ? why AND operator is not usued as the files are opening in all the three modes ?


 
It is binary - and will cancel all the bits out.

eg if

ios::eek:ut = 1
ios::app = 2

ios::eek:ut | ios::app = 3
ios::eek:ut & ios::app = 0

 
yes, your example is ok. you are using bitwise operator . it is doing addition. does "app" and "out" can really hold those numbers. plz note ,those are merely mode of opening. can they hold decimal values ?

however your example is ok to convince why AND should not be usued.


can you plz tell, is there any difference between "app" and "ate"....i have seen these two modes. it seems to me both does the same thing. is not it?
 
satellite03 said:
yes, your example is ok. you are using bitwise operator . it is doing addition. does "app" and "out" can really hold those numbers. plz note ,those are merely mode of opening. can they hold decimal values ?
Actually it is not doing addition, it is doing a bitwise OR. For example:

1001 | 0101 = 1101
1001 + 0101 = 1110

Technically decimal values can be put instead of the flags, but that is not a good idea because on any particular implementation the flags can correspond to different number.
satellite03 said:
can you plz tell, is there any difference between "app" and "ate"....i have seen these two modes. it seems to me both does the same thing. is not it?
The difference between the two is that with "ate" if you change the file position you can write to the middle or beginning of the file. With "app" even if you change the file position you will write to the end of the file. If you aren't planning on changing the file position at all, just use "app".
 
thanks for the clarification...it is ok.

i have one more question. what does it mean i we open the file ios::trunc ?

what does "trunc" means ? will it truncate the file from any position ?
 
When you open with ios::trunc, the entire contents of the file will be removed at that time, so you are basically starting with an empty file.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top