C ++ file operations of some problems that need attention and a method for writing a file

C ++ file operations can be described as a more difficult area, because the beginner to the file system and not enough understanding, often feel baffled. Now I come to some common problems and easy way to introduce to you a sort of written document.
1.fstream not automatically create a file.
In C ++, ofstream and ifstream are automatically created when binding files in the current project in a folder, and fstream can not, so we will be ready to file in advance, added to the project folder.
2. Reading and writing binary file must be opened in binary mode.
This nothing to say, so as to read and write properly.
3. It should be noted when reading and writing text files, each line will be with a newline '\ n'
if there indent, the first line will add '\ t'.
Previous typewriter, it wraps to the beginning of the line and two operations. In C ++, a text mode for line breaks for a special treatment, so only read '\ n'. In binary mode, the two operations will not be processed. Many languages were processed for line breaks, and C ++, but retains this original approach.
4.getline function
literally getline to read a line that is the meaning, if the default terminator, the line read in will not contain line breaks, then we will write the file manually wrap. Sometimes we want a one-time read the file, the file can be added at the end of the '/', this is a comment commonly used symbols, but with the perfect escape character '\ distinguished, and this time we will set the termination character relevant character '/' and the like, can be read one time.
Have read here, give a little praise of it, autumn Li Gao, this is my first blog, we can support it (● '◡' ●).

 未经作者允许严禁转载
Published an original article · won praise 5 · views 26

Guess you like

Origin blog.csdn.net/qq_46221910/article/details/104570337