美文网首页
fstream读取文件

fstream读取文件

作者: 江河湖海洋 | 来源:发表于2022-10-25 13:49 被阅读0次

    fstream读取文件

    #include <fstream>
    #include <iostream>
    #include <string>
    #include <stdio.h>
    
    int readFile()
    {
        std::ifstream ifs;
        //ifs.open("read.txt", std::ios::in);
        ifs.open("read.txt", std::ios::in | std::ios::binary);
        if (!ifs.is_open())
        {
            std::cout << "文件打开失败" << std::endl;
        }
    #if 0
        char buf[1024] = { 0 };
        while(ifs >> buf)
        {
            std::cout << buf << std::endl;
        }
        char buf[1024] = { 0 };
        while(ifs.getline(buf.size(buf)))
        {
            std::cout<< buf << std::endl;
        }
        std::string buf;
        while(getline(ifs, buf))
        {
            std::cout<< buf << std::endl;
        }
    
        char c;
        while((c = ifs.get()) != EOF)
        {
            std::cout << c;
        }
    #endif
        ifs.seekg(0, ifs.end);
        uint32_t fileSize = ifs.tellg();
        std::cout << "fileSize="<< fileSize << std::endl;
        //读取二进制
        char buf[1024] = { 0 };
        //ifs.read(buf, sizeof(buf));
        ifs.read(buf, 10);
        uint32_t readlength = ifs.gcount();
        std::cout << "readlength="<< readlength << std::endl;
        std::cout << buf << std::endl;
        ifs.close();
    
        uint16_t did = 0xf1bc; 
        printf("%s[%-02x]\n", __FILE__, did);
        return 0;
    }
    
    int main()
    {
        readFile();
        return 0;
    }
    

    相关文章

      网友评论

          本文标题:fstream读取文件

          本文链接:https://www.haomeiwen.com/subject/cxbazrtx.html