VisualStudio2008中的C++ 如何读文本文件?

下面是C#的代码,翻译成C++是什么样子?
using (StreamReader sr = new StreamReader("TestFile.txt")) {
String text = sr.ReadToEnd();
}

第1个回答  2012-05-13
#include <iostream>
#include<fstream>
#include <string>
using namespace std;
int main()
{
ifstream infile("TestFile.txt");
string word;
string text;
while(infile >> word);
cout << word; //word 是最后一个单词;
infile.clear();
infile.close();
infile.open("TestFile.txt");
while(getline(infile,text));
cout << text; //text 是最后一行;
return 0;
}
第2个回答  2012-05-13
#include <iostream>
#include <strstream>
#include <string>
using namespace std;

int main()
{
strstream s;
s << "TestFile.txt";
string text;
s >> text;
cout << text;
system("pause");
}本回答被提问者和网友采纳
相似回答