c++ Text Read-Write Details

Keywords: iOS

c++ Text Read-Write

Method Steps

  1. Reference header file < fstream >
  2. Create Stream Object
  3. Specify the file and how to open it
  4. Read and write data
  5. Close File

Stream Object

ofstream:Write output data from memory to file 
ifstream:Read operations enter data from a file into memory
 fstream:read and write operations

Open Mode

Open Mode
 ios::in Open file for reading
 ios::out Opens a file for writing
 ios::ate is initially at the end of the file
 ios::app. Append Write File
 ios::trunc If the file exists, delete it before creating it
 ios::binary Open File Binary 
There are several ways to open a file | connections, such as ios::out | ios::trunc

Code Samples

/*
20190918
c++ File Operation 
*/ 

#include <iostream>
using namespace std;
#include <string>
//header file
#include <fstream> 

/*
Open Mode
ios::in		Open file for reading
ios::out 	Open a file for writing
ios::ate	Initial position is at end of file
ios::app 	Write files by appending
ios::trunc	If the file exists, delete it before creating it
ios::binary	Open file binary 
Multiple ways to open files available | Connect 
*/

void writeToFile(){
	//Output data from memory to file 
	//1. Include header file <fstream>
	//2. Create Stream Objects
	ofstream ofs;
	
	//3. Specify how to open
	ofs.open("text.txt", ios::out | ios::trunc);
	
	//4. Write data
	ofs <<"hello world"<<endl;
	ofs <<"hello c++"<<endl;
	ofs <<"hello fstream"<<endl; 
	
	//5. Close the file
	ofs.close(); 
} 

void readFile(int method){
	//Enter data from a file into memory
	 
	//1. Create Stream Objects 
	ifstream ifs;
	
	//2. Specify how to open the file
	ifs.open("text.txt", ios::in);
	
	//3. Determine if the file was opened successfully
	if( !ifs.is_open() ){
		cout <<"File Open Failed"<<endl;
		return; 
	} 
	 
	//4. Read Data 
	
	switch(method){
		case 1:{
			//Method 1 
			char buf[1024] = {0};
			while(ifs >> buf){	//Space Output 
				cout <<buf <<endl;
			} 
			break;
		}
		case 2:{
			//Method 2
			char buf2[1024] = {0};
			while(ifs.getline(buf2, sizeof(buf2))){	//Read the whole line 
				cout <<buf2 <<endl;
			} 
			break;
		}
		case 3:{
			//Method 3
			string buf3; 
			while(getline(ifs, buf3)){	 //Read the whole line
				cout <<buf3 <<endl;
			}
			break;
		}
		case 4:{
			//Method 4
			char c;
			while((c = ifs.get()) != EOF){	//	End Of File
				cout <<c;
			} 
			break;
		}
		default:{
			cout <<"method is wrong"<<endl;
			break;
		}	
		
	}
	
	//5. Close the file 
	ifs.close();
}

int main(){
	//Write File 
	writeToFile(); 
	//read file 
	int method = 2; 
	readFile(method);
	
	return 0;
} 

Code explanation:
There are four ways to write files, and students who have tested them personally will find that their output is different.Method 1 reads a space or a line break and outputs it; both methods 2 and 3 read a line and then output it; and method 4 outputs every character read, so line breaks are not read until a line break is read.
The results are as follows
Method 1:
      
Methods 2, 3, 4:
      
Storage in text.txt text:
      

Application Instances

Read a 20-line text file consisting of numbers, read the first 15 lines of data into matrix1[15][10], lines 16 to 19 into matrix2[4][4], and lines 20 into num.
The text file is as follows:

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

The code is as follows:

/*
20190919
 Read File Instances 
*/ 

#include <iostream>
using namespace std;

#include <fstream>

int matrix1[15][10];
int matrix2[4][4];
int num;
 
int main(){
	//Create Stream Object
	ifstream ifs;
	
	//Determine how to open
	ifs.open("data.txt", ios::in);
	if(!ifs.is_open()){
		cout <<"File open failed!!"<<endl;
		return 0; 
	}
	
	//Read Data 
	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; 
	}
	//Close File
	ifs.close(); 
	
	//output data
	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;
} 

Experimental results:
      

Posted by cirko on Thu, 19 Sep 2019 18:07:59 -0700