C++流和文件操作
C++中流的概念
C++中輸入輸出操作使用流來完成,將輸出輸入的操作稱爲流類,實現流類的庫爲流類庫
流類庫頭文件
#include <iostream>
C++常用流類派生關係:
-
ios
抽象的基類
-
istream
輸入的流類。cin爲該流類的對象
-
ostream
輸出的流類。cout爲該流類的對象
-
ifstream
讀取文件數據流類
-
ofstream
向文件寫入數據流類
-
fstream
向文件讀寫數據的流類
iostream頭文件中定義的四個標準流對象
- cin——標準輸入流,從輸入設備中讀取數據,可重定向爲文件寫入數據
- cout——標準輸出流,在顯示設備顯示數據,可重定向爲向文件讀出數據
- cerr——標準錯誤輸出流,向顯示設備輸出出錯信息
- clog——標準錯誤輸出流,向顯示設備輸出出錯信息
文件操作
頭文件
#include <fstream>
文件打開方式
打開方式 | 說明 |
---|---|
ios::in | 讀文件方式打開文件,文件不存在打開失敗 |
ios::out | 寫文件方式打開文件,文件不存在久創建了再打開 |
ios::ate | 打開文件後,指針指向文件尾 |
ios::app | 以追加方式打開寫文件 |
ios::trunc | 文件存在先刪除再創建 |
ios::binary | 二進制方式 |
寫文件
void write_file(void)
{
fstream ifs; //1、創建流對象
ifs.open("person.txt", ios::out); //2、打開文件
ifs << "name : guangjieMVP" << endl; //3、向文件中寫入數據,使用<<運算符寫入
ifs << "id : 5201314" << endl; //4、向文件中寫入數據,使用<<運算符寫入
ifs.close(); //5、關閉文件
}
int main(void)
{
write_file();
return 0;
}
函數名 | 參數1 | 參數2 |
---|---|---|
open | 文件路徑,如D:\test\person.txt,不寫路徑默認當前目錄 | 文件打開方式 |
讀文件
四種方式:
//方式1,使用運算法>>讀出數據到buf
void read_file1(void)
{
fstream ifs; //1、創建流對象
ifs.open("person.txt", ios::in); //2、打開文件
if(!ifs.is_open()) //3、判斷文件是否打開成功
{
cout << "文件打開失敗!" << endl;
return;
}
char buf[1024];
while(ifs >> buf) //4、使用運算符>>讀出數據到buf
{
cout << buf << endl;
}
ifs.close();
}
void read_file2(void)
{
fstream ifs; //1、創建流對象
ifs.open("person.txt", ios::in); //2、打開文件
if(!ifs.is_open()) //3、判斷文件是否打開成功
{
cout << "文件打開失敗!" << endl;
return;
}
char buf[1024];
while(ifs.getline(buf, sizeof(buf))) //4、使用流對象中的讀出函數從文件中讀出數據放到buf中
{
cout << buf << endl;
}
ifs.close(); //5、關閉文件
}
void read_file3(void)
{
fstream ifs; //1、創建流對象
ifs.open("person.txt", ios::in); //2、打開文件
if(!ifs.is_open()) //3、判斷文件是否打開成功
{
cout << "文件打開失敗!" << endl;
return;
}
string buf;
while(getline(ifs, buf)) //4、從文件中讀出數據放到buf中
{
cout << buf << endl;
}
ifs.close(); //5、關閉文件
}
void read_file4(void)
{
fstream ifs; //1、創建流對象
ifs.open("person.txt", ios::in); //2、打開文件
if(!ifs.is_open()) //3、判斷文件是否打開成功
{
cout << "文件打開失敗!" << endl;
return;
}
char c;
while((c = ifs.get()) != EOF) //4、使用流對象ifs.get()函數從文件one by one byte方式放到c中
{
cout << c;
}
ifs.close(); //5、關閉文件
}
int main(void)
{
write_file();
// read_file1();
// read_file2();
// read_file3();
read_file4();
return 0;
}
二進制方式讀寫文件
#include <fstream>
#include <string>
#include <iostream>
using namespace std;
class Person
{
public:
int age;
string name;
public:
Person(string name = "zhangsan", int age = 20)
{
this->age = age;
this->name = name;
}
};
//二進制方式讀寫文件
void binary_write_read(void)
{
Person person("guangjieMVP");
Person person1;
//寫文件
fstream ofs("test.txt", ios::out | ios::binary); //1、創建輸出流對象
ofs.write((char *)&person, sizeof(person)); //2、寫入數據
ofs.close(); //3、關閉文件
//讀文件
fstream ifs("test.txt", ios::in | ios::binary); //1、創建輸入流對象
if(!ifs.is_open())
{
cout << "打開文件失敗!" << endl;
}
ifs.read((char *)&person1, sizeof(person1)); //2、讀出文件數據
cout << "Person name : " << person1.name << endl;
cout << "Person age : " << person1.age << endl;
ifs.close(); //3、關閉文件
}
int main(void)
{
binary_write_read();
return 0;
}