C++分析——文件操作

来源:互联网 发布:js替换标签 编辑:程序博客网 时间:2024/06/03 15:36

  • 文件操作方法
    • 打开文件属性open
    • 设置文件当前指针
  • 写文件操作类stdofstream
    • 写入数据
    • 写入数据块
  • 读文件操作类stdifstream
    • 读出数据
      • 读出一个字符
      • 读到空格结束
      • 读到回车字符结束
      • 读到文件末尾结束
    • 读出数据块

C++文件操作主要依靠两个类来进行

1、 写文件操作类:std::ofstream
2、 读文件操作类:std::ifstream

std::fstream类可以进行读写操作,但一般用上面两个类进行,分工明确。

需要包括的头文件:

#include <iostream>#include <fstream>

C++操作函数像C需要将相对地址转换成绝对地址。

文件操作结构体

typedef struct Cpp_sys_info{    char name[10];    char group[10];    int  permissions;}Cpp_User_info;

文件操作方法

打开文件属性open

ios::in     :以输入方式打开文件ios::out    :以输出方式打开文件ios::app    :以追加的方式打开文件,所有输出附加在文件末尾ios::ate    :文件打开后,文件流指针定位到文件尾,和ios::app功能相同ios::trunc  :如果文件已存在则先删除该文件 ios::binary :以二进制的方式打开文件

具体用法在后面的示例中展现。

设置文件当前指针

seekp:设置写文件指针
seekg:设置读文件指针

ios::beg:  文件开头 ios::cur:  文件当前位置 ios::end:  文件结尾
m_steam_read.seekg(-8L,ios::end);   //把读文件指针移动到从结尾位置起始,往负方向的6个字节处m_steam_write.seekp(5L,ios::beg);   //把写文件指针移动到从文件开头起始,往正方向的5个字节处

写文件操作类std::ofstream

写入数据

源码

 //std::ofstream steam_write(p_argv[1],ios::in | ios::trunc);       //比较简单的初始化函数,直接读取文件std::ofstream steam_write;m_steam_write.open(p_argv[1],ios::in | ios::trunc);         //比较正式的写法,更加直观 if (!steam_write.is_open())                            //判断文件是否打开成功 {        cout << "wirte is NO" << endl;        return; } steam_write << __func__ << " func is wirte test " << 45 << 0x61 <<endl;        //写入数据 steam_write << "Test C++ wirte number " << 54.79;                  //写入数据 steam_write.close();       //关闭文件

执行结果

$ ./project.o write.txt$ cat write.txt write_file func is wirte test 4597Test C++ wirte number 54.79$

写入数据块

源码

    Cpp_User_info Test_User_info[2] = {     //需要写入的数据块        {"admin_cpp","root_cpp",3},        {"guest_cpp","user_cpp",4}    };    std::ofstream steam_write;    steam_write.open(p_argv[1],ios::in | ios::binary | ios::app); if (!steam_write.is_open()) {        cout << "wirte is NO" << endl;        return; }    steam_write.write(reinterpret_cast<char *>(&Test_User_info),static_cast<int>(sizeof(Test_User_info)));    steam_write.close();

执行结果

$ ./project.o writetable.bin $ ./project.o writetable.bin $ ./project.o writetable.bin $ hexdump writetable.bin 0000000 6461 696d 5f6e 7063 0070 6f72 746f 635f0000010 7070 0000 0003 0000 7567 7365 5f74 70630000020 0070 7375 7265 635f 7070 0000 0004 00000000030 6461 696d 5f6e 7063 0070 6f72 746f 635f0000040 7070 0000 0003 0000 7567 7365 5f74 70630000050 0070 7375 7265 635f 7070 0000 0004 00000000060 6461 696d 5f6e 7063 0070 6f72 746f 635f0000070 7070 0000 0003 0000 7567 7365 5f74 70630000080 0070 7375 7265 635f 7070 0000 0004 00000000090 6461 696d 5f6e 7063 0070 6f72 746f 635f00000a0 7070 0000 0003 0000 7567 7365 5f74 706300000b0 0070 7375 7265 635f 7070 0000 0004 000000000c0$

hex格式查询即为
这里写图片描述

读文件操作类std::ifstream

读文件就分几种读法,读到空格结束,读到回车字符结束,读数据块

读出数据

读数据的文件

$ cat write.txt write_file func is wirte test 4597Test C++ wirte number 54.79$

读出一个字符

源码

    std::ifstream steam_read;    steam_read.open(p_argv[1],ios::out); if (!steam_read.is_open()) {        cout << "wirte is NO" << endl;        return; } char ch = 0; while(!steam_read.eof())  {     steam_read.get(ch);    //读取一个字符     cout << ch; }    cout << endl;    steam_read.close(); 

执行结果

$ ./project.o write.txt write_file func is wirte test 4597Test C++ wirte number 54.799$

读到空格结束

源码

    std::ifstream steam_read;    steam_read.open(p_argv[1],ios::out); if (!steam_read.is_open()) {        cout << "wirte is NO" << endl;        return; }    string read_data;    steam_read >> read_data;        //读到空格结束    cout << read_data << endl;    steam_read.close(); 

执行结果

$ ./project.o write.txt write_file$

读到回车字符结束

源码

    std::ifstream steam_read;    steam_read.open(p_argv[1],ios::out);    if (!steam_read.is_open())    {        cout << "wirte is NO" << endl;        return;    }    string read_data;    std::getline(steam_read,read_data); //读到回车字符结束    cout << read_data << endl;    steam_read.close();

执行结果

$ ./project.o write.txt write_file func is wirte test 4597$

读到文件末尾结束

源码

    std::ifstream steam_read;    steam_read.open(p_argv[1],ios::out); if (!steam_read.is_open()) {        cout << "wirte is NO" << endl;        return; }    string read_data;    while(!steam_read.eof())    //判断是否到达文件末尾    {        std::getline(steam_read,read_data);        cout << read_data << endl;    }    steam_read.close(); 

执行结果

$ ./project.o write.txt write_file func is wirte test 4597Test C++ wirte number 54.79$

读出数据块

源码

    std::ifstream steam_read;    steam_read.open(p_argv[1],ios::out | ios::binary); if (!steam_read.is_open()) {        cout << "wirte is NO" << endl;        return; }    string read_data;    while(!steam_read.eof())    //判断是否到达文件末尾    {        std::getline(steam_read,read_data);        cout << read_data << endl;    }    steam_read.close(); 

执行结果

$ ./project.o writetable.bin name is         admin_cppgroup is        root_cpppermissions is  3name is         guest_cppgroup is        user_cpppermissions is  4           //文件函数判断多一次,读取不成功,打印上一次的值name is         guest_cppgroup is        user_cpppermissions is  4$

hex格式查询即为
这里写图片描述

原创粉丝点击