File operations in the C language---redirect action files

Source: Internet
Author: User

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

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.