Two precautions for using fstream-general Linux technology-Linux programming and kernel information. The following is a detailed description. 1. open the file in append Mode
Ofstream oFile;
OFile. open (strFileName. c_str (), ios_base: binary | ios_base: out | ios_base: app );
In this case, where do you think is the write pointer of the oFile? File Header? File tail? In fact, in linux, it is the end of the file, and in windows, it is the file header. Because, if you are programming in windows, remember to do the following after opening the file:
OFile. seekp (0, ios: end );
2. Cross-read/write of fstream
If you need to use fstream to read and write a file for a while, and then read and write "Cross" reads and writes similar to this, pay special attention to it, otherwise, you do not know where the error is. Some strange and difficult-to-locate problems may occur. Because fstream has a read pointer and a write pointer. Getg seekg is used to obtain and set the read pointer, getp seekp is used to obtain and set the write pointer. If you change from a read operation to a write operation, you must use seekp to set the write pointer. Similarly, if you change from a write operation to a read operation, you must use seekp to set the read pointer. Otherwise, you will see the ghost. What you read is not what you want.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.