Write file input and output instances of & Ptask

Foreword

Recently wrote pTask , the way to understand how to read the output file. In Ptask most important, the bug is most susceptible to the place that file operations. So how input and output files, play an important role in the program do?

Entry

First, in order to ensure can be input and output in the console and file, we do not freopen (It is said that after the switch to open the file read into not coming back) then you can only use header files <fstream>in ifstreamand ofstreamthe.

First, we need to use ifstream 名字to open a file stream input. such as

ifstream in;
in.open("in.txt");
in >> a;

In this example we use in open in.txt this document, the current document is read-only, read what was inside a a.
Integer, then did not try, but the string is typically read one line.

Why is read-only document it?
Because we only opened a file stream read by me after trying to find, read and output can not open two streams simultaneously. Otherwise it will be garbled. (Not necessarily correct, please correct if wrong)

Then after the finish to use in.close()to turn off the operation of the input stream.

Export

Input and output truth is similar, but with ofstreamnothing.

Examples

So let's take a look at a direct example, the following is Ptask part of the source code (the contents change).

void end()
{
	ofstream out;
	out.open(txtname);
	n = 0;
	for(int i = 1; i <= 100; i++)
	{
		if(strlen(name[i]) == 0)
			continue;
		if(del[i] == 1)
			continue;
		n++;
	}
	out<<n<<endl;
	out<<themer<<endl;
	out.close();
}

We see, open the file name is replaced with a string , and in order to facilitate the reading, I have a line break after each item of information.

In Ptask functions among the more difficult to achieve it is a delete operation of real-time display , use a lot of code that could have been achieved, but the use of file operations can be realized.
We only need to do to delete the contents of the tag, and then delete the contents will not export, re-import the last show, which is equivalent to reload the document. But it is also due to the realization of the principle reasons for the deletion, we can not not hold one deletion.

Since the code has been written, so if you want to change this unimaginable quantities but, after all, we are behind the workers can depend on file operations. I'm a changed man, hey, do not come.

In the final Finally, if you think the content here to help you, then in the lower right corner followers or the point of a recommendation or to share out this article, then, no more.

Guess you like

Origin www.cnblogs.com/mngmbear/p/12643978.html