一、文件寫入的基本流程
在進行文件寫入操作時,首先需要創建一個ofstream對象,並打開對應的文件。打開文件的方式有兩種:覆蓋原有文件內容和追加文件內容。當我們創建好ofstream對象並打開對應文件後,就可以向文件中寫入數據了。
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
//創建ofstream對象-file1.txt將在當前程序所在目錄下創建
ofstream ofs1("file1.txt",ofstream::out);
if(!ofs1)
{
cout<<"create file failed"<<endl;
return -1;
}
//覆蓋模式寫文件
ofs1<<"Hello world"<<endl;
ofs1.close();
//打印file1.txt文件內容
char buf[64] = {0};
ifstream ifs1("file1.txt",ifstream::in);
while(ifs1.getline(buf,64))
{
cout<<buf<<endl;
}
ifs1.close();
//創建ofstream對象-file2.txt將在當前程序所在目錄下創建
ofstream ofs2("file2.txt",ofstream::out|ofstream::app);
if(!ofs2)
{
cout<<"create file failed"<<endl;
return -1;
}
//追加模式寫文件
ofs2<<"Append mode"<<endl;
ofs2.close();
//打印file2.txt文件內容
ifstream ifs2("file2.txt",ifstream::in);
while(ifs2.getline(buf,64))
{
cout<<buf<<endl;
}
ifs2.close();
return 0;
}
二、從標準輸入流中讀入並寫入到文件中
我們可以通過從標準輸入流中讀取用戶輸入的數據,然後寫入到文件中。這種方式可以方便地實現文件內容的自定義輸入。
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ofstream ofs("file.txt",ofstream::out);
if(!ofs)
{
cout<<"Create file failed"<<endl;
return -1;
}
//從標準輸入流中讀取並寫入到文件中
string str;
while(getline(cin,str))
{
ofs<<str<<endl;
}
ofs.close();
return 0;
}
三、結構體數組寫入到文件中
在實際應用中,我們經常會將結構體數組中的元素寫入到文件中。這需要使用流提取運算符和流插入運算符來實現。
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
//定義結構體
struct Student
{
string name;
int age;
float score;
};
int main()
{
Student stu[2] = { {"Tom",18,80.0},{"Jerry",20,85.5} };
ofstream ofs("file.txt",ofstream::out);
if(!ofs)
{
cout<<"Create file failed"<<endl;
return -1;
}
//將結構體數組寫入到文件中
for(int i=0;i<2;++i)
{
ofs<<stu[i].name<<", "<<stu[i].age<<", "<<stu[i].score<<endl;
}
ofs.close();
return 0;
}
四、二進制文件寫入
除了文本文件,我們還可以將數據以二進制的方式寫入到文件中。這需要使用ofstream類對象的write方法,並傳入待寫入數據的地址和長度。
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
double scores[] = {80.0,85.5,90.0};
ofstream ofs("file.dat",ofstream::out|ofstream::binary);
if(!ofs)
{
cout<<"Open file failed"<<endl;
return -1;
}
//將數組寫入二進制文件
ofs.write(reinterpret_cast<const char*>(scores),sizeof(scores));
ofs.close();
//讀取二進制文件中的數據
ifstream ifs("file.dat",ifstream::in|ifstream::binary);
double score;
while(ifs.read(reinterpret_cast<char*>(&score),sizeof(score)))
{
cout<<score<<"\t";
}
ifs.close();
return 0;
}
五、結構體寫入二進制文件
我們可以將結構體作為一個整體寫入到二進制文件中,這需要使用write方法,並傳入待寫入結構體的地址和長度。
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
//定義結構體
struct Student
{
string name;
int age;
float score;
};
int main()
{
Student stu[2] = {{"Tom",18,80.0},{"Jerry",20,85.5}};
ofstream ofs("file.dat",ofstream::out|ofstream::binary);
if(!ofs)
{
cout<<"Open file failed"<<endl;
return -1;
}
//將結構體數組寫入到二進制文件
ofs.write(reinterpret_cast<const char*>(&stu),sizeof(stu));
ofs.close();
//讀取二進制文件中的數據
ifstream ifs("file.dat",ifstream::in|ifstream::binary);
Student stu_new[2];
ifs.read(reinterpret_cast<char*>(&stu_new),sizeof(stu_new));
ifs.close();
//輸出讀取的結果
for(int i=0;i<2;++i)
{
cout<<stu_new[i].name<<":"<<stu_new[i].age<<":"<<stu_new[i].score<<endl;
}
return 0;
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/160801.html