柚子快報激活碼778899分享:開發(fā)語言 C++的IO流與操作
柚子快報激活碼778899分享:開發(fā)語言 C++的IO流與操作
一. C++IO流
IO的定義: 向設(shè)備輸入數(shù)據(jù)和輸出數(shù)據(jù)
C++的IO流
設(shè)備:
文件控制臺特定的數(shù)據(jù)類型(stringstream)
c++中,必須通過特定的已經(jīng)定義好的類, 來處理IO(輸入輸出),一共11個類。
二.文件流打開、讀寫文件
文件流: 對文件進(jìn)行讀寫操作頭文件: fstream類庫: ifstream 對文件輸入(讀文件) ofstream 對文件輸出(寫文件) fstream 對文件輸入或輸出
文件打開方式:
模式標(biāo)志描述ios::in讀方式打開文件ios:out寫方式打開文件ios::trunc如果此文件已經(jīng)存在, 就會打開文件之前把文件長度截斷為0ios::app尾部追加加方式(在尾部寫入)ios::ate文件打開后, 定位到文件尾ios::binary二進(jìn)制方式(默認(rèn)是文本方式)
以上打開方式, 可以使用位操作 | 組合起來
1.對文本文件流讀寫
①寫文本文件
#include
#include
#include
using namespace std;
int main()
{
string name;
int age;
ofstream outfile; //也可以使用fstream, 但是fstream的默認(rèn)打開方式不截斷文件長度,直接從第一個開始覆蓋
// ofstream的默認(rèn)打開方式是, 截斷式寫入 ios::out | ios::trunc
// fstream的默認(rèn)打開方式是, 截斷式寫入 ios::out
// 建議指定打開方式,讓原文件長度截斷為0
outfile.open("user.txt", ios::out | ios::trunc);
while (1) {
cout << "請輸入姓名: [ctrl+z退出] ";
cin >> name;
if (cin.eof()) { //判斷文件是否結(jié)束
break;
}
outfile << name << "\t";
cout << "請輸入年齡: ";
cin >> age;
outfile << age << endl; //文本文件寫入
}
// 關(guān)閉打開的文件
outfile.close();
system("pause");
return 0;
}
②讀文本文件
#include
#include
#include
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;
}
2.對二進(jìn)制文件流讀寫
文本文件和二進(jìn)制文件的區(qū)別?
文本文件: 寫數(shù)字1, 實際寫入的是 ‘1’ 二進(jìn)制文件:寫數(shù)字1, 實際寫入的是 整數(shù)1(4個字節(jié),最低字節(jié)是1, 高3個字節(jié)都是0);寫字符‘R’實際輸入的還是‘R’
①寫二進(jìn)制文件
使用文件流對象的write方法寫入二進(jìn)制數(shù)據(jù).
#include
#include
#include
using namespace std;
int main()
{
string name;
int age;
ofstream outfile;
//因為.txt是文本文件
outfile.open("user.dat", ios::out | ios::trunc | ios::binary);
while (1) {
cout << "請輸入姓名: [ctrl+z退出] ";
cin >> name;
if (cin.eof()) { //判斷文件是否結(jié)束
break;
}
outfile << name << "\t";
cout << "請輸入年齡: ";
cin >> age;
//outfile << age << endl; //會自動轉(zhuǎn)成文本方式寫入
outfile.write((char*)&age, sizeof(age));//寫入內(nèi)存
}
// 關(guān)閉打開的文件
outfile.close();
system("pause");
return 0;
}
注意:如果用這種方式outfile << age << endl;那么文件中顯示的將是整數(shù)對應(yīng)的字符。
②讀二進(jìn)制文件
使用文件流對象的read方法.
#include
#include
#include
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ù), 使用這個方式
infile.read((char*)&age, sizeof(age));
cout << age << endl;
}
// 關(guān)閉打開的文件
infile.close();
system("pause");
return 0;
}
3.對文件流按格式讀寫取數(shù)據(jù)
使用stringstream
①按指定格式寫文件
#include
#include
#include
#include
using namespace std;
int main()
{
string name;
int age;
ofstream outfile;
outfile.open("user.txt", ios::out | ios::trunc);
while (1) {
cout << "請輸入姓名: [ctrl+z退出] ";
cin >> name;
if (cin.eof()) { //判斷文件是否結(jié)束
break;
}
cout << "請輸入年齡: ";
cin >> age;
//向特殊的類型s中塞東西
stringstream s;
s << "name:" << name << "\t\tage:" << age << endl;
outfile << s.str(); //轉(zhuǎn)為string對象
}
// 關(guān)閉打開的文件
outfile.close();
system("pause");
return 0;
}
②按指定格式讀文件
C語言中有fscanf可以用,但是C++沒有很好的解決方案, 需使用C語言的sscanf
#include
#include
#include
#include
#include
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;
}
//轉(zhuǎn)為C語言類型的字符串
sscanf_s(line.c_str(), "姓名:%s 年齡:%d", name, sizeof(name),&age);
cout << "姓名:" << name << "\t\t年齡:" << age << endl;
}
infile.close();
system("pause");
return 0;
}
4.文件流的狀態(tài)檢查
s.is_open( )
文件流是否打開成功,
s.eof( )
流s是否結(jié)束
s.fail( )
流s的failbit或者badbit被置位時, 返回true failbit: 出現(xiàn)非致命錯誤,可挽回, 一般是軟件錯誤 badbit置位, 出現(xiàn)致命錯誤, 一般是硬件錯誤或系統(tǒng)底層錯誤, 不可挽回
s.bad( )
流s的badbit置位時, 返回true
s.good( )
流s處于有效狀態(tài)時, 返回true
s.clear( )
流s的所有狀態(tài)都被復(fù)位
補(bǔ)充:
cin.ignore(count, c)
從輸入流中提取并丟棄字符,直到遇到下列三種情況 1.提取的字符達(dá)到了參數(shù)count指定的數(shù)量 2.在輸入序列中遇到文件結(jié)束(EOF) 3.輸入序列中的下一個字符為參數(shù)c指定的字符(這個字符會被提取并丟棄)
count常常?。?std::numeric_limitsstd::streamsize::max() 相當(dāng)于IO流的最大字符個數(shù)
常見用法:(把標(biāo)準(zhǔn)輸入緩沖區(qū)cin的所有數(shù)據(jù)都清空) cin.ignore(std::numeric_limits::max(), ‘\n’); cin.ignore(); 一般用于用完cin后面又用getline等會讀取換行符的輸入。
5.隨機(jī)讀寫:文件流的定位
①seekg
偏移量 起始位置 seekg( off_type offset, ios::seekdir origin );
作用:設(shè)置輸入流的位置
參數(shù)1: 偏移量 參數(shù)2: 相對位置 beg 相對于開始位置 cur 相對于當(dāng)前位置 end 相對于結(jié)束位置
讀取當(dāng)前程序的最后50個字符
#include
#include
#include
using namespace std;
int main(void) {
ifstream infile;
infile.open("定位.cpp");
if (!infile.is_open()) {
return 1;
}
infile.seekg(-50, infile.end); //定位到倒數(shù)第50的位置
while (!infile.eof()) {
string line;
getline(infile, line);
cout << line << endl;
}
infile.close();
system("pause");
return 0;
}
②tellg
返回該輸入流的當(dāng)前位置(距離文件的起始位置的偏移量) 獲取當(dāng)前文件的長度
#include
#include
#include
using namespace std;
int main(void) {
ifstream infile;
infile.open("定位.cpp");
if (!infile.is_open()) {
return 1;
}
// 先把文件指針移動到文件尾
infile.seekg(0, infile.end);
int len = infile.tellg();
cout << "len:" << len;
infile.close();
system("pause");
return 0;
}
③seekp
設(shè)置該輸出流的位置
先向新文件寫入:“123456789” 然后再在第4個字符位置寫入“ABC”
#include
#include
#include
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;
}
總結(jié):
文件沒有關(guān)閉 :文件沒有關(guān)閉, close(),可能導(dǎo)致寫文件失敗文件打開方式不合適在VS2015的部分版本中,當(dāng)sscanf和sscanf_s的格式字符串中含有中文時,可能會讀取失敗。在vs2019中未發(fā)現(xiàn)該類問題。
柚子快報激活碼778899分享:開發(fā)語言 C++的IO流與操作
推薦閱讀
本文內(nèi)容根據(jù)網(wǎng)絡(luò)資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點和立場。
轉(zhuǎn)載請注明,如有侵權(quán),聯(lián)系刪除。