fstream到const char * [英] fstream to const char *
本文介绍了fstream到const char *的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想要做的是读取一个名为"test.txt"的文件,然后将文件的内容设置为const char *类型.怎么会这样?
What I want to do is read a file called "test.txt", and then have the contents of the file be a type const char *. How would one do this?
推荐答案
#include <string>
#include <fstream>
int main()
{
std::string line,text;
std::ifstream in("test.txt");
while(std::getline(in, line))
{
text += line + "\n";
}
const char* data = text.c_str();
}
注意不要对数据进行显式调用
Be careful not to explicitly call delete on data
这篇关于fstream到const char *的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文