楼上的那个是C语言版的,用的是FILE 类型,我给你这个纯C++版的,不用循环读取。
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void main()
{
string str1;
ifstream infile("c:\\1.txt",ios::in);
if(!infile)
cout<<"文件打开错误"<<endl;
else
infile>>str1;
cout<<"读入的字符为: "<<str1<<endl;
}
记住:ifstream infile("c:\\1.txt",ios::in);中的“c:\\1.txt"就是文件在磁盘中的位置,你可以改为其他位置。