最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C++?IO設(shè)備讀寫功能實現(xiàn)詳解

 更新時間:2022年11月07日 09:06:00   作者:劃水貓  
C++的文件IO(Input,Output)操作就是指對文件進行讀寫(輸入與輸出)的操作。輸入就是從磁盤上的文件中讀取內(nèi)容到內(nèi)存中。輸出就是將內(nèi)存中的數(shù)據(jù)內(nèi)容輸出或者說寫入到磁盤的文件中,這篇文章主要介紹了C++?IO設(shè)備讀寫功能實現(xiàn)

1 輸入輸出IO流

1.1 圖解輸入輸出流

IO設(shè)備:文件、終端(dos黑框框)、特殊的數(shù)據(jù)類型(streamstring)

1.2 輸入輸出流類庫

C++中的輸入輸出流是靠定義好的類庫來操作的

2 文件讀寫操作

2.1 文件的打開方式

2.2 文件讀寫類庫的頭文件

頭文件:fstream

ofstream:讀寫

istream:讀操作

ofstream:寫操作

2.3 文本文件讀寫

使用ofstream來寫文本

ofstream寫入文件默認打開方式是ios::trunc,即沒有文件那么創(chuàng)建,該文件存在并且有內(nèi)容會直接清空內(nèi)容

#include<iostream>
#include<windows.h>
#include<fstream>
using namespace std;
int main() {
	ofstream outfile;
	string name;
	int age;
	cin >> name >> age;
	outfile.open("C:/Users/98207/desktop/test.txt", ios::out);  // 寫入文件,沒有文件會新建
	outfile << name << endl;
	outfile << age;
	outfile.close();  // 文件結(jié)束需要關(guān)閉
	return 0;
}

使用ifstream讀取文件

程序:

#include<iostream>
#include<windows.h>
#include<string>
#include<fstream>
using namespace std;
int main() {
	ifstream infile;
	string str;
	int age;
	infile.open("C:/Users/98207/desktop/test.txt", ios::in);  // 讀取文件
	while (1) {
		if (infile.eof()) {
			break;
		}
		infile >> str;
		cout << str << endl;;
		// getline(infile, str);
		// cout << str << endl;
	}
	infile .close();
	return 0;
}

結(jié)果:

bian
12

使用fstream來讀寫文件

寫入文件fstream默認不會截斷文件

#include<iostream>
#include<windows.h>
#include<string>
#include<fstream>
using namespace std;
int main() {
	string name;
	int age;
	fstream outfile;
	outfile.open("C:/Users/98207/Desktop/test2.txt", ios::out);
	cin >> name >> age;
	outfile << name << endl;
	outfile << age;
	outfile.close();
	return 0;
}

讀取文件

#include<iostream>
#include<windows.h>
#include<string>
#include<fstream>
using namespace std;
int main() {
	string str;
	fstream infile;
	infile.open("C:/Users/98207/Desktop/test2.txt", ios::in);
	while (1) {
		if (infile.eof()) {
			break;
		}
		infile >> str;
		cout << str << endl;
	}
	infile.close();
	return 0;
}

2.4 二進制的讀寫

二進制和文本寫區(qū)別在于數(shù)字,二進制數(shù)字是把實際字節(jié)數(shù)寫入進去。

比如整數(shù)9,那么寫入的是4個char字符0009,至于存儲的大小端方式要看cpu。

2.4.1 二進制寫

#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main() {
	fstream outfile;
	char name[20];
	int age;
	// 為什么保存格式是dat,因為使用文本格式會按照文本格式解析,最后出來的是亂碼
	outfile.open("C:/Users/98207/Desktop/1.dat", ios::trunc | ios::out | ios::binary);
	cin >> name >> age;
	outfile << name << '\t';
	outfile.write((char*)&age, sizeof(age));  // 二進制寫
	outfile.close();
	return 0;
}

2.4.2 二進制讀

#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main() {
	fstream infile;
	char name[20];
	char temp;
	int age;
	infile.open("C:/Users/98207/Desktop/1.dat", ios::in | ios::binary);
	infile >> name;
	infile.read((char*)&temp, sizeof(temp));  // 丟棄制表符
	infile.read((char*)&age, sizeof(age));
	cout << name << '\t' << age << endl;
	infile.close();
	return 0;
}

2.5 按照特殊格式讀寫

2.5.1 特殊格式寫入

