C++中Txt文件读取和写入(国外英文资料).doc
文本预览下载声明
C++中Txt文件读取和写入(国外英文资料)
Txt file reads and writes in c + +
A, ASCII output
In order to use the following method, you must include the header file fstream. H (translator note: in the standard c + +, have been used fstream replace fstream. H , all the c + + standard header files are no suffix.) It is. This is iostream. H an extension set, provide a buffer file input/output operations. In fact, iostream. H fstream. H contains had been made, and so you dont have to include all these two files, if you want to explicitly include them, that suit yourself. We start with the design of the file action class, and Ill show you how to do ASCII I/O operations. If you guessed fstream, congratulations! But how do we use the ifstream in this article? And ofstream to do the input and output.
If you use the standard console stream cin? And cout, thats easy for you now. Were now starting to talk about the output section, first declaring a class object.
Ofstream fout. This is fine, but if you want to open a file, you must call ofstream: : open ().
Open ( output.txt ); You can also open a file as a constructor parameter.
Ofstream fout ( output.txt ); This is the method we use, as so to create and open a file looks more simple. By the way, if you want to open the file does not exist, it will create a for you, so you dont have to worry about the problem of file creation. Output to a file, it now looks like those cout operation. For those who do not understand the console output cout, here is an example.
Int num = 150;
The char name [] = John Doe;
Here is a number, Here is a number, Here is a number.
Now save the file, you must close the file, or to write the file buffer. The file closed cant again after the operation, so only when you are no longer operating this file to call it, it will automatically save the file. The write buffer keeps the file when the file is opened, so use it whenever necessary. The write-back looks like another out
显示全部