//vs2008实测通过
#include
#include
using namespace std;
void main()
{
ifstream infile;
infile.open("test.txt",ios::in);
char str[1000];
while(!infile.eof())
{
infile>>str;
}
printf("%s\n", str);//已保存在变量str中
infile.close();
}
需要用到fstream头文件
用ifstream进行读取
存到变量里面是需要看需要读取的变量的格式的
例如读取到结构体中就是将变量依次读取存入结构体的每个变量里面
strcut stockType
{
string personAddress;
string personCity;
string personState;
int personZIP;
};
void addressType :: getData()
{
ifstream infile;
int index;
string inputFile;
stockType addressTypeList[NO_OF_PERSON];
cout<<"Enter the file path: ";
cin>>inputFile; //输入txt文件路径
cout<
infile(inputFile.c_str()); //infile按照指定路径读取txt文件
for(index = 0; index < NO_OF_PERSON; index++)//读取txt文件至struct
{
infile>>addressTypeList[index].personAddress;
infile>>addressTypeList[index].personCity;
infile>>addressTypeList[index].personState;
infile>>addressTypeList[index].personZIP;
}
}
string fileInput (ifstream &infile)
{
char str[FILE_LENGTH];
int i = 0;
while (!infile.eof ())
{
str[i++] = infile.get ();
};
str[i] = '\0';// end of str signal for c style string
return str;
}