C++中fstream、ofstream、ifstream的使用--将数据重定向输出到文本文件

1.打开文件

在从文件读取信息或者向文件写入信息之前,必须先打开文件。ofstream 和 fstream 对象都可以用来打开文件进行写操作,如果只需要打开文件进行读操作,则使用 ifstream 对象。
下面是 open() 函数的标准语法,open() 函数是 fstream、ifstream 和 ofstream 对象的一个成员。

void open(const char *filename, ios::openmode mode);

模式含义
ios::in打开文件用于读取
ios::out打开文件用于写入
ios::ate文件打开后定位到文件末尾
ios::app追加模式。所有写入都追加到文件末尾。
ios::trunc如果该文件已经存在,其内容将在打开文件之前被截断,即把文件长度设为 0。
ios::binary以二进制方式进行读写

进入源文件,我们可以看到每个变量的定义:

	static constexpr _Openmode in = (_Openmode)0x01;
	static constexpr _Openmode out = (_Openmode)0x02;
	static constexpr _Openmode ate = (_Openmode)0x04;
	static constexpr _Openmode app = (_Openmode)0x08;
	static constexpr _Openmode trunc = (_Openmode)0x10;
	static constexpr _Openmode _Nocreate = (_Openmode)_IOS_Nocreate;
	static constexpr _Openmode _Noreplace = (_Openmode)_IOS_Noreplace;
	static constexpr _Openmode binary = (_Openmode)_IOSbinary;

说明每一种模式是一个由二进制数转化为_Openmode类型的常量
_Openmode类型定义为:

enum _Openmode
		{	// constants for file opening options
		_Openmask = 0xff};

open函数接收的参数就是openmode变量:

void open(const char *_Filename,
		ios_base::openmode _Mode = ios_base::out,
		int _Prot = (int)ios_base::_Openprot)

openmode类型其实就是int型

typedef int openmode;

归根结底,输出模式其实就是一个int型的变量,只是被各种封装掩盖了真实面目,因此根据ios::ate|ios::out = 0x0a,可以推知将0x0a作为open()函数的参数,可以得到同样的结果。下面是一个打开文件的实例:

std::fstream output;
output.open("..//..//output//output.txt", std::ios::app | std::ios::out)
output.open("..//..//output//output.txt", 0x0a)//ios::ate|ios::out = 0x0a,结果与上面相同

注意:源文件开头必须包含头文件

#include <fstream>
#include <fiostream>

2.关闭文件

当 C++ 程序终止时,它会自动关闭刷新所有流,释放所有分配的内存,并关闭所有打开的文件。但程序员应该养成一个好习惯,在程序终止前关闭所有打开的文件。close() 函数是 fstream、ifstream 和 ofstream 对象的一个成员。成员函数close(),它负责将缓存中的数据排放出来并关闭文件。这个函数一旦被调用,原先的流对象就可以被用来打开其它的文件了,这个文件也就可以重新被其它的进程所访问了。为防止流对象被销毁时还联系着打开的文件,析构函数将会自动调用关闭函数close。

output.close();

3.写入文件

在 C++ 编程中,我们使用流插入运算符( << )向文件写入信息,就像使用该运算符输出信息到屏幕上一样。唯一不同的是,在这里使用的是 ofstream 或 fstream 对象,而不是 cout 对象,也就是说它重定向了输出流,将输入定向到文件中。

output <<"Hello world!"<< std::endl;

这就将Hello world!字符串写到了output.txt文件中。

4.读取文件

在 C++ 编程中,我们使用流提取运算符( >> )从文件读取信息,就像使用该运算符从键盘输入信息一样。唯一不同的是,在这里您使用的是 ifstream 或 fstream 对象,而不是 cin 对象,也就是说它重定向了输入流,将输入定向到文件中。

char data[256];
char buffer[256];

fstream infile; 
infile.open("infile.txt"); 
infile >> data; 
while (!infile .eof())//更安全的方式
{
    infile .getline (buffer,100);
    cout << buffer << endl;
}

5.一个完整的读写文件代码

参考这篇博客
写文件:将数据输出到output.txt

 // writing on a text file
  #include <fiostream.h>
  using namespace std;
  int main () 
  {
      ofstream out("output.txt");
      if (out.is_open()) 
      {
          out << "Hello world!\n";
          out << "Hello CPP!\n";
          out.close();
      }
      return 0;
  }
 //结果: 在output.txt中写入:
 Hello world!
 Hello CPP!

读文件:将test.txt数据输入到buffer

// reading a text file
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
using namespace std;
int main () {
    char buffer[256];
    ifstream infile("test.txt");
    if (!infile.is_open())
    { 
    	cout << "Can't open file!"<<endl; 
    	exit (1); 
    }
    while (!infile.eof() )
    {
        infile.getline (buffer,100);
        cout << buffer << endl;
    }
    return 0;
}
 //结果 在屏幕上输出
 Hello world!
 Hello CPP!
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值