#include<iostream>
#include<fstream>  //ofstream
#include<sstream>  // stringstream
using namespace std;
int main() {
	stringstream ret;
	ofstream outfile;
	string name;
	int age;
	outfile.open("C:/Users/98207/Desktop/test2.txt", ios::out | ios::trunc);
	while (1) {
		cin >> name >> age;
		if (cin.eof()) {
			break;
		}
		ret << name << "\t\t\t" << age << endl;  // ret會累積
		// outfile << ret.str();
		// ret.clear();
	}
	outfile << ret.str();
	outfile.close();
	return 0;
}

2.5.2 特殊格式讀取

#include<iostream>
#include<fstream>
#include<string>  // getline, string
using namespace std;
int main() {
	fstream infile;
	string str;
	char name[20];
	int age;
	infile.open("C:/Users/98207/Desktop/test2.txt", ios::in);
	while (1) {
		getline(infile, str);
		if (infile.eof()) {
			break;
		}
		sscanf_s(str.c_str(), "%s %d", name, sizeof(name), & age);  // 這里的參數(shù)只能是char類型,這里的空格會替換文件的制表符或者空格
		cout << name << "\t\t\t" << age << endl;
	}
	infile.close();
	return 0;
}

2.6 文件流標(biāo)志

這里常用的就是is_open()和eof()

2.7 文件指針

輸入流指針seekg

原形:basic_istream& seekg( off_type off, // 偏移量

std::ios_base::seekdir dir); // 起始位置

作用:設(shè)置輸入流位置

參數(shù)1:偏移量

參數(shù)2:相對位置

  • beg 相對于開始位置
  • cur 相對于當(dāng)前位置
  • end 相對于結(jié)束位置

從開始位置文件指針偏移5個字節(jié),然后讀取內(nèi)容

#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main() {
	// ofstream infile;
	ifstream infile;
	string str;
	infile.open("C:/Users/98207/Desktop/1.txt", ios::in);
	infile.seekg(5, ios::beg);  // 從開始位置偏移5個字節(jié)
	while (1) {
		getline(infile, str);
		cout << str;
		if (infile.eof()) {
			break;
		}
	}
	infile.close();
	return 0;
}

輸入流指針tellg

作用:返回輸入流的當(dāng)前位置(距離文件的起始位置的偏移量)

#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main() {
	ifstream infile;
	string str;
	infile.open("C:/Users/98207/Desktop/1.txt", ios::in);
	infile.seekg(5, ios::beg);  // 設(shè)置偏移量位5個字節(jié)
	cout << "文件指針偏移量:" << infile.tellg() << endl;  // 相對于文件起始位置
	infile.close();
	return 0;
}

結(jié)果:

文件指針偏移量:5

E:\Microsoft Visual Studio\code\Project15\x64\Debug\Project15.exe (進程 68440)已退出,代碼為 0。
要在調(diào)試停止時自動關(guān)閉控制臺,請啟用“工具”->“選項”->“調(diào)試”->“調(diào)試停止時自動關(guān)閉控制臺”。
按任意鍵關(guān)閉此窗口. . .

輸出流指針seekp

作用:設(shè)置輸出流位置

函數(shù)原形:basic_ostream& seekp( off_type off, // 偏移量

std::ios_base::seekdir dir ); // 起始位置

#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main() {
	ofstream outfile;
	outfile.open("user1.txt", ios::out | ios::trunc);
	outfile << "123456789";
	outfile.seekp(3, ios::beg);  // 指針先指向開頭,然后向后偏移三個字節(jié)
	outfile << "ABC";
	outfile.close();
	return 0;
}

到此這篇關(guān)于C++ IO設(shè)備讀寫功能實現(xiàn)詳解的文章就介紹到這了,更多相關(guān)C++ IO設(shè)備讀寫內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

棋牌| 昌宁县| 莱阳市| 清新县| 武宣县| 万荣县| 鄄城县| 宁远县| 响水县| 永安市| 长兴县| 榆树市| 石首市| 舟曲县| 辽中县| 武强县| 晋宁县| 庆云县| 浮梁县| 万山特区| 南漳县| 大新县| 江北区| 惠州市| 定边县| 康定县| 霍城县| 凉山| 韶关市| 华池县| 锦屏县| 竹山县| 麟游县| 馆陶县| 辛集市| 前郭尔| 寿光市| 洛南县| 乡城县| 九龙城区| 大姚县|