一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - C/C++ - 帶你粗略了解C++流的讀寫文件

帶你粗略了解C++流的讀寫文件

2021-12-20 15:02_Space_ C/C++

這篇文章主要為大家總結(jié)了C++中輸入輸出流及文件流操作,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能給你帶來幫助

讀寫文本文件

C++的IO流:
IO:向設(shè)備輸入數(shù)據(jù)和輸出數(shù)據(jù)

設(shè)備有:
1)文件
2)控制臺
3)特定的數(shù)據(jù)類型(stringstream)
C++中,必須通過特定的已經(jīng)定義好的類, 來處理IO(輸入輸出)

帶你粗略了解C++流的讀寫文件

C++的 IO類庫為:

帶你粗略了解C++流的讀寫文件

文件流:對文件進(jìn)行讀寫操作
頭文件: < fstream >

ifstream 對文件輸入(讀文件)
ofstream 對文件輸出(寫文件)
fstream 對文件輸入或輸出

文件的打開方式:

模式標(biāo)志 描述
ios::in 讀方式打開文件
ios:out 寫方式打開文件
ios::trunc 如果此文件已經(jīng)存在, 就會打開文件之前把文件長度截?cái)酁?
ios::app 尾部最加方式(在尾部寫入)
ios::ate 文件打開后, 定位到文件尾
ios::binary 二進(jìn)制方式(默認(rèn)是文本方式)

寫文本文件

#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main()
{
	string name;
	int age;
	ofstream outfile;  //也可以使用fstream, 但是fstream的默認(rèn)打開方式不截?cái)辔募L度
	// 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 << "[ctrl+z退出]" << endl;
		cout << "請輸入姓名:";
		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 <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;
}

二進(jìn)制讀寫文件

寫二進(jìn)制文件
使用文件流對象的write方法寫入二進(jìn)制數(shù)據(jù).

注:若 ***outfile << age << end;***
寫入文件會轉(zhuǎn)換到文本方式寫入
需要使用write(寫)吧整形轉(zhuǎn)換到char類型,進(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 << "請輸入姓名: [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));
	}
	// 關(guān)閉打開的文件
	outfile.close();
	system("pause");
	return 0;
}

二進(jìn)制讀文件

需使用read(讀)吧寫入的內(nèi)容讀取出來并輸出

#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ù), 使用這個方式
		infile.read((char*)&age, sizeof(age));
		cout << age << endl;  //文本文件寫入
	}
	// 關(guān)閉打開的文件
	infile.close();
	system("pause");
	return 0;
}

按指定格式讀寫文件

指定格式寫文件:
使用 < 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 << "[ctrl+z退出]" << endl;
		cout << "請輸入姓名: ";
		cin >> name;
		if (cin.eof()) { //判斷文件是否結(jié)束
			break;
		}
		cout << "請輸入年齡: ";
		cin >> age;
		stringstream s;
		s << "name:" << name << "\t\tage:" << age << endl;
		outfile << s.str();
	}
	// 關(guān)閉打開的文件
	outfile.close();
	system("pause");
	return 0;
}

指定格式讀文件:
在C++指定格式讀文件并沒有優(yōu)雅的解決方案
就用C語言的: 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)檢查

描述
is_open() 文件流是否打開成功
eof() 流是否結(jié)束
fail() 流的failbit或者badbit被置位時(shí), 返回true
failbit: 出現(xiàn)非致命錯誤,可挽回, 一般是軟件錯誤 badbit:置位, 出現(xiàn)致命錯誤, 一般是硬件錯誤或系統(tǒng)底層錯誤, 不可挽回
bad() 流的badbit置位時(shí), 返回true
good() 流處于有效狀態(tài)時(shí), 返回true
clear() 流的所有狀態(tài)都被復(fù)位

文件流的三種定位 seekg tellg seekp

seekg:

seekg( off_type offset, //偏移量
ios::seekdir origin ); //起始位置
作用:設(shè)置輸入流的位置
參數(shù)1: 偏移量
參數(shù)2: 相對位置
beg 相對于開始位置
cur 相對于當(dāng)前位置
end 相對于結(jié)束位置

獲取文件的最后50個字符:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(void) {
	ifstream infile;
	infile.open(/*文件名字這里我就不寫了*/".cpp");
	if (!infile.is_open()) {
		return 1;
	}
		//定位到最后50個字母
	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)前位置(距離文件的起始位置的偏移量)

獲取文件的長度:

#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(0, infile.end);
	int len = infile.tellg();
	cout << "len:" << len;
	infile.close();
	system("pause");
	return 0;
}

seekp

設(shè)置該輸出流的位置

先向新文件寫入:“123456789”

然后再在第4個字符位置寫入"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;
}

常見的錯誤

1.文件沒有關(guān)閉, close(),可能導(dǎo)致寫文件失敗
2.文件打開方式不合適
3.在VS2015的部分版本中,當(dāng)sscanf和sscanf_s的格式字符串中含有中文時(shí),可能會讀取失敗。

 

總結(jié)

本篇文章就到這里了,希望能給你帶來幫助,也希望您能夠多多關(guān)注服務(wù)器之家的更多內(nèi)容!

原文鏈接:https://blog.csdn.net/qq_45140193/article/details/119789181

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 大东北chinesexxxx露脸 | 精品国产线拍大陆久久尤物 | 亲爱的客栈第二季免费观看完整版 | 亚洲国产区男人本色在线观看欧美 | 青青青国产在线 | 97色伦在线观看 | 欧美草逼网 | 奇米激情| 午夜私人福利影院 | 精品麻豆国产 | 黄情视频 | 成人1234 | 日韩一区二区在线视频 | 久久精品中文騷妇女内射 | 97久久久亚洲综合久久88 | 吃大胸寡妇的奶 | 欧美日韩国产一区二区三区不卡 | 97视频久久| 铁牛tv 在线观看 | yellow视频免费观看播放 | 国产剧情一区二区三区 | 国产欧美一区二区三区免费看 | 精品午夜寂寞影院在线观看 | 亚州性夜夜射在线观看 | 精品一久久香蕉国产二月 | 歪歪私人影院成人毛片 | 男插女的下面免费视频夜色 | 久久精品无码一区二区日韩av | 国产一卡2卡3卡四卡国色天香 | 国产精品亚欧美一区二区三区 | www四虎影视 | 国产一级免费片 | 成人aaaa | 操老逼 | 91影视永久福利免费观看 | 天堂色 | 国模一区二区三区视频一 | 四虎免费永久观看 | 超91在线 | 三级全黄的视频 | 亚洲国产日韩欧美mv |