First of all, the file operation and string and character in-depth processing (that is, what puts (), GetChar () or something) is my deep understanding of C + + the last two mountains. Today, the redirect file operation is resolved first.
Input-output redirection is probably the simplest method for file I/O operations, with two statements added to the beginning of the main function, such as:
Freopen ("D:\\input.txt", "R", stdin); Freopen ("D:\\output.txt", "w", stdout);
The first parameter is the path of the input and output file, and the second is the mode of manipulating the file (here we first understand that "R", "W" means read-only and write-only; "RB", "WB" means read-only and write-only binaries); the last parameter is a pointer to this file (file*), generally we fill "StdIn" and "stdout" can be.
One of the advantages of input-output redirection is that you don't need to call fclose () at the end, because the system will do this for you automatically.
OK, here's a super-simple example of how to use Freopen () input and output redirection.
"Input" two integers a and b
Results of the "output" a+b
The source code is as follows (compiled under Visual Studio 2008):
#include <stdafx.h> #include <stdio.h>int main (void) {int A, b;freopen ("d:\\abc.in", "RB", stdin) Freopen (" D:\\abc.out "," WB ", stdout), scanf ("%d%d ", &a, &b);p rintf ("%d is the result\n ", A + b); return 0;}
At this point, we create a file called abc.in in the D disk, using a text editor to enter 66 and 34, and then save; then, compile and run the program; After running, go to see the results? The content of Abc.out is: the result
If you are interested, you can go and see what the second parameter is, but the pattern mentioned in this article is more common enough to deal with algorithmic contests. Ok,freopen () The content of the input and output redirects is temporarily over.
This article from "mlh1719233148 Blog" blog, reproduced please contact the author!
File operations in the C language---redirect action files