C++?IO設(shè)備讀寫功能實(shí)現(xiàn)詳解
1 輸入輸出IO流
1.1 圖解輸入輸出流
IO設(shè)備:文件、終端(dos黑框框)、特殊的數(shù)據(jù)類型(streamstring)

1.2 輸入輸出流類庫(kù)
C++中的輸入輸出流是靠定義好的類庫(kù)來(lái)操作的

2 文件讀寫操作
2.1 文件的打開方式

2.2 文件讀寫類庫(kù)的頭文件
頭文件:fstream
ofstream:讀寫
istream:讀操作
ofstream:寫操作
2.3 文本文件讀寫
使用ofstream來(lái)寫文本
ofstream寫入文件默認(rèn)打開方式是ios::trunc,即沒(méi)有文件那么創(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); // 寫入文件,沒(méi)有文件會(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來(lái)讀寫文件
寫入文件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ì)按照文本格式解析,最后出來(lái)的是亂碼
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)文章
詳解QListWidget如何實(shí)現(xiàn)自定義Item效果
這篇文章主要為大家介紹了如何通過(guò)QListWidget實(shí)現(xiàn)自定義Item效果,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起了解一下2022-04-04
C語(yǔ)言中的fscanf()函數(shù)與vfscanf()函數(shù)使用
這篇文章主要介紹了C語(yǔ)言中的fscanf()函數(shù)與vfscanf()函數(shù)使用,是C語(yǔ)言入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-08-08
C語(yǔ)言二進(jìn)制思想以及數(shù)據(jù)的存儲(chǔ)
本文主要介紹了C語(yǔ)言的二進(jìn)制思想以及數(shù)據(jù)的存儲(chǔ),這里對(duì)二進(jìn)制和數(shù)據(jù)存儲(chǔ)做了詳細(xì)的說(shuō)明,對(duì)開始學(xué)習(xí)C語(yǔ)言的同學(xué)比較有參考價(jià)值2016-07-07
C++處理輸入字符串并轉(zhuǎn)為數(shù)組的操作
這篇文章主要介紹了C++處理輸入字符串并轉(zhuǎn)為數(shù)組的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-01-01
C++11?nullptr實(shí)現(xiàn)初始化空指針
避免產(chǎn)生“野指針”最有效的方法,就是在定義指針的同時(shí)完成初始化操作,本文主要介紹了C++11?nullptr初始化空指針,感興趣的可以了解一下2022-01-01
C語(yǔ)言之素?cái)?shù)(質(zhì)數(shù))的判斷以及輸出
這篇文章主要介紹了C語(yǔ)言之素?cái)?shù)(質(zhì)數(shù))的判斷以及輸出方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03

