在C++从文本文件逐个读取字符
有文本文件in.txt
内容:
abcder
abcdddddd
现想将文件in.txt的内容读出来,放在主函数的数组中,应如何用C++ 语言描述?
[解决办法]
//c++ CODE
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(int argc, char** argv)
{
string s, str;
ifstream infile( "in.txt ");
if (!inf)
return -1;
while (getline(infile, s))
{
str += s;
cout < < s < < endl;
}
//如果想要一个C的数组,可以这样
char * carray = str.c_str();
printf( "%s ", carray);
return 0;
}