天天看点

c++文本读写操作详解

c++文本读写

    • 方法步骤
    • 流对象
    • 打开方式
    • 代码示例
    • 应用实例

方法步骤

  1. 引用头文件 < fstream >
  2. 创建流对象
  3. 指定文件及打开方式
  4. 读写数据
  5. 关闭文件

流对象

ofstream: 写操作 从内存向文件中输出数据 
ifstream: 读操作 从文件中向内存中输入数据
fstream: 读写操作
           

打开方式

打开方式
ios::in		为读文件而打开文件
ios::out 	为写文件而打开文件
ios::ate	初始位置为文件尾
ios::app 	追加方式写文件
ios::trunc	如果文件存在,则先删除在创建
ios::binary	二进制方式打开文件 
多种方式打开文件 可用 | 连接 ,如  ios::out | ios::trunc
           

代码示例

/*
20190918
c++ 文件操作 
*/ 

#include <iostream>
using namespace std;
#include <string>
//头文件
#include <fstream> 

/*
打开方式
ios::in		为读文件而打开文件
ios::out 	为写文件而打开文件
ios::ate	初始位置为文件尾
ios::app 	追加方式写文件
ios::trunc	如果文件存在,则先删除在创建
ios::binary	二进制方式打开文件 
多种方式打开文件 可用 | 连接 
*/

void writeToFile(){
	//从内存向文件中输出数据 
	//1.包含头文件 <fstream>
	//2.创建流对象
	ofstream ofs;
	
	//3.指定打开方式
	ofs.open("text.txt", ios::out | ios::trunc);
	
	//4.写数据
	ofs <<"hello world"<<endl;
	ofs <<"hello c++"<<endl;
	ofs <<"hello fstream"<<endl; 
	
	//5.关闭文件
	ofs.close(); 
} 

void readFile(int method){
	//从文件中向内存中输入数据
	 
	//1.创建流对象 
	ifstream ifs;
	
	//2.指定文件打开方式
	ifs.open("text.txt", ios::in);
	
	//3.判断文件是否打开成功
	if( !ifs.is_open() ){
		cout <<"文件打开失败"<<endl;
		return; 
	} 
	 
	//4.读数据 
	
	switch(method){
		case 1:{
			//方法1 
			char buf[1024] = {0};
			while(ifs >> buf){	//遇空格输出 
				cout <<buf <<endl;
			} 
			break;
		}
		case 2:{
			//方法2
			char buf2[1024] = {0};
			while(ifs.getline(buf2, sizeof(buf2))){	//读一整行 
				cout <<buf2 <<endl;
			} 
			break;
		}
		case 3:{
			//方法3
			string buf3; 
			while(getline(ifs, buf3)){	 //读一整行
				cout <<buf3 <<endl;
			}
			break;
		}
		case 4:{
			//方法4
			char c;
			while((c = ifs.get()) != EOF){	//	End Of File
				cout <<c;
			} 
			break;
		}
		default:{
			cout <<"method is wrong"<<endl;
			break;
		}	
		
	}
	
	//5.关闭文件 
	ifs.close();
}

int main(){
	//写文件 
	writeToFile(); 
	//读文件 
	int method = 2; 
	readFile(method);
	
	return 0;
} 
           

代码解释:

      使用了四种方法来写文件,亲自测试过的同学会发现他们的输出结果不同。方法1读到空格或换行,都会进行输出;方法2和方法3都是读一行,然后进行输出;方法4是每读一个字符就输出,所以读到换行符时才会换行。

具体结果如下

方法1:

c++文本读写操作详解

方法2、3、4:

c++文本读写操作详解

text.txt文本中的存储:

c++文本读写操作详解

应用实例

      读一个共有20行的由数字构成的文本文件,将里面的数据前15行读入到matrix1[15][10],第16到19行读入到matrix2[4][4],第20行读入到num中。

文本文件如下:

0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1 0 0
0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 0 1 0 0 0
1 1 1 0 0 0 1 1 1 1
0 0 0 0 1 0 0 0 0 0
1 0 0 0
1 0 0 0
1 0 0 0
1 0 0 0
1
           

代码如下:

/*
20190919
读文件实例 
*/ 

#include <iostream>
using namespace std;

#include <fstream>

int matrix1[15][10];
int matrix2[4][4];
int num;
 
int main(){
	//创建流对象
	ifstream ifs;
	
	//确定打开方式
	ifs.open("data.txt", ios::in);
	if(!ifs.is_open()){
		cout <<"文件打开失败!!"<<endl;
		return 0; 
	}
	
	//读数据 
	int temp_num;
	for(int i=0; i<15; i++){
		for(int j=0; j<10; j++){
			if(ifs >> temp_num){
				matrix1[i][j] = temp_num;
			}
		}
	} 
	for(int i=0; i<4; i++){
		for(int j=0; j<4; j++){
			if(ifs >> temp_num){
				matrix2[i][j] = temp_num;
			}
		}
	} 
	if(ifs >> temp_num){
		num = temp_num; 
	}
	//关闭文件
	ifs.close(); 
	
	//输出数据
	cout <<"matrix1 :"<<endl;
	for(int i=0; i<15; i++){
		for(int j=0; j<10; j++){
			cout <<matrix1[i][j]<<" ";
		}
		cout <<endl;
	} 
	cout <<"matrix2 :"<<endl;
	for(int i=0; i<4; i++){
		for(int j=0; j<4; j++){
			cout <<matrix2[i][j]<<" ";
		}
		cout <<endl;
	}
	cout <<"num :"<<endl;
	cout <<num<<endl;
} 
           

实验结果:

c++文本读写操作详解