C++ I/O文件讀寫操作的示例代碼
IO: 向設(shè)備輸入數(shù)據(jù)和輸出數(shù)據(jù)C++的IO流

c++中,必須通過特定的已經(jīng)定義好的類, 來(lái)處理IO(輸入輸出)

文件流: 對(duì)文件進(jìn)行讀寫操作
頭文件:
類庫(kù):
ifstream 對(duì)文件輸入(讀文件)
ofstream 對(duì)文件輸出(寫文件)
fstream 對(duì)文件輸入或輸出

//寫文件
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
int age;
ofstream outfile; //也可以使用fstream, 但是fstream的默認(rèn)打開方式不截?cái)辔募L(zhǎng)度
// ofstream的默認(rèn)打開方式是, 截?cái)嗍綄懭?ios::out | ios::trunc
// fstream的默認(rèn)打開方式是, 截?cái)嗍綄懭? ios::out
// 建議指定打開方式
outfile.open("user.txt", ios::out | ios::trunc);
while (1) {
cout << "請(qǐng)輸入姓名: [ctrl+z退出] ";
cin >> name;
if (cin.eof()) { //判斷文件是否結(jié)束
break;
}
outfile << name << "\t";
cout << "請(qǐng)輸入年齡: ";
cin >> age;
outfile << age << endl; //文本文件寫入
}
// 關(guān)閉打開的文件
outfile.close();
system("pause");
return 0;
}
//讀文件
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
int age;
ifstream infile;
infile.open("user.txt");
while (1) {
infile >> name;
if (infile.eof()) { //判斷文件是否結(jié)束
break;
}
cout << name << "\t";
infile >> age;
cout << age << endl;
}
// 關(guān)閉打開的文件
infile.close();
system("pause");
return 0;
}
對(duì)二進(jìn)制文件流讀寫
文本文件和二進(jìn)制文件的區(qū)別?
文本文件: 寫數(shù)字1, 實(shí)際寫入的是 ‘1'
二進(jìn)制文件:寫數(shù)字1, 實(shí)際寫入的是 整數(shù)1(4個(gè)字節(jié),最低字節(jié)是1, 高3個(gè)字節(jié)都是0)
寫字符‘R'實(shí)際輸入的還是‘R'
寫二進(jìn)制文件
使用文件流對(duì)象的write方法寫入二進(jìn)制數(shù)據(jù).
//寫二進(jìn)制文件
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
int age;
ofstream outfile;
outfile.open("user.dat", ios::out | ios::trunc | ios::binary);
while (1) {
cout << "請(qǐng)輸入姓名: [ctrl+z退出] ";
cin >> name;
if (cin.eof()) { //判斷文件是否結(jié)束
break;
}
outfile << name << "\t";
cout << "請(qǐng)輸入年齡: ";
cin >> age;
//outfile << age << endl; //會(huì)自動(dòng)轉(zhuǎn)成文本方式寫入
outfile.write((char*)&age, sizeof(age));
}
// 關(guān)閉打開的文件
outfile.close();
system("pause");
return 0;
}
//讀二進(jìn)制文件
使用文件流對(duì)象的read方法.
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
int age;
ifstream infile;
infile.open("user.dat", ios::in | ios::binary);
while (1) {
infile >> name;
if (infile.eof()) { //判斷文件是否結(jié)束
break;
}
cout << name << "\t";
// 跳過中間的制表符
char tmp;
infile.read(&tmp, sizeof(tmp));
//infile >> age; //從文本文件中讀取整數(shù), 使用這個(gè)方式
infile.read((char*)&age, sizeof(age));
cout << age << endl; //文本文件寫入
}
// 關(guān)閉打開的文件
infile.close();
system("pause");
return 0;
}
對(duì)文件流按格式讀寫取數(shù)據(jù)
使用stringstream
#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
string name;
int age;
ofstream outfile;
outfile.open("user.txt", ios::out | ios::trunc);
while (1) {
cout << "請(qǐng)輸入姓名: [ctrl+z退出] ";
cin >> name;
if (cin.eof()) { //判斷文件是否結(jié)束
break;
}
cout << "請(qǐng)輸入年齡: ";
cin >> age;
stringstream s;
s << "name:" << name << "\t\tage:" << age << endl;
outfile << s.str();
}
// 關(guān)閉打開的文件
outfile.close();
system("pause");
return 0;
}
按指定格式讀文件
沒有優(yōu)雅的C++解決方案, 使用C語(yǔ)言的sscanf
#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
#include <Windows.h>
using namespace std;
int main(void)
{
char name[32];
int age;
string line;
ifstream infile;
infile.open("user.txt");
while (1) {
getline(infile, line);
if (infile.eof()) { //判斷文件是否結(jié)束
break;
}
sscanf_s(line.c_str(), "姓名:%s 年齡:%d", name, sizeof(name),&age);
cout << "姓名:" << name << "\t\t年齡:" << age << endl;
}
infile.close();
system("pause");
return 0;
}
文件流的狀態(tài)檢查
s.is_open( )
文件流是否打開成功,
s.eof( ) 流s是否結(jié)束
s.fail( )
流s的failbit或者badbit被置位時(shí), 返回true
failbit: 出現(xiàn)非致命錯(cuò)誤,可挽回, 一般是軟件錯(cuò)誤
badbit置位, 出現(xiàn)致命錯(cuò)誤, 一般是硬件錯(cuò)誤或系統(tǒng)底層錯(cuò)誤, 不可挽回
s.bad( )
流s的badbit置位時(shí), 返回true
s.good( )
流s處于有效狀態(tài)時(shí), 返回true
s.clear( )
流s的所有狀態(tài)都被復(fù)位
文件流的定位
seekg( off_type offset, //偏移量
ios::seekdir origin ); //起始位置
作用:設(shè)置輸入流的位置
參數(shù)1: 偏移量
參數(shù)2: 相對(duì)位置
beg 相對(duì)于開始位置
cur 相對(duì)于當(dāng)前位置
end相對(duì)于結(jié)束位置
讀取當(dāng)前程序的最后50個(gè)字符
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(void) {
ifstream infile;
infile.open("定位.cpp");
if (!infile.is_open()) {
return 1;
}
infile.seekg(-50, infile.end);
while (!infile.eof()) {
string line;
getline(infile, line);
cout << line << endl;
}
infile.close();
system("pause");
return 0;
}
//tellg返回該輸入流的當(dāng)前位置(距離文件的起始位置的偏移量)
獲取當(dāng)前文件的長(zhǎng)度
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(void) {
ifstream infile;
infile.open("定位.cpp");
if (!infile.is_open()) {
return 1;
}
// 先把文件指針移動(dòng)到文件尾
infile.seekg(0, infile.end);
int len = infile.tellg();
cout << "len:" << len;
infile.close();
system("pause");
return 0;
}
seekp設(shè)置該輸出流的位置
先向新文件寫入:“123456789”
然后再在第4個(gè)字符位置寫入“ABC”
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(void) {
ofstream outfile;
outfile.open("test.txt");
if (!outfile.is_open()) {
return 1;
}
outfile << "123456789";
outfile.seekp(4, outfile.beg);
outfile << "ABC";
outfile.close();
system("pause");
return 0;
}
計(jì)算機(jī)英語(yǔ)
ioinput output的簡(jiǎn)寫
輸出輸出
iosC++ 輸出輸出流的基類
stream流
istream輸入流
ostream輸出流
iostream輸出輸出流
fstreamfile stream的簡(jiǎn)寫 文件流
ifstream文件輸入流
ofstream文件輸出流
stringstream字符串流
istringstream字符串輸入流
ostringstream字符串輸出流
seek尋找
seekg 設(shè)置輸入流的位置
seekp設(shè)置輸出流的位置
tell告訴
tellg返回當(dāng)前流的位置
open打開
is_open是否打開成功
eofend of file的簡(jiǎn)寫
文件結(jié)束
good好的
bad壞的
fail失敗
常見錯(cuò)誤總結(jié)
1.文件沒有關(guān)閉
文件沒有關(guān)閉, close(),可能導(dǎo)致寫文件失敗
2.文件打開方式不合適
3.在VS2015的部分版本中,當(dāng)sscanf和sscanf_s的格式字符串中含有中文時(shí),可能會(huì)讀取失敗。
在vs2019中未發(fā)現(xiàn)該類問題。
到此這篇關(guān)于C++ I/O文件讀寫操作的示例代碼的文章就介紹到這了,更多相關(guān)C++ I/O文件讀寫操作內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++詳細(xì)講解圖論的基礎(chǔ)與圖的儲(chǔ)存
圖論〔Graph?Theory〕是數(shù)學(xué)的一個(gè)分支。它以圖為研究對(duì)象。圖論中的圖是由若干給定的點(diǎn)及連接兩點(diǎn)的線所構(gòu)成的圖形,這種圖形通常用來(lái)描述某些事物之間的某種特定關(guān)系,用點(diǎn)代表事物,用連接兩點(diǎn)的線表示相應(yīng)兩個(gè)事物間具有這種關(guān)系2022-05-05
c語(yǔ)言實(shí)現(xiàn)基數(shù)排序解析及代碼示例
這篇文章主要介紹了c語(yǔ)言實(shí)現(xiàn)基數(shù)排序解析及代碼示例,具有一定借鑒價(jià)值,需要的朋友可以參考下。2017-12-12
MFC串口通信發(fā)送16進(jìn)制數(shù)據(jù)的方法
這篇文章主要為大家詳細(xì)介紹了MFC串口通信發(fā)送16進(jìn)制數(shù)據(jù),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01

