freopen function input and output redirection

Source: Internet
Author: User

Usually when we debug, always want a sample input, and then look at the output.
But if we enter a large amount of time, although the copy function can be used, but if the output too much, the screen can not be displayed, for our debugging is very inconvenient.

Here is a description of the use of the Freopen function. Let's look at the following example:
Enter 10 numbers, and then output 10 numbers.
In general we will write this:

#include"stdio.h"int main(){    int n=10;    int a[20];    for(int i=1;i<=n;i++)        scanf("%d",&a[i]);     for(int i=1;i<=n;i++)        printf("%d\n",a[i]);    return0;}

But what if there's a lot of data? or multiple sets of inputs?
We can change the following posture:

#define TEST#include "stdio.h"intMain () {#ifdef TEST freopen ("In.txt","R",stdin); Freopen ("OUT.txt","W",stdout); #endif//TEST    intn=Ten;inta[ -]; for(intI=1; i<=n;i++) scanf ("%d", &a[i]); for(intI=1; i<=n;i++) printf ("%d\n", A[i]); #ifdef TEST fclose (stdin); Fclosestdout); #endif//TEST    return 0;}

Compared to the previous one, there is a macro definition test, followed by the statement that the test is executed when it is defined.
StdIn is our input stream, stdout is our output stream.
We redirect them by reading and writing the files separately.
The function is that the contents of the in file only need us to prepare the previous content.
Then each time you run the out file, you can see the results of our output.
Finally, remember to close the file, otherwise the file may have a series of problems because of being occupied.

We don't see any input and output on the screen at runtime.

In the same directory as the project, we first prepare the In.txt file, and enter the data we need to enter, close the save.
After running the program you can see more than one OUT.txt file, open to see what we originally want to output to the screen content.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

freopen function input and output redirection

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.