学习文件输入/输出

学习文件输入/输出在C++17以前,操作文件使用的是非C++的API,它们是不安全、不通用甚至是不完整的。打开文件不同的方法使用std::fstream对象,如下例:#include <fstream>#include <iostream>int main(){ if (auto file = std::fstream("text.txt")) { std::cout << "success\n"; } e
摘要由CSDN通过智能技术生成

《Hands-On System Programming with C++》读书笔记之八

在C++17以前,操作文件使用的是非C++的API,它们是不安全、不通用甚至是不完整的。

打开文件

不同的方法

使用std::fstream对象,如下例:

#include <fstream>
#include <iostream>

int main()
{
   
    if (auto file = std::fstream("text.txt"))
    {
   
        std::cout << "success\n";
    }
    else
    {
   
        std::cout << "failure\n";
    }
}

可以用touch test.txt命令先在当前目录下建立一个文件。默认的权限是read/write。
也可以用is_open()确认文件是否成功打开。

#include <fstream>
#include <iostream>

int main()
{
   
    if (auto file = std::fstream("text.txt"); file.is_open())
    {
   
        std::cout << "success\n";
    }
}

上面的例子是使用了std::fstream()的构造函数打开文件,也可以使用open()函数。

#include <fstream>
#include <iostream>

int main()
{
   
    auto file = std::fstream();
    if (file.open("test.txt"); file.is_open())
    {
   
        std::cout << "success\n";
    }
}

上面的各个例子中都不需要使用close()来关闭文件,析构函数会自动完成,当然也可以手工完成。

#include <fstream>
#include <iostream>

int main()
{
   
    std::cout << std::boolalpha;
    if (auto file = std::fstream("text.txt"))
    {
   
        std::cout << file.is_open() << '\n'; // true
        file.close();
        std::cout << file.is_open() << '\n'; // false
    }
}

几种打开模式

打开文件有两种基本模式:

  • std::ios::in:读模式
  • std::ios::out:写模式
    同时还哦于以下一种模式可以与上面的基本模式结合使用:
  • std::ios::binary:二进制格式打开
  • std::ios::app:在文件末尾写入
  • std::ios::ate:打开时操作点在文件末尾
  • std::ios::trunc:打开时删除原文件内容

例如

#include <fstream>
#include <iostream>

int main()
{
   
    constexpr auto mode = std::ios::out | std::ios::binary | std::ios::app;
    if (auto file = std::fstream("text.txt", mode))
    {
   
        std::cout << "success\n";
    }
}

注意在使用std::ios::out模式时,除非指明使用std::ios::ate或std::ios::app模式,否则默认使用std::ios::trunc模式。
在使用了std::ios::ate或std::ios::app模式后,仍然可以通过seekp(0)方法将操作位置回到文件的起始。
当std::ios::in和std::ios::out一起使用时,如果要先清除原有文件内容,还是需要显式指明std::ios::trunc模式。

文件的读与写

读文件

Reading by field

以空格或换行为分解,把字符串写入字符串变量中。假设文件中已经写入了Hello World,可以用echo “Hello World” >text.txt实现。

#include <fstream>
#include <iostream>

int main()
{
   
    if (auto file = std::fstream("text.txt"))
    {
   
        std::string str1, str2;
        file >> str1 >> str2;
        std::cout << str1 << " " << str2 << '\n'; // Hello World
    }
}

也可以直接读入整型变量,当文件内容不是整型时,读入结果为0。

为了读入自定义类型,可以重载std::fstream操作符。

#include <fstream>
#include <iostream>

struct myclass
{
   
    std::string str1;
    std::string str2;
};

std::fstream &operator>>(std::fstream &is, myclass &obj)
{
   
    is >> obj.str1;
    is >> obj.str2;
    return is;
}

std::ostream &operator<<(std::ostream &os, const myclass &obj)
{
   
    os << obj.str1;
    os << ' ';
    os << obj.str2;
    return os;
}

int main()
{
   
    if (auto file = std::fstream("text.txt"))
    {
   
        myclass obj;
        file >> obj;
        std::cout << obj << '\n'; // Hello World
    }
}
Reading bytes

可以用get()直接从文件中读入字节。假设下面例子中文件的内容是Hello World

#include <fstream>
#include <iostream>

int main()
{
   
    if (auto file = std::fstream("text.txt"))
    {
   
        char c = file.get();
        std::cout << c << '\n'; // H
    }
}

读入多个字节还是不安全的,因为无法使用std::string类,只能读到C风格的数组中。

#include <fstream>
#include <iostream>

int main()
{
   
    if (auto file = std::fstream("text.txt"))
    {
   
        char buf[25] = {
   };
        file.read(buf, 11);
        std::cout << buf << '\n'; // Hello World
    }
}

可以把read()包在一个模板中,检查读入的长度是否大于缓冲区。

#include <fstream>
#include <iostream>

template<typename T, std::size_t N>
void myread(std::fstream &file, T(&str)[N], std::size_t count)
{
   
    if (count >= N)
    {
   
        throw std::out_of_range("file.read out of bounds");
    }
    file.read(static_cast<char*>(str), count);

}

int main()
{
   
    if (auto file = std::fstream("text.txt"))
    {
   
        char buf[25] = {
   };
        myread(file, buf, 11);
        std::cout << buf << '\n'; // Hello World
    }
}

使用tellg()方法来获得读操作在文件中的位置。

#include <fstream>
#include <iostream>

int main()
{
   
    
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值