redirect input and output stream--freopen

Source: Internet
Author: User

Freopen is a function that is included in the C standard library header file <stdio.h> for redirecting input and output streams. This function can change the input and output environment without changing the original code.

C99 function Declaration:
File *freopen (const char * restrict filename, const char * restrict mode, file * restrict stream);
Parameter description: FileName: The file name or file path to which you want to redirect. Mode: A string representing the access rights of the file. For example, "R" means "read-only Access", "W" means "write-only Access", "a" means "append write". Stream: The file stream that needs to be redirected. Return value: If successful, returns the file pointer to the output stream, otherwise returns NULL.
#include <stdio.h>int main (void) {/    * REDIRECT standard output to a file *    /if (Freopen ("D:\\output.txt", "W ", stdout) = = NULL)        fprintf (stderr," error redirecting stdout\n ");    /* This output would go to a file *    /printf ("This would go into a file.\n");    /*close the standard output stream*/    fclose (stdout);    return 0;}

  

Take another example, in the D-Disk directory, create a new file, write a string of numbers to the file:

#include <stdio.h>int main (void) {    int i;    if (Freopen ("D:\\output.txt", "w", stdout) = = NULL)        fprintf (stderr, "error redirecting stdout\n");    for (i = 0; i < ten; i++)        printf ("%3d", I);    printf ("\ n");    Fclose (stdout);    return 0;}

Compile and run, you will find that 10 number output to the D packing directory in the text file output.txt

#include <stdio.h>int main (void) {    int A, b;    Freopen ("In.txt", "R", stdin);    /* If the in.txt is not in the connected EXE directory, you need to specify the path such as D:\\in.txt *    /Freopen ("OUT.txt", "w", stdout);    while (scanf ("%d%d", &a, &b)! = EOF)        printf ("%d\n", a+b);    Fclose (stdin);    Fclose (stdout);    return 0;}

Reads data from file In.txt, computes the addition and output of two adjacent numbers to OUT.txt

As the scanf function is used here, it is necessary to say:

The scanf () function returns the number of data items that were successfully assigned, and EOF is returned when an error is read to the end of the file. Such as:
scanf ("%d%d", &a,&b);
If both A and B are read successfully, then the return value of scanf is 2 if only a is read successfully, the return value is 1 if both A and B are not successfully read in, the return value is 0 if an error is encountered or an end of file is encountered, the return value is EOF. And the return value is of type int. The & in &a,&b,&c is the address operator, and &a refers to the address in memory of a. The function of scanf is to store the input data in A,b,c according to the A,b,c memory address. The address of the variable a,b,c is allocated at compile-time (the storage order is determined by the compiler). Note here: If%d in scanf is written as "%d%d%d", when entering data, the data cannot be separated by commas, only the space or TAB key or return--"2 3 4" or "2 (press TAB) 3 (Press TAB) 4 (Press TAB)". If "%d,%d,%d", you need to add "," such as "2,3,4" when entering data.

redirect input and output stream--freopen

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.