代码中的输入输出(重定向、文件流C/C++)

一.freopen的使用(C/C++)

函数原型:FILE *freopen( const char *path, const char*mode, FILE *stream );

头文件: stdio.h

参数:

path: 文件名,用于存储输入输出的自定义文件名。

mode: 文件打开的模式。和fopen中的模式(如r-只读, w-写)相同。

stream: 一个文件,通常使用标准流文件。

返回值:成功,则返回一个path所指定文件的指针;失败,返回NULL。一般不使用。

功能:实现重定向,把预定义的标准流文件定向到由path指定的文件中。标准流文件具体是指stdin、stdout和stderr。其中stdin是标准输入流,默认为键盘;stdout是标准输出流,默认为屏幕;stderr是标准错误流,一般把屏幕设为默认。

例:

  freopen("data.in", "r", stdin); //重定向标准输入为data.in文件

        freopen("data.out", "w", stdout); //重定向标准输出为data.out文件

[cpp] view plain copy
 
 print?
  1. // 程序功能是统计输入的序列串中的最大值、最小值和序列串的平均值  
  2.   
  3. #define LOCAL  
  4. #include <stdio.h>  
  5. // #include <iostream>  
  6. #define INF 1000000000  
  7. // using namespace std;  
  8.   
  9. int main (void) {  
  10. #ifdef LOCAL   
  11.     freopen ("data.in", "r", stdin);  
  12.     freopen ("data.out", "w", stdout);  
  13.     #endif  
  14.   
  15.   
  16.     int x, n = 0, min = INF, max = -INF, s = 0;  
  17.     while (scanf ("%d", &x) == 1) {  
  18.     // while (cin >> x) {  
  19.         s += x;  
  20.         if (x < min) min = x;  
  21.         if (x > max) max = x;  
  22.   
  23.         //printf ("x = %d, min = %d, max = %d ", x, min, max);  
  24.   
  25.         ++n;  
  26.     }  
  27.   
  28.   
  29.     printf ("%d %d %.3f ", min, max, (double)s/n);  
  30.     // cout << min << " " << max << " " << (double)s / n << endl;  
  31.   
  32.     return 0;  
  33. }  

二.文件流使用(C)

fopen函数

函数原型:FILE * fopen(const char * path, const char * mode);

头文件: stdio.h

参数:

path: 文件名。

mode: 文件打开的模式。


返回值:成功,则返回一个path所指定文件的指针;失败,返回NULL。

[cpp] view plain copy
 
 print?
  1. // 程序功能是统计输入的序列串中的最大值、最小值和序列串的平均值  
  2.   
  3. #include <stdio.h>  
  4. #define INF 1000000000  
  5.   
  6. int main (void) {  
  7.     FILE *fin, *fout;  
  8.     fin = fopen ("data.in", "rb");  
  9.     fout = fopen ("data.out", "wb");  
  10.   
  11.     int x, n = 0, min = INF, max = -INF, s = 0;  
  12.     while (fscanf (fin, "%d", &x) == 1) {  
  13.         s += x;  
  14.         if (x < min) min = x;  
  15.         if (x > max) max = x;  
  16.   
  17.         //printf ("x = %d, min = %d, max = %d ", x, min, max);  
  18.         ++n;  
  19.     }  
  20.   
  21.     fprintf (fout, "%d %d %.3f ", min, max, (double)s/n);  
  22.     fclose (fin);  
  23.     fclose (fout);  
  24.   
  25.     return 0;  
  26. }  
 

三.C++中文件流的使用

在C++中文件和string都可以转换为对应的输入输出流,由此可以简单对文件和string对象进行操作。具体的操作为:
1. 创建对应的流对象(文件流、String流,如:ifstream,ofstream,istringstream,ostringstream等)
2. 用对应的文件名(或string)初始化该流对象
 
经过上面两步,就可以很简单的对对应的流对象进行操作,跟标准输入输出流的操作方式一致(与cin、cout的使用方式一致)
 
[cpp] view plain copy
 
 print?
  1. #include <iostream>  
  2. #include <fstream>  
  3. #define INF 1000000000  
  4.   
  5. using namespace std;  
  6.   
  7. int main (void) {  
  8.     ifstream fin("data.in");  
  9.     ofstream fout ("data.out");  
  10.   
  11.     int x, n = 0, min = INF, max = -INF, s = 0;  
  12.     while (fin >> x) {  
  13.         s += x;  
  14.         if (x < min) min = x;  
  15.         if (x > max) max = x;  
  16.   
  17.         //printf ("x = %d, min = %d, max = %d ", x, min, max);  
  18.         ++n;  
  19.     }  
  20.   
  21.     fout << min << " " << max << " " << (double)s/n;  
  22.     fout.close ();  
  23.     fin.close ();  
  24.   
  25.     return 0;  
  26. }  
 
类继承图:
 
 
原文地址:https://www.cnblogs.com/fire909090/p/8134348.html