0
点赞
收藏
分享

微信扫一扫

c++ 文本操作多种方法

雷亚荣 2023-01-04 阅读 12


文章目录

  • ​​1、二进制文件流​​
  • ​​2、直接文本方式​​

1、二进制文件流

#include <iostream>
#include <fstream>
#include <vector>
#define p "/home/barry/CLionProjects/temp/t.txt"
#define path "/home/barry/CLionProjects/temp/file1.txt"
#define path2 "/home/barry/CLionProjects/temp/file2.dat"

using namespace std;

template<class T>
void serialize(ofstream& stream, T&x){
stream.write((char*)&x, sizeof(T));
}

template <class T> // 基本类型-读
void de_serialize(ifstream& stream, T& x) //读
{
stream.read((char*)&x, sizeof(T));
}

void serialize(ofstream& stream, string& str)
{
int len = str.length();
stream.write((char*)&len, sizeof(int));
stream.write(str.c_str(), len); //str.c_str()返回该字符串的首字符地址
}

//std::string 读
void de_serialize(ifstream& stream, string& str)
{
int len;
stream.read((char*)&len, sizeof(int));//读取字符串长度值
str.resize(len); //调整字符串str的长度为len
char x;
for (int i = 0; i < len; i++)
{
stream.read(&x, sizeof(char));
str[i] = x;
}
}


class Student
{
string stu_no;
string name;
int age;
public:
Student(){};
Student(string _st, string _na,int _ag){
stu_no = _st; name = _na; age = _ag;
}
void display(){
cout << stu_no << " " << name << " " << age << endl;
}

string get_stu_no(){ return stu_no; }
string get_name(){ return name; }
int get_age(){ return age; }
friend void save(ofstream&stream, Student& student);
friend void load(ifstream &stream, Student &student);
};

void save(ofstream&stream, Student& student){
serialize(stream,student.stu_no);
serialize(stream,student.name);
serialize(stream, student.age);
}

void load(ifstream &stream, Student &student){
de_serialize(stream,student.stu_no);
de_serialize(stream,student.name);
de_serialize(stream,student.age);

}


int main(){
Student stu[3] = { Student("201101", "liming", 18), Student("201102", "zhangting", 18), Student("201103", "xiaobin", 19) };

//写入文件
ofstream out;
out.open(path2,ios::binary|ios::app);
if(!out){
cout <<"error";
abort();
}
for(int i = 0; i < 3; i++){
save(out,stu[i]);
}
out.close();


//读取文件
Student stu2[3];
ifstream in;
in.open(path2,ios::binary);
if(!in){
cout << "error";
abort();
}

Student s;
while (in.peek() !=EOF){
load(in,s);
s.display();
}
}

c++ 文本操作多种方法_#include

2、直接文本方式

#include <iostream>
#include <fstream>
#include <vector>
#define p "/home/barry/CLionProjects/temp/t.txt"
#define path "/home/barry/CLionProjects/temp/file1.txt"

using namespace std;


//字符串分割函数
vector<string> split(string s,char c){
vector<string> re;
for(int i = 0; i < s.size(); i++){
string t="";
while(i<s.size()&&s[i]!=c){
t+=s[i];
i++;
}
re.push_back(t);
}
return re;
}

class Student
{
string stu_no;
string name;
int age;
public:
Student(){};
Student(string _st, string _na,int _ag){
stu_no = _st; name = _na; age = _ag;
}
void display(){
cout << stu_no << " " << name << " " << age << endl;
}

string get_stu_no(){ return stu_no; }
string get_name(){ return name; }
int get_age(){ return age; }
};

void line_write(string s){
ofstream outfile;
outfile.open(path,ios::app);
cout << "Writing to the file" << endl;
outfile<<s.data();
outfile.close();
}

void line_read(){
string G[1000];
int k = 0;

char buffer[256];
fstream out;
out.open(path,ios::in);
while(!out.eof())
{

out.getline(buffer,256,'\n');//getline(char *,int,char) 表示该行字符达到256个或遇到换行就结束
G[k++] = buffer;
// cout<<buffer<<endl;
}


out.close();
vector<string> re[1000];
for(int i = 0; i < k; i++){
re[i] = split(G[i],' ');
}

for(int i = 0; i < k; i++){
Student t(re[i][0],re[i][1],stoi(re[i][2]));
t.display();
}
}
int main(){
Student stu[3] = { Student("201101", "liming", 18), Student("201102", "zhangting", 18), Student("201103", "xiaobin", 19) };

//写入文本
for (int i = 0; i < 3; i++){
string str = "";
str = stu[i].get_stu_no()+" "+stu[i].get_name()+" "+ to_string(stu[i].get_age())+"\n";
line_write(str);
}
//读取文本
line_read();
}

结果

c++ 文本操作多种方法_c++_02

使用了fstream与ofstream对文件file1进行读写操作。在写入文件时,student对象的不同属性之间使用空格隔开写入文件,file1文件内容如下所示

c++ 文本操作多种方法_i++_03

在读取时,按行读取,并且使用vector split(string s, char c);对每行字符串进行按空格分割,得到的数据进行Student对象的生命,再调用student->display()函数展示数据即可。


举报

相关推荐

0 条评论