登录    注册      
    
  

News Message

c++中ifstream一次读取文件



c++中ifstream一次读取文件



c++中一次读取整个文件的内容的方法:

  1. 读取至char*的情况
std::ifstream t;  
int length;  
t.open("file.txt");      // open input file  
t.seekg(0, std::ios::end);    // go to the end  
length = t.tellg();           // report location (this is the length)  
t.seekg(0, std::ios::beg);    // go back to the beginning  
buffer = new char[length];    // allocate memory for a buffer of appropriate dimension  
t.read(buffer, length);       // read the whole file into the buffer  
t.close();                    // close file handle  
  
// ... do stuff with buffer here ...  
  1. 读取至std::string的情况:

第一种方法:

#include   
#include   
#include   
  
std::ifstream t("file.txt");  
std::string str((std::istreambuf_iterator<char>(t)),  
                 std::istreambuf_iterator<char>()); 

第二种方法:

#include   
#include   
#include   
std::ifstream t("file.txt");  
std::stringstream buffer;  
buffer << t.rdbuf();  
std::string contents(buffer.str());



Share Http URL:  http://www.wittx.cn/get_news_message.do?new_id=776



请输入评论





























Best Last Month

佛学经典

佛学经典

Office culture and education

by wittx


Fundamentals of Machine Component Design

Fundamentals of Machine Component Design

Information industry

by wittx


2020/12/12 金融行情

2020/12/12 金融行情

Information industry

by wittx


Loss Function

Loss Function

Information industry

by wittx


基于深度强化学习的遥感视频目标跟踪



量子计算机工作原理

量子计算机工作原理

Electronic electrician

by wittx


冲击美股今年最大 IPO!Snowflake 计划融资 30.8 亿美元



Numerical Analysis of a Liquid Nitrogen Engine for Efficient Energy Conversion



DeepMind新算法MuZero

DeepMind新算法MuZero

Information industry

by wittx


Google Research提出超强预训练模型CoCa,在ImageNet上微调Top-1准确率达91%!