C++?IO設備讀寫功能實現(xiàn)詳解
1 輸入輸出IO流
1.1 圖解輸入輸出流
IO設備:文件、終端(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(); // 文件結束需要關閉 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; }
結果:
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 文件流標志
這里常用的就是is_open()和eof()
2.7 文件指針
輸入流指針seekg
原形:basic_istream& seekg( off_type off, // 偏移量
std::ios_base::seekdir dir); // 起始位置
作用:設置輸入流位置
參數(shù)1:偏移量
參數(shù)2:相對位置
- beg 相對于開始位置
- cur 相對于當前位置
- end 相對于結束位置
從開始位置文件指針偏移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
作用:返回輸入流的當前位置(距離文件的起始位置的偏移量)
#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); // 設置偏移量位5個字節(jié) cout << "文件指針偏移量:" << infile.tellg() << endl; // 相對于文件起始位置 infile.close(); return 0; }
結果:
文件指針偏移量:5
E:\Microsoft Visual Studio\code\Project15\x64\Debug\Project15.exe (進程 68440)已退出,代碼為 0。
要在調(diào)試停止時自動關閉控制臺,請啟用“工具”->“選項”->“調(diào)試”->“調(diào)試停止時自動關閉控制臺”。
按任意鍵關閉此窗口. . .
輸出流指針seekp
作用:設置輸出流位置
函數(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; }
到此這篇關于C++ IO設備讀寫功能實現(xiàn)詳解的文章就介紹到這了,更多相關C++ IO設備讀寫內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
詳解QListWidget如何實現(xiàn)自定義Item效果
這篇文章主要為大家介紹了如何通過QListWidget實現(xiàn)自定義Item效果,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起了解一下2022-04-04C語言中的fscanf()函數(shù)與vfscanf()函數(shù)使用
這篇文章主要介紹了C語言中的fscanf()函數(shù)與vfscanf()函數(shù)使用,是C語言入門學習中的基礎知識,需要的朋友可以參考下2015-08-08C++處理輸入字符串并轉(zhuǎn)為數(shù)組的操作
這篇文章主要介紹了C++處理輸入字符串并轉(zhuǎn)為數(shù)組的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01C語言之素數(shù)(質(zhì)數(shù))的判斷以及輸出
這篇文章主要介紹了C語言之素數(shù)(質(zhì)數(shù))的判斷以及輸出方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03