C++文本文件,二进制文件,write(),read(),map容器,seekg(),seekp(),tellg(),tellp()函数

#include<fstream>

 一:文本文件

1.ofstream和ifstream数据类型 文本文件

ofstream f1;//该数据类型表示输出文件流,用于创建并向文件中写入信息
ifstream f2;//该数据类型表示输入文件流,用于从文件读取信息 
//f1,f2表示变量

2.从程序向文件写入字符串

void Write()
{
    string file;//文件名
    string s;//要写入文件的字符串
    f1.open(file.c_str(),ios::out);
//.c_str()是因为文件名file被声明为string,而f1.open()参数要char[]类型,所以要用.c_str()转换一下
//ios::out是定义文件被打开的模式,"out"指写入模式,这种方式会将原有的内容覆盖
    f1.open(file.c_str(),ios::app|ios::out);//这种模式会在原来的内容上追加
    f1<<s<<endl;
    f1.close();
}

3.由程序从文件中读内容

void Read()
{
    f2.open(file.c_str(),ios::in);//定义文件模式为读
    while(1)
    {
        getline(f2,s);//一行一行读取文件内容
        if(!f2.eof())  //fstream.eof()如果读到文件结束符了返回真,否则返回假
        {
            cout<<s<<endl;
        }
        else break;
     }
    f2>>n;
//假如文件中有一个数字,想要在程序中显示这个数字的平方,一定要先输入流将数字输入到文件中,见上
    cout<<n*n<<endl;
    f2.close();
}

 4.实例:英汉词典

class dictionary
{
private:
    string file;
    ifstream f1;
public:
dictionary(string ff)
{
    file=ff;
}
string word(string s)
{
    string temp;
    string::size_type n1;
    n1=s.find(" ",0);
    temp=s.substr(0,n1);
    return temp;
}
void Find()
{
    string temp,s;
    cout<<"input";cin>>temp;
    f1.open(file.c_str(),ios::in);
    if(!f1)//如果f1文件没问题可以读写,f1返回1;
    {
        cout<<"file error"<<endl;
        exit(0);
    }
    else
    {
        while(1)
        {
            getline(f1,s);
            if(!f1.eof())
            {
                if(temp==word(s))
                {
                    cout<<s<<endl;
                }
            }
            else break;
        }
    }
}
};
int main()
{
    dictionary t("英汉词典.txt");
    t.Find();
    return 0;
}

在介绍二进制文件之前,简单说一下二进制文件和文本文件的区别: 

二进制文件读写速度非常快,因为数据在内存中是以二进制存储的,二进制文件中存储的数据也是二进制形式的,所以向二进制文件写入数据的时候,内存中的数据直接不需转化放入二进制文件中。而文本文件中的数据是以字符串的形式存储的,数据从内存放入文本文件需要经过一定的编码方式转化(Unicode或ASCII码)成字符串。在读出时,二进制文件的数据不需要解码就可以直接放在内存中,但是这些二进制串我们看不懂,而文本文件需要将字符串解码,再放入内存中,所以效率稍低。我们常用的记事本是文本文件,当从内存中存入非字符串时会乱码,因为字符串在内存中是以编码方式排列好的,而数字则是以二进制形式存储,所以被解码之后会变成我们不知道的码(乱码)

二.二进制文件

fstream f;//二进制文件读写只需要一个变量

1.从程序向二进制文件中写内容

void Write()
{
    string::size_type len;
    len=s.size();
    f.open(file,ios::in|ios::out|ios::binary);//如果是类的话以上操作可以放在字符串中
//ios模式把写和读模式放在一起,还加了一个ios::binary这个是规定Windows的二进制文件换行符为一个\n
    f.write((char*)s.c_str(),len);//第一个参数是字符指针类型,第二个是要写的字符串长度
}

 write()函数的返回值通常是len,如果是-1,说明磁盘空间满了或者文件大小超出限制。

第一个参数是写入的来源,第二个参数是要写入的长度

2.从二进制文件向程序读内容并在程序中输出

void Read()
{
    string::size_type len;
    string s1;
    f.seekg(0,ios::end);//将指针移动到文件尾
    len=f.tellg();//得到当前指针位置(读取流)
    s1.resize(len);//resize第一个参数(此参数)是重新设置的内容长度,第二个参数是初始化值
    f.seekg(0,ios::beg);
    f.read((char*)s1.c_str(),len);
    cout<<s1<<endl;
}

read()函数返回值是读取的字节数,如果是-1说明出现问题; 

