我想把txt文件里面的单词按照行和列放入一个char的二维数组里面 (默认一行最多1000个单词) 但是写出来后发现乱码了 请问这个读取过程有什么问题吗?
txt文件是这个:
I gazed—and gazed—but little thought
What wealth the show to me had brought:
For oft, when on my couch i lie
In vacant or in pensive mood,
They flash upon that inward eye
Which is the bliss of solitude;
And then my heart with pleasure fills,
And dances with the daffodils.
你的代码有不少问题。既然是C++,就尽量用STL库。
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
using namespace std;
int main()
{
ifstream infile("testfile.txt");
vector<string> lines;
string line;
while (getline(infile, line))
lines.push_back(line);
for (const auto &line : lines)
cout << line << '\n';
return 0;
}