c++ read/write excel

15 篇文章 1 订阅
1 篇文章 0 订阅
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++ 中,可以通过类继承 `fstream`,然后重写 `write`、`read` 和 `seek` 方法来实现自定义的文件读写操作。具体实现如下: ```c++ #include <fstream> #include <iostream> using namespace std; class MyFileStream : public fstream { public: MyFileStream(const char* filename, ios::openmode mode) : fstream(filename, mode) {} void write(const char* buf, streamsize size) { // 自定义写入操作,这里只是简单地输出写入的内容 cout << "Writing " << size << " bytes: "; for (streamsize i = 0; i < size; ++i) { cout << buf[i]; } cout << endl; // 调用父类的写入方法 fstream::write(buf, size); } void read(char* buf, streamsize size) { // 自定义读取操作,这里只是简单地输出读取的内容 cout << "Reading " << size << " bytes" << endl; // 调用父类的读取方法 fstream::read(buf, size); } streampos seek(streamoff off, ios_base::seekdir dir) { // 自定义偏移操作,这里只是简单地输出偏移量和方向 cout << "Seeking " << off << " bytes " << (dir == ios::beg ? "from the beginning" : (dir == ios::cur ? "from the current position" : "from the end")) << endl; // 调用父类的偏移方法 return fstream::seek(off, dir); } }; int main() { MyFileStream fs("test.txt", ios::out | ios::in | ios::trunc); fs.write("Hello, world!", 13); char buf[13]; fs.seekg(0); fs.read(buf, 13); cout << "Read: " << buf << endl; fs.close(); return 0; } ``` 在上面的示例中,我们定义了一个 `MyFileStream` 类,继承了 `fstream` 类,并重写了 `write`、`read` 和 `seek` 方法。在 `write` 方法中,我们自定义了写入操作,输出了写入的内容,并调用了父类的写入方法;在 `read` 方法中,我们自定义了读取操作,输出了读取的内容,并调用了父类的读取方法;在 `seek` 方法中,我们自定义了偏移操作,输出了偏移量和方向,并调用了父类的偏移方法。 最后,在 `main` 函数中,我们创建了一个 `MyFileStream` 对象,并进行了写入、读取和偏移操作,输出了相应的结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值