3.实例:英汉词典(字符串,容器,文件结合)

class test
{
private:
    string s,s1,s2;
    string::size_type n,n1,n2,n3,pos;
    map<string,string>a;
    map<string,string>::iterator p;
    fstream f;
public:
test()
{
    f.open("英汉词典.txt",ios::in|ios::out|ios::binary);//此txt文件自己预先存到对应编译器路径中
    if(!f)
    {
        cout<<"file error"<<endl;
        exit(0);
    }
    f.seekg(0,ios::end);
    n=f.tellg();
    s.resize(n);
    f.seekg(0,ios::beg);
    f.read((char*)s.c_str(),n);
    pos=0;
    while(1)
    {
        if(s.find("\n",pos)!=string::npos)
        {
            n1=s.find("\n",pos);//寻找一行的结尾
        }
        else
        {
            n1=s.size();//如果到最后一行了没有换行符,避免查找不到最后一行
        }
        s1=s.substr(pos,n1-pos);//行串
        n2=s1.find(" ",0);
        s2=s1.substr(0,n2);
        a.insert(pair<string,string>(s2,s1));//键值存入单词,second存入行串(释义);
        pos=n1+1;
        if(pos>=s.size())//判断是否读完
        break;
    }
}
void browse()//自己浏览调试
{
    for(p=a.begin();p!=a.end();++p)
    {
        cout<<p->first<<p->second<<endl;
    }
}
void Find()
{
    string temp;
    cout<<"input the word you want to check:"<<endl;cin>>temp;
    p=a.find(temp);
    if(p!=a.end())
    {
        cout<<p->second<<endl;
    }
}
~test()//析构函数关闭文件
{
    f.close();
}
};
int main()
{
    test t;
    t.Find();
    return 0;
}

4.文件加解密

class test
{
private:
    string::size_type n,n1,n2,n3;
    string s,s1,s2;
    int i;
    fstream f;
public:
test()
{
    f.open("test.doc",ios::in|ios::out|ios::binary);
    if(!f)
    {
        cout<<"file error"<<endl;
        exit(0);
    }
    f.seekg(0,ios::end);
    n=f.tellg();
    s.resize(n);
    f.seekg(0,ios::beg);
    f.read((char*)s.c_str(),n);
}
void Reverse()//字符串倒序
{
    string temp;
    temp=s;
    for(i=0;i<n;++i)
    {
        temp[i]=s[n-i-1];
    }
    s=temp;
}
void turn()//字符串前n个字符放到后面
{
    int n=100;
    s1=s.substr(0,100);
    s2=s.substr(100,n-100);
    s=s2+s1;
}
void Write()
{
    f.seekg(0,ios::beg);
    f.write((char*)s.c_str(),n);
}
~test()
{
    f.close();
}
};
int main()
{
    test t;
    t.Reverse();//t.turn();
    t.Write();
    return 0;
}


  • 6
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是将Python代码转换为C++的代码: ```cpp #include <fstream> #include <vector> #include <cstdint> #include <cstring> std::vector<float> read_lvx_file(const std::string& file_path) { std::vector<float> point_cloud_data; std::ifstream file(file_path, std::ios::binary); if (!file) { throw std::runtime_error("Failed to open file: " + file_path); } // 读取文件头 char file_header[8]; file.read(file_header, 8); uint32_t data_size = *reinterpret_cast<uint32_t*>(file_header + 4); // 读取点云数据 while (true) { char data_header[16]; file.read(data_header, 16); if (!file) { break; } uint32_t point_count = *reinterpret_cast<uint32_t*>(data_header + 12); std::vector<float> point_data(point_count * 7); file.read(reinterpret_cast<char*>(point_data.data()), point_count * 16); // 将点云数据添加到point_cloud_data中 point_cloud_data.insert(point_cloud_data.end(), point_data.begin(), point_data.end()); } return point_cloud_data; } int main() { std::string file_path = "path/to/pointcloud.lvx"; std::vector<float> point_cloud_data = read_lvx_file(file_path); return 0; } ``` 在C++中,我们需要使用fstream库中的ifstream类来打开文件,并使用binary模式读取文件内容。此外,我们还需要使用vector容器来存储点云数据。在读取文件时,我们先读取文件头部8个字节的数据,然后解析其中的数据大小信息。接着循环读取数据块的头部,每个数据块包含点云数据的数量和数据本身。循环中使用reinterpret_cast将读取的字节流转换为对应的数据类型,并将点云数据添加到point_cloud_data中。最后,我们返回point_cloud_data作为函数的返回值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值