我在看完文件后试了一下。它没有显示文件的最后一行,它应该在完成读取后在new1字符串指针中显示。
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main () {
std::string line;
int i;
std::string *new1;
new1 = new string;
ifstream myfile ("path.txt");
while (myfile)
{
getline (myfile,line);
cout << line<<endl;
new1=line;
}
cout<<new1<<endl;
myfile.close();
cin.get();
return 0;
}
提前谢谢。
发布于 2014-11-19 06:03:42
这是因为在结束循环之前最后一个值是eof。
用这个:
while (getline (myfile,line))
{
cout << line<<endl;
new1=line;
}
https://stackoverflow.com/questions/27009847
复制相似问题