[C/C++] freopen function and fopen function | standard input and input write to file | screen output to write to file

Table of contents

similarities and differences

example

fopen

freopen


similarities and differences

Both freopen and fopen are C standard library functions included in the C standard library header file <stdio.h>.

fopen is what we are most familiar with to open a file for writing or reading

freopen is to open a file to receive data from input and output streams.

freopen("xxx.in","r",stdin);	//输入文件
freopen("xxx.out","w",stdout);	//输出文件

example

fopen

FILE *fp1,*fp2; //定义文件指针类型
fp1=fopen("input.in","r"); //用fopen函数以只读方式(r)打开输入文件input.in;
fp2=fopen("output.out","w");//用fopen函数以写入方式(w)打开输出文件output.out;

fscanf(fp1,"%d",&temp);//fscanf从文件中读取数据,fp1文件指针指定文件;
fprintf(fp2,"%d",temp);//fprintf将数据输出到文件,fp2文件指针指定文件;

fclose(fp1);//关闭文件指针。
fclose(fp2);

freopen

#include<cstdio>
#include<iostream>
using namespace std;
int main()
{
	freopen("a+b.in","r",stdin);
	freopen("a+b.out","w",stdout);
	//以上是模板
	int a,b;
	cin>>a>>b;
	cout<<a+b<<endl;
	return 0;
}

At this point, the user's input on the screen (cin>>a>>b;) will be written into the a+b.in file.

The standard output stream (cout<<a+b) will be written to the a+b.out file.

The result of running the above program is:

insert image description here

Reference or excerpt:

http://t.csdn.cn/e3OZH

Example of program interactively generating shutdown script:

(From: http://t.csdn.cn/BP6I7)

#include<bits/stdc++.h>
#include <conio.h>
#include <windows.h>
using namespace std;
int main()
{
    system("color f0");//初始化控制台模样
    system("title 关机软件工厂");//同上关机软件工厂
    char c; 
    cout<<"欢迎来到关机软件工厂,这里有各种各样的关机bat文件"<<endl;
    Sleep(1000) ;//暂停1秒,主要是让这个软件变得不那么突兀怪异
    cout<<"建议您作死前先制造一个放弃关机的文件,不然......后果自负"<<endl; 
    Sleep(1000);
    cout<<endl<<"---------按键继续----------" ;
    c=getch();//读取(getch是不带回显,不带缓冲区的单个字符读入。
    system("cls"); //清屏
    cout<<"请问您需要什么关机文件?"<<endl<<endl; 
    Sleep(1000) ;
    cout<<"请输入对应编号"<<endl<<"1.定时关机"<<endl<<"2.立即关机(慎用!!!)"<<endl<<"3.放弃关机"<<endl<<"4.注销"<<endl<<"5.重启(慎用,too!!!)"<<endl<<"其他.我不需要,谢谢";
    c=getch();//同上
    system("cls");//同上
    int a;
    if(c=='1')
    {
        cout<<"请问您需要定时多久关机?(请输入秒数)";
        cin>>a;
        system("cls");
        cout<<"搞定,您可以在当前文件夹下找到定时关机文件,再见" ;
         //因为这是在fropen之前的输出,所以会输出在控制台而不是文件中。
        freopen("定时关机.bat","w",stdout);
        //众所周知,可以用记事本输入一个shutdown在改变拓展名为bat改为批处理文件,然后便可实现关机。
        cout<<"shutdown -s -t "<<a;
        Sleep(1000) ;
        return 0;
    }
    if(c=='2') 
    {
        system("cls");
        cout<<"搞定,您可以在当前文件夹下找到立即关机文件,再见";
        freopen("立即关机.bat","w",stdout);
        cout<<"shutdown -s";
        Sleep(1000) ;
        return 0; 
    }
    if(c=='3') 
    {
        system("cls");
        cout<<"搞定,您可以在当前文件夹下找到放弃关机文件,再见";
        freopen("放弃关机.bat","w",stdout);
        cout<<"shutdown -a";
        Sleep(1000) ;
        return 0; 
    }
    if(c=='4') 
    {
        system("cls");
        cout<<"搞定,您可以在当前文件夹下找到注销文件,再见";
        freopen("注销.bat","w",stdout);
        cout<<"shutdown -l";
        Sleep(1000) ;
        return 0; 
    }
    system("cls");
    cout<<"好的,下次见!!!";
    Sleep(1000) ;
    return 0; 
}


 

1. fopen()
  encounters the use of file input/output in C language, and it may be the most common one.

  The general form of fopen call is:
  file pointer name = fopen (file name, using file method)

The file pointer name must be a pointer variable of FILE type;
the file name is the name of the file to be opened.
The file method is the file type and operation requirements, which are as follows:
r (read): read-only;
w (write): Write only;
a (append): append;
t (text): text file, which can be omitted;
b (binary): binary file.
+ : The read and write
  open methods are a combination of the above basic types. Note:

r opens a file, the file must exist, and can only be read from this file;
w opens a file and only wants to write to this file. If the file does not exist, it will be created automatically; if the file already exists, delete the original file and create a new one.
  The usage of this function is as follows:

FILE *fp1,*fp2; //Define the file pointer type
fp1=fopen("input.in","r"); //Use the fopen function to open the input file input.in in read-only mode (r);
fp2=fopen ("output.out","w");//Use the fopen function to open the output file output.out in writing mode (w);

fscanf(fp1,"%d",&temp);//fscanf reads data from the file, the fp1 file pointer specifies the file; fprintf(
fp2,"%d",temp);//fprintf outputs the data to the file, fp2 The file pointer specifies the file;

fclose(fp1);//Close the file pointer.
fclose(fp2);


  When the fopen function is enabled, specific functions fscanf and fprintf are required to operate on files; ordinary scanf and printf can still be operated on the command line interface.

2. freopen()
  The freopen function is widely used in ACM, because there may be a large amount of input data in the ACM test questions, and the program operation is often not successful at one time. It is a waste of time to re-enter every time, so freopen can solve the test Duplicate data entry problem.

Function declaration:

FILE * freopen(const char *filename, const char *mode, FILE *stream);
1
parameter description:

filename: the name of the file to be opened;
mode: the mode of opening the file, which is the same as the mode (r/w) in fopen.
stream: file pointer, usually using standard stream files (stdin/stdout/stderr)
usage:

freopen("data.in","r",stdin); 
freopen("data.out","w",stdout);

fclose(stdin);
fclose(stdout);

  The freopen() function redirects the standard stream to point to the specified file, so there is no need to modify scanf and printf.

  It's really convenient, but there is still a problem to be solved. Through freopen, we redirect the standard stream to the specified file, so what should we do if we want to output some information to the screen?

  The fopen function consists of two functions, fprintf and printf, which output to the file and the console respectively. For the freopen function, the filename parameter of the function needs to be modified so that the standard stream can be output to the console. And the name of this console device file is related to the operating system.

DOS, Win system:

freopen("CON","r",stdin);
freopen("CON","w",stdout);

Linux:

freopen("/dev/console", "r", stdin);

  In this way, part of the required data can be output to the file, while another part of the data can be output to the console. This allows the program to have some simple interactive capabilities.

Original link: https://blog.csdn.net/XavierDarkness/article/details/80638641

Guess you like

Origin blog.csdn.net/bandaoyu/article/details/128783236