fstream 示例代码

fstream 示例代码

参考:C++中文件流(fstream)的使用方法及示例

  1. 获取流长度,并读取。
int _tmain(int argc, _TCHAR* argv[])
{
    using namespace std;
    ifstream stream("F:\\WorkSpace\\StreamDemo\\Debug\\abc.txt", ios::in);
    if(!stream)
        cerr << "Open failed" << endl;
    stream.seekg(0, ios::end);
    int len = stream.tellg();
    stream.seekg(0, ios::beg);
    char* buffer = new char[len];
    memset(buffer, '\0', len);
    if(!stream.read(buffer, len))
    {
        cout << "Read error:" << stream.rdstate() << endl;
    }
    cout.write(buffer, len);
    stream.close();
    system("pause");
    return 0;
}
  1. mysql 读取blob。
std::stringstream s;
        s << "SELECT * FROM Dao WHERE gender = "<< data <<"";

        prepStmt = con->prepareStatement (s.str());
        res = prepStmt->executeQuery();

        unsigned char* ptr;
        size_t blobSize=100;
        std::istream *is;
        while(res->next()){
            tempFR.uuId = res->getInt64("uuId");
            tempFR.cameraNo = res->getInt("cameraNo");
            tempFR.age = res->getInt("age");
            tempFR.gender = res->getInt("gender");
            is = res->getBlob("image");
            is->seekg (0, std::ios::end);
            blobSize = is->tellg();
            is->seekg (0, std::ios::beg);
            unsigned char * image = new unsigned char[blobSize];
            is->read((char*)image, blobSize);
            size_t imageSize = blobSize;
        }
  • 另外一种没用过:
std::istream *blobData = set->getBlob("image");
std::istreambuf_iterator<char> isb = std::istreambuf_iterator<char>(*blobData);
std::string blobString = std::string(isb, std::istreambuf_iterator<char>());
unsigned char * image = blobString.c_str();
blobData->seekg(0, ios::end);
size_t imageSize = blobData->tellg();
`fstream`是C++中用于文件输入和输出的流类。它是`iostream`的派生类,同时继承了`ifstream`和`ofstream`的功能,可以实现文件的读取和写入操作。 下面是`fstream`的定义和示例代码: 定义: 1. 需要包含`<fstream>`头文件来使用`fstream`类。 2. 使用`std::fstream`来定义一个`fstream`对象。 3. `fstream`对象可以打开文件用于读取或写入,并且可以通过流操作符(`<<`和`>>`)进行文件的输入和输出。 示例代码: ```cpp #include <iostream> #include <fstream> int main() { std::fstream file("example.txt", std::ios::out); // 创建并打开文件用于写入 if (file.is_open()) { file << "Hello, World!" << std::endl; // 写入数据到文件 file.close(); // 关闭文件 } else { std::cout << "Failed to open the file." << std::endl; } std::fstream file2("example.txt", std::ios::in); // 打开文件用于读取 if (file2.is_open()) { std::string line; while (std::getline(file2, line)) { // 逐行读取文件内容 std::cout << line << std::endl; // 输出读取到的内容 } file2.close(); // 关闭文件 } else { std::cout << "Failed to open the file." << std::endl; } return 0; } ``` 在上述示例中,我们首先包含了`<fstream>`头文件以使用`fstream`类。 在`main()`函数中,我们创建了一个`fstream`对象`file`,并使用`std::ios::out`参数将文件打开用于写入。然后,我们使用流操作符`<<`将数据写入文件,并最后关闭文件。 接着,我们创建了另一个`fstream`对象`file2`,并使用`std::ios::in`参数将文件打开用于读取。然后,我们使用`std::getline()`函数逐行读取文件的内容,并将每行内容输出到屏幕上。 最后,我们关闭了文件。 使用`fstream`类,我们可以方便地进行文件的读取和写入操作,实现对文件的读写功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值