C language input and output re-set, Freopen () magical.

Source: Internet
Author: User

Reason for use (scope): This function can be used if the input data is large and needs to be re-entered and debugged again and again.

Freopen () function:

1. format

FILE * Freopen (const char * filename, const char * mode, FILE * stream);

2. parameter Description

FileName: The file name to open

Mode: The file opens with the same mode as in fopen (r/w)

Stream: A file pointer, typically using a standard stream file (stdin/stdout/stderr)

Return value: Successful, returns aPathA pointer to the specified file; failed, returnedNULL. (You can generally not use its return value)
Function: Implement redirection to direct predefined standard stream files to thePaththe specified file. The standard stream file specifically refers to thestdin,stdoutand thestderr. Whichstdinis the standard input stream, the default is the keyboard;stdoutis the standard output stream, the default is the screen;stderris the standard error stream, which generally sets the screen as the default. by callingFreopen, you can modify the default value of the standard stream file to implement redirection.

3. How to use

Because the file pointer uses a standard stream file, we can not define a file pointer.

Next we use the freopen () function to open the input file as read-only R (read) slyar.in

Freopen ("Slyar.in", "R", stdin);

Then use the freopen () function to open the output file in write mode W (write) slyar.out

Freopen ("Slyar.out", "w", stdout);

The next thing is the advantage of using the freopen () function, we no longer need to modify scanf and printf, but to maintain the code as it is. Because the freopen () function redirects the standard stream, it points to the file that you specified earlier .

Finally, just use fclose to close the input file and output file.

Fclose (stdin);

Fclose (stdout);

To recover a handle, you can reopen the standard console device file, except that the name of the device file is associated with the operating system.

Dos/windows:

Freopen ("CON", "R", stdin);

Linux:

Freopen ("/dev/console", "R", stdin);

4. Algorithm examples

Enter some integers to find their minimum, maximum, and average values (keep 3 decimal places). Enter an integer that guarantees that these numbers are not more than .

Sample input:2 8 3 5 1 7 3 6

Sample output:1 8 4.375

Reference Program 1:

#define TEST

#include

#define MM 1000

Main ()

{

#ifdef TEST

Freopen ("D:\\c2_4_in.txt", "R", stdin);

Freopen ("D:\\c2_4_out.txt", "w", stdout);

#endif

int i=0,sum=0,n,max=-mm,min=mm;

while (scanf ("%d", &n) ==1)

{

Sum=sum+n;

if (max<n) < span= "" >

Max=n;

if (min>n)

Min=n;

i++;

}

printf ("%d%d%.3lf\n", Min,max, (double) sum/i);

}

Description: (1) for the subject, we used the redirect simply, that is the data entered by the standard input scanf () function in the program from the d:\c2_4_in.txt read in, printf () the data of the function output is written directly D:\c2_4_out.txt , the screen is not waiting for the input data and no longer displays the output results.

(2) If the first sentence is removed (#define TEST),

#ifdef TEST

Freopen ("D:\\c2_4_in.txt", "R", stdin);

Freopen ("D:\\c2_4_out.txt", "w", stdout);

#endif

does not serve any purpose, it must also use standard input and output.

Reference Program 2:

#include
#define MM 1000
Main ()
{
FILE *fin,*fout;
Fin=fopen ("D:\\c2_4_in.txt", "RB");
Fout=fopen ("D:\\c2_4_out.txt", "WB");
int i=0,n,sum=0,max=-mm,min=mm;
while (FSCANF (Fin, "%d", &n) ==1)
{
Sum+=n;
if (max<n)
Max=n;
if (min>n)
Min=n;
i++;
}
fprintf (Fout, "%d%d%.3lf", Min,max, (double) sum/i);
Fclose (Fin);
Fclose (Fout);
}

The above procedure is passed in VC6.0 environment test.

C language input and output re-set, Freopen () magical.

Related Article

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.