Detailed explanation of the usage of freopen function

hzc
Release: 2020-06-18 16:41:57
Original
10633 people have browsed it

Detailed explanation of the usage of freopen function

freopen 函数说明 函数名: freopen 功 能: 实现数据重定向到文件中 用 法: FILE *freopen(const char *filename, const char *mode, FILE *stream); 返回值: 成功,则返回文件指针;失败,返回NULL(可以不使用它的返回值) 7 #include  int main(void) { /* redirect standard output to a file */ if (freopen("OUTPUT.FIL", "w", stdout) == NULL) { fprintf(stderr, "error redirecting\ stdout\n");   } /* this output will go to a file */ printf("This will go into a file."); /* close the standard output stream */ fclose(stdout); return 0; }
Copy after login

The freopen function accesses files by implementing the standard I/O redirection function, while the fopen function accesses files through file I/O.

The freopen function is often used in algorithm competitions. In algorithm competitions, contestants generally need to input their data multiple times, and to avoid repeated input, redirection is used.

The freopen function is very useful in debugging:

The function of freopen("debug\\in.txt", "r", stdin) is to redirect the standard input stream stdin to debug\ \in.txt file, so that when using scanf or cin to input, the data will not be read from the standard input stream, but the input will be obtained from the in.txt file.

As long as you paste the input data into in.txt in advance, it will be much more convenient during debugging.

Similarly, the function of freopen("debug\\out.txt", "w", stdout) is to redirect stdout to the debug\\out.txt file, so that the output results need to open out. txt file viewing.

What needs to be explained is:

1. In freopen("debug\\in.txt", "r", stdin), place the input file in.txt in the folder debug , the debug folder is the debug folder automatically generated when creating the project file in VC. If changed to freopen("in.txt","r",stdin), the in.txt file will be placed in the created project folder. The in.txt file can also be placed in other folders, as long as the path is written correctly.

2. You can not use output redirection and still view the output on the console. 3. After the program is successfully debugged, don’t forget to delete the statements related to redirection when submitting it to oj.

Recommended tutorial: "c"

The above is the detailed content of Detailed explanation of the usage of freopen function. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
c
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!