Usage in C ++ freopen () function

#include <stdio.h>  

#include <the iostream> the using namespace STD; int main () 
{ int A, B; 
    The freopen ( " in.txt " , " R & lt " , stdin); // input redirection, the input data read from the file in.txt 
    the freopen ( " out.txt " , " W " , stdout); // output redirection, the output data stored in the file out.txt the while (CIN >> A >> B) 
        COUT A + B << endl <<; // Note the use endl 
    fclose (stdin);// close the file

 

 




    



    



    fclose (stdout); // close the file 

    return  0 ; 

}

  effect freopen ( "in.txt", "r ", stdin) is the standard input stream stdin in.txt redirected to a file, so that when used with scanf or cin input stream will read from stdin data, but to obtain input from in.txt file.  
Similarly, the role of freopen ( "out.txt", "w ", stdout) is out.txt redirect stdout to a file, so that the output out.txt need to open the file to view. 

Function name: freopen 
statement: FILE * freopen (const char *  path, const char * mode, FILE * stream);
where file: stdio.h 
Parameters: 
       path: file name, file name for storing the custom input and output. 
       mode: file should be opened. And a mode fopen (e.g., read only r-, w- writing) the same. 
       stream: a file, typically use standard streaming file. 
Return Value: success, a pointer to the specified file path is returned; failure returns NULL. (Typically may not use its return value) 

Function: redirection, the orientation of predefined criteria stream file specified by the file path. Standard stream file specifically refers to stdin, stdout and stderr. Wherein stdin is the standard input stream, the default keyboard; stdout is the standard output stream, default screen; stderr is the standard error, the screen is generally set as the default. By calling freopen, you can modify the default value of the standard stream file redirection.

Guess you like

Origin www.cnblogs.com/Bella2017/p/11105136.html