繁体   English   中英

如何读取txt文件为字符串?

[英]How to read a txt file to string?

我只想读取一个txt文件并接收一个像这样的字符串文件

Blob rgbBlob;
    string strIccRGBFile = "./icc/RGB.icc";
    string strIccRGBContent = LoadFile(strIccRGBFile);
    rgbBlob.update(strIccRGBContent.c_str(), strIccRGBContent.length());
    image.profile("ICM", rgbBlob);

我如何实现LoadFile函数

#include <fstream>
#include <string>
int main()
{
  std::string buff;
   std::fstream fs("filename",std::ios::in | std::ios::ate)
   if(fs.is_open())
   {
       fstream::pos_type size = fs.tellg();
       fs.seekg(0);
       buff.resize(size);
       fs.read(&buff[0],size);
   }
   std::cout << buff << endl;
}

这是一个如何完整读取文件到字符串缓冲区的示例。 它应该给您一个关于如何进行的好主意。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM