c++ 读写文件

2021-07-05 15:40:48 浏览数 (1)

本篇练习涉及到的知识点

  • 写文本文件
  • 4种方式读文本文件
  • 写二进制文件
  • 读二进制文件(例如写入自定义类的实例,和浮点数)
  • char* p = "abc";// valid in C, invalid in C
代码语言:javascript复制
#include<iostream>
#include<fstream>
#include<string>
using namespace std;


class Person
{
public:
    Person(char* name,int age,char* gender)
    {
        m_name = name;
        m_age = age;
        m_gender = gender;
    }
    Person()
    {
    }
    get_age()
    {
        return m_age;
    }
public:
    char* m_name;
    char* m_gender;
private:
int m_age;
};

void write_text();
void write_binary();
//以下4种方式均可写文本文件
void read1_text();
void read2_text();
void read3_text();
void read4_text();
void read_binary();


int main()
{
    write_text();
    cout<<endl;
    read1_text();//0.3s
    cout<<endl;
    read2_text();//0.3s
    cout<<endl;
    read3_text();//0.3s
    cout<<endl;
    read4_text();//0.3s
    cout<<endl;
    write_binary();
    cout<<endl;
    read_binary();


    return 0;
}


void write_text()
{
    ofstream ofs;
    ofs.open("E:\test1.txt", ios::out);
    for(int i = 0; i  <5;)
    {
        ofs<< i <<endl;
    }
    ofs<< "王八" <<endl;
    ofs.close();
    cout<<"文本文件写入成功!"<<endl;
}




void read1_text()
{
    ifstream ifs("E:\test1.txt", ios::in);//也在ifstream构造函数中指定参数,可省去open调用
    //ifs.open("E:\test1.txt", ios::in);
    if (!ifs.is_open())
    {
        cout<<"文本文件打开失败!"<<endl;
        return;
    }
    cout<<"文本文件打开成功!下面依次读取每一行"<<endl;


    char buffer[1024]= {0};
    while(ifs >> buffer)//循环读取每一行(不含'n'),存到字符数组
    {
        cout<< buffer <<endl;
    }
    ifs.close();
}




void read2_text()
{
    //也在ifstream构造函数中指定参数,即可省去open调用
    ifstream ifs("E:\test1.txt", ios::in);
    if (!ifs.is_open())
    {
        cout<<"文本文件打开失败!"<<endl;
        return;
    }
    cout<<"文本文件打开成功!下面依次读取每一行"<<endl;


    char buffer[1024]= {0};
    //循环读取每一行(不含'n',),存到字符数组(但是限定了最多读取多少字节)
    while(ifs.getline(buffer,sizeof(buffer)))
    {
        cout<< buffer<<endl;
    }
    ifs.close();
}




void read3_text()
{
    ifstream ifs("E:\test1.txt", ios::in);
    if (!ifs.is_open())
    {
        cout<<"文本文件打开失败!"<<endl;
        return;
    }
    cout<<"文本文件打开成功!下面依次读取每一行"<<endl;


    string buffer;
    //循环读取每一行(不含'n',),存到字符数组(但是限定了最多读取多少字节)
    while(getline(ifs, buffer))
    {
        cout<< buffer<<endl;
    }
    ifs.close();
}


void read4_text(void)
{
    ifstream ifs("E:\test1.txt", ios::in);
    if (!ifs.is_open())
    {
        cout<<"文本文件打开失败!"<<endl;
        return;
    }
    cout<<"文本文件打开成功!下面依次读取每一行"<<endl;


    char c;
    //循环读取每个字符(包含'n',),赋值给字符变量c
    while((c = ifs.get())!=EOF)
    {
        cout<< c;
    }
    ifs.close();
}


void write_binary()//写二进制文件
{
    ofstream ofs;
    ofs.open("E:\test2.bin", ios::out | ios::binary);
    Person p1 = {(char*)"法外狂徒张三", 28, (char*)"男"};//字符数组常量强制转换成字符指针
    ofs.write((const char*)&p1, sizeof(Person));//写入对象


    float x1  = 0.999f;
    ofs.write((const char*)&x1, sizeof(float));
    ofs.close();
    cout<<"二进制文件写入成功!"<<endl;
}


void read_binary()//读二进制文件
{
    ifstream ifs("E:\test2.bin", ios::in | ios::binary);
    if (!ifs.is_open())
    {
        cout<<"二进制文件打开失败!"<<endl;
        return;
    }
    cout<<"二进制文件打开成功!"<<endl;


    Person p;
    //读二进制数据,注意须强制类型转换 (char*)
    ifs.read((char*) &p, sizeof(Person));


    float x = 0;
    ifs.read((char*) &x, sizeof(float));
    ifs.close();


    cout<< "p.name: "<< p.m_name << endl;
    cout<< "p.gender: "<< p.m_gender << endl;
    cout<< "p.age: "<< p.get_age()<< endl;
    cout<< "x: "<< x << endl;


    char name[1024] ={0};
    int i;
    for(; i< int(sizeof(name)); i  )//将 字符指针指向的值复制到字符数组中
    {
        name[i] = *(p.m_name   );
        if(name[i] == '') break;
    }
    cout<<name<<" 共"<<i<<"字节";


    char* ps1 = "abc";// valid in C, invalid in C  
    char* ps2 = (char*)"abc";// OK .注意括号!
    char const *cp = "abc";// OK
}

0 人点赞