一、文件的基礎操作
文件是一種存儲數據的容器,通常用於長期保存程序的重要數據。C++標準庫提供了一組簡單而易用的函數來實現文件的讀寫操作。文件操作主要包括打開文件、讀取文件、寫入文件和關閉文件4個步驟。
1.文件打開
在進行文件讀寫操作前,必須先打開文件。C++標準庫提供了fstream頭文件,其中的fstream類就具有打開文件的功能。打開文件的方式有兩類:二進制模式和文本模式(默認為文本模式),可以通過打開方式的第二個參數來指定。
#include <fstream>
using namespace std;
int main()
{
fstream file; // 聲明一個fstream對象
file.open("test.txt", ios::in | ios::out | ios::trunc);
// 打開文件test.txt,若文件不存在則創建新文件,同時可讀寫文件
if (!file) // 判斷文件是否打開成功
{
cout << "open failed." << endl;
return 0;
}
return 0;
}
2.文件讀取
文件讀取有兩種方法:字符讀取和行讀取。其中,字符讀取使用get函數,行讀取使用getline函數。
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ifstream file("test.txt"); // 打開文件test.txt
string line;
while (getline(file, line)) // 按行讀取文件
{
cout << line << endl;
}
char ch;
while (file.get(ch)) // 按字符讀取文件
{
cout << ch;
}
file.close(); // 關閉文件
return 0;
}
3.文件寫入
文件寫入使用put函數和write函數。put函數用於像文件中寫入一個字符,write函數將一段數據寫入到文件中。
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream file("test.txt");
file.put('A'); // 向文件寫入字符A
char str[] = "Hello World!";
file.write(str, sizeof(str)); // 向文件寫入字符串Hello World!
file.close();
return 0;
}
4.文件關閉
文件關閉使用close函數,調用close函數後,文件被釋放並從內存中刪除。
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream file("test.txt");
file << "Hello World!";
file.close(); // 關閉文件
return 0;
}
二、文件的其他操作
除了基本的讀寫操作外,C++標準庫還提供了一些文件的其他操作,包括文件長度、文件指針位置、文件複製、文件重命名等。
1.文件長度
獲取文件長度使用tellg函數和seekg函數,tellg函數返回當前文件讀指針的位置,seekg函數用於設置文件讀指針的位置。
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream file("test.txt");
file.seekg(0, ios::end); // 將讀指針設置到文件末尾
int length = file.tellg(); // 獲取文件長度
cout << length << endl;
file.close();
return 0;
}
2.文件指針位置
獲取文件指針位置使用tellp函數和seekp函數,同樣是用來操作文件讀寫指針的。
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream file("test.txt");
file << "Hello World!";
file.seekp(0, ios::beg); // 將寫指針設置到文件開始位置
file << "h"; // 寫入新的字符
file.close();
return 0;
}
3.文件複製
文件複製使用fstream和streambuf兩個類,其中fstream用來打開複製的文件,streambuf用來讀取原文件並寫入新文件。
#include <iostream>
#include <fstream>
#include <streambuf>
using namespace std;
int main()
{
fstream file1("test1.txt", ios::in | ios::binary);
fstream file2("test2.txt", ios::out | ios::binary);
file2 << file1.rdbuf(); // 將file1的讀取緩衝區寫入file2
file1.close();
file2.close();
return 0;
}
4.文件重命名
文件重命名使用rename函數,可以將一個文件重命名為另一個文件名。
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
rename("test1.txt", "test2.txt"); // 將test1.txt重命名為test2.txt
return 0;
}
三、代碼示例
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
// 文件打開
fstream file("test.txt", ios::in | ios::out | ios::trunc);
if (!file)
{
cout << "open failed." << endl;
return 0;
}
// 文件讀取
string line;
while (getline(file, line))
{
cout << line << endl;
}
char ch;
while (file.get(ch))
{
cout << ch;
}
// 文件寫入
file.put('A');
char str[] = "Hello World!";
file.write(str, sizeof(str));
// 文件長度
file.seekg(0, ios::end);
int length = file.tellg();
cout << length << endl;
// 文件指針位置
file.seekp(0, ios::beg);
file << "h";
// 文件複製
fstream file1("test1.txt", ios::in | ios::binary);
fstream file2("test2.txt", ios::out | ios::binary);
file2 << file1.rdbuf();
file1.close();
file2.close();
// 文件重命名
rename("test1.txt", "test2.txt");
// 文件關閉
file.close();
return 0;
}
原創文章,作者:QUYHA,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/370387.html