如何在char*中存储文件

Pea*_*oul -5 c c++ arrays char

我的问题是:如何将.txt文件的内容存储char*在C++ 中的命名m_str中?

请注意,我的文件有一个非常明确的格式,我想保留.我不想将这些线合并在一起.我希望第1行保留第1行,第2行是什么,保留第2行.因为最终我将序列化char*并通过网络发送,当节点收到它时,它将反序列化它然后将内容放在一个文件中,并读取原始文件中的行.

谢谢.

Naw*_*waz 7

你可以使用vector作为:

std::ifstream file("file.txt");
std::istreambuf_iterator<char> begin(file), end;
std::vector<char> v(begin, end); //it reads the entire file into v

char *contentOfTheFile= &v[0]; 
Run Code Online (Sandbox Code Playgroud)

文件的内容存储在contentOfTheFile.您可以使用它,也可以修改它.