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

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

 更新時(shí)間:2022年11月07日 09:06:00   作者:劃水貓  
C++的文件IO(Input,Output)操作就是指對(duì)文件進(jìn)行讀寫(輸入與輸出)的操作。輸入就是從磁盤上的文件中讀取內(nèi)容到內(nèi)存中。輸出就是將內(nèi)存中的數(shù)據(jù)內(nèi)容輸出或者說寫入到磁盤的文件中,這篇文章主要介紹了C++?IO設(shè)備讀寫功能實(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寫入文件默認(rèn)打開方式是ios::trunc,即沒有文件那么創(chuàng)建,該文件存在并且有內(nèi)容會(huì)直接清空內(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);  // 寫入文件,沒有文件會(huì)新建
	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默認(rèn)不會(huì)截?cái)辔募?/p>

#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 二進(jìn)制的讀寫

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

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

2.4.1 二進(jìn)制寫

#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main() {
	fstream outfile;
	char name[20];
	int age;
	// 為什么保存格式是dat,因?yàn)槭褂梦谋靖袷綍?huì)按照文本格式解析,最后出來的是亂碼
	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));  // 二進(jìn)制寫
	outfile.close();
	return 0;
}

2.4.2 二進(jìn)制讀

#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會(huì)累積
		// 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類型,這里的空格會(huì)替換文件的制表符或者空格
		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:相對(duì)位置

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

從開始位置文件指針偏移5個(gè)字節(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個(gè)字節(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個(gè)字節(jié)
	cout << "文件指針偏移量:" << infile.tellg() << endl;  // 相對(duì)于文件起始位置
	infile.close();
	return 0;
}

結(jié)果:

文件指針偏移量:5

E:\Microsoft Visual Studio\code\Project15\x64\Debug\Project15.exe (進(jìn)程 68440)已退出,代碼為 0。
要在調(diào)試停止時(shí)自動(dòng)關(guān)閉控制臺(tái),請(qǐng)啟用“工具”->“選項(xiàng)”->“調(diào)試”->“調(diào)試停止時(shí)自動(dòng)關(guān)閉控制臺(tái)”。
按任意鍵關(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);  // 指針先指向開頭,然后向后偏移三個(gè)字節(jié)
	outfile << "ABC";
	outfile.close();
	return 0;
}

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

相關(guān)文章

最新評(píng)論

尼勒克县| 惠安县| 安图县| 孟津县| 兴国县| 温泉县| 红原县| 衡东县| 万安县| 城步| 上饶市| 鞍山市| 西峡县| 三穗县| 珠海市| 府谷县| 浪卡子县| 阜平县| 永定县| 宾阳县| 玛曲县| 泾阳县| 扶沟县| 当雄县| 文水县| 镶黄旗| 宁强县| 富源县| 永昌县| 蒙城县| 阿合奇县| 交城县| 电白县| 当阳市| 都匀市| 民勤县| 永兴县| 冕宁县| 太保市| 阳城县| 孝义市|