網站首頁 編程語言 正文
C++定義了I/O標準類庫,這些每個類都稱為流/流類,用以完成某方面的功能
C++ 系統實現了一個龐大的類庫,其中 ios 為基類,其他類都是直接或間接派生自 ios 類
1.標準輸出輸入流-控制臺流(iostream類)?
cin實際上是一個標準輸入流對象(類對象),常用的方式有,cin>>(cin.operator>>()), cin.get(), cin.getline()(用法:cin.getline(char s[], int nLength))
或者在std命名空間下,有一個單獨的getline()函數,但是該函數時使用string對象作為參數的,即:getline(cin, str)
cout是標準輸出流對象(類對象),cout<<
cout cin printf scanf(格式化輸入輸出)比較
總結:c++中盡量用cin和cout
//c_str()就是把string類對象轉換成和c兼容的char*類型。
//這是為了與c語言兼容,在c語言中沒有string類型
//故必須通過string類對象的成員函數c_str()把string 對象轉換成c中的字符串樣式
//例如
string a = "hellofjifj";
printf("%s\n", a.c_str());
//printf("%s\n", a);//這樣會有問題
2. 文件流(I/O操作)
在文件流中提供了三個派生類對文件數據進行操作(注意這里是類,不像控制臺提供的是類對象)
- ofstream:輸出,即寫文件,由ostream引申而來
- ifstream:輸入,即讀取文件,由istream引申而來
- fstream :輸入輸出,同時讀寫操作,由iostream引申而來
文件的類型:文本文件 和 二進制文件
文件讀寫的步驟:
- 包含的頭文件:#include <fstream>
- 創建流
- 打開文件(文件和流關聯)
- 讀寫 (寫操作:<<,put( ),write( )讀操作:>> , get( ),getline( ), read( ))
- 關閉文件:把緩沖區數據完整地寫入文件, 添加文件結束標志, 切斷流對象和外部文件的連接
文本文件
使用<< >> 進行讀寫
<< 能實現以行為單位寫入文件
>> 不能一行為單位讀入內存,而是以單詞為單位。總是以空格、Tab、回車結束
ofstream OpenFile("file.txt");
if (OpenFile.fail())
{
cout<<"打開文件錯誤!"<<endl;
exit(0);
}
OpenFile << "abc def ghi";//把內容寫入file文件
OpenFile.close();
const int len=20;
char str[len];
ifstream OpenFile("file.txt");
if (OpenFile.fail())
{
cout<<"打開文件錯誤!"<<endl;
exit(0);
}
OpenFile >> str;
cout << str << endl;
OpenFile.close(); //str的內容為abc,而不是abc def ghi(見空格停止)
getline()讀取一行
getline():以行為單位讀入內存,能一次讀入一行
函數原型:istream &getline( char *buffer, streamsize num );
getline( )函數用于從文件讀取num個字符到buffer(內存)中,直到下列情況發生時,讀取結束:
- num個字符已經讀入
- 碰到一個換行標志
- 碰到一個EOF
const int len=20;
char str[len];
ifstream OpenFile("file.txt");
if (OpenFile.fail())
{
cout<<"打開文件錯誤!"<<endl;
exit(0);
}
OpenFile.getline(str,20);
cout << str << endl;
OpenFile.close();//運行結果:str的內容為abc def ghi (一直把一行讀完)
get() put()進行單個字符讀寫
ostream& put (char c);
函數功能:使用 put( )函數,向文件中寫入字符
char ch='1';
ofstream OpenFile("file.txt");
if (OpenFile.fail())
{
cout<<"打開文件錯誤!"<<endl;
exit(0);
}
OpenFile.put(ch); // 把字符1寫入文件
OpenFile.close();
istream& get (char& c);
函數功能:使用 get( )函數,從文件中讀取字符
char ch;
ifstream OpenFile("file.txt");
if (OpenFile.fail())
{
cout<<"打開文件錯誤!"<<endl;
exit(0);
}
OpenFile.get(ch);
cout << ch; //把字符1從文件中讀到ch(內存)中
OpenFile.close();
二進制文件讀寫
get() put()進行單個字節讀寫
ofstream &put(char ch)
在內存中寫入一個字節到文件
char ch='a';
ofstream OpenFile("file.txt",ios::binary);
//以二進制的方式處理,在打開時要用 ios::binary 顯式聲明
if (OpenFile.fail())
{
cout<<"打開文件錯誤!"<<endl;
exit(0);
}
OpenFile.put(ch);
OpenFile.close();
ifstream &get(char ch)
在文件中讀取一個字節到內存
char ch;
ifstream OpenFile("file.txt",ios::binary);
if (OpenFile.fail())
{
cout<<"打開文件錯誤!"<<endl;
exit(0);
}
OpenFile.get(ch); // 從文件中讀取一個字符
cout << ch;
OpenFile.close();
read() write()多個字節讀寫
ostream & ostream :: write ( char * buf , int n ) ;
功能:把buf指向的內容取n個字節寫入文件
參數說明:buf表示要寫入內存的地址,傳參時要取地址。n表示要讀入字節的長度
注意:
- 該函數遇到空字符時并不停止,因而能夠寫入完整的類結構
- 第一個參數一個char型指針(指向內存數據的起始地址),與對象結合使用的時候,要在對象地址之前要char做強制類型轉換
char ch[12]="12 3 456 78";
ofstream OpenFile("file.txt");
if (OpenFile.fail())
{
cout<<"打開文件錯誤!"<<endl;
exit(0);
}
OpenFile.write(ch, 12);
OpenFile.close();
istream & read ( char * buf , int n ) ;
功能:從文件中提取 n 個字節數據,寫入buf指向的地方中
char ch[12];
ifstream OpenFile("file.txt");
if (OpenFile.fail())
{
cout<<"打開文件錯誤!"<<endl;
exit(0);
}
OpenFile.read(ch,12);
cout << ch; //運行結果:數組ch的內容為12 3 456 78
OpenFile.close();
注意事項
(1)程序不再使用文件時,為什么要關閉文件?
因為:
- 文件緩沖區是一塊小的內存空間.
- 操作系統限制同時打開的文件數量
注意:close ( ) 函數關閉文件,但流對象仍然存在。
(2)文件的默認打開方式為文本文件,要是想以二進制的方式處理,在打開時要用 ios::binary 顯式聲明。
(3)針對文本文件操作時,get函數和>>的區別:
- 在讀取數據時,get函數包括空白字符(遇空白字符不停止讀取)
- >>在默認情況下拒絕接受空白字符(遇到空白符停止讀取)
(4)文本文件的讀寫常使用的方法:使用<<寫入文件,使用getline 和 >> 讀到內存
二進制文件的讀寫常使用的方法:使用istream 類的成員函數read 和write 來實現,
3.字符串輸入輸出流(sstream)
其相應的派生類有istringstream類、ostringstream類、iostringstream類
sprintf sscanf 和 stringstream的使用
其實是整型和字符創類型的相互轉化:序列化(轉成字符串)和反序列化(將字符串中數據提取處理)
C當中用法:sprintf和sscanf函數
C++中用法:字符串流
struct ServerInfo//要讀些的信息類
{
char _ip[20];
int _port;
};
//C當中用法
//將info中數據轉成字符串并存入buff中
ServerInfo info = { "192.0.0.1", 8000 };
char buff[128];//buff的大小不好確定
sprintf_s(buff, "%s %d", info._ip, info._port);// 序列化,轉成字符串存入buff中
//將buff中數據格式化輸出到rinfo中
ServerInfo rinfo;
sscanf(buff, "%s%d", rinfo._ip, &rinfo._port);// 反序列化,將字符串buff中數據轉成規定格式
//空格間隔開了
//C++用法
//將info中數據轉成字符串并存到buff中
ServerInfo info = { "192.0.0.1", 8000 };
stringstream ssm;//字符串流對象
ssm << info._ip <<" "<<info._port;//轉成字符串
string buff = ssm.str();//查看字符串
//從字符串提取至rinfo
stringstream ssm;
ssm.str("127.0.0.1 90");//這兩行直接按照下面的寫法也行
//stringstream ssm("127.0.0.1 90");
ServerInfo rinfo;
ssm >> rinfo._ip >> rinfo._port;
補充內容:C/C++中int和字符串類型的轉換
string轉int
- atoi(c庫函數、ascll to integer)
- strtol(c庫函數,字符串轉成長整型)
- stoi(stoi是C++的函數,頭文件:#include < string >)
int轉string
- itoa(c庫函數)
- to_string(C++11的函數,可以適應8種類型的重載,將其轉換為string,頭文件:#include < string >)
多功能轉換
stringstream
原文鏈接:https://blog.csdn.net/m0_60416282/article/details/125540129
相關推薦
- 2022-07-09 systemd開機啟動和關機回調腳本
- 2021-12-14 解決Oracle?11g?導出數據報?“ORA-01455:?轉換列溢出整數數據類型”的問題_ora
- 2024-03-06 Springboot實現緩存預熱
- 2022-07-02 在React中使用axios發送請求
- 2023-01-03 Android?自定義Livedata使用示例解析_Android
- 2022-11-27 C語言移除元素的三種思路講解_C 語言
- 2023-09-17 POM文件中使用<exclusions>解決jar沖突問題
- 2023-05-06 Python執行ping操作的簡單方法_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支