0
点赞
收藏
分享

微信扫一扫

c++ 读取txt文件保存到vect while(infile>> word)


​​link​​

#include<iostream>
#include <string>
#include<fstream>
#include<cstdlib>
#include<vector>
using namespace std;

int main()
{
char data;
ifstream infile;
ifstream infile1;
std::vector<string> classNames;
infile.open("../my.txt");

if(infile.fail())
{
cout<<"Fail to open the input file"<<endl;
exit(1);
}

while( !infile.eof() )
{
infile >> data;
cout << data <<endl;
}

infile1.open("../my.txt");
vector<string> alpha;
std::string word;
while( infile1>> word)
{
alpha.push_back(word);
}


cout<<"alpha.size="<<alpha.size()<<endl;
for (int i = 0; i < alpha.size(); i++)
{
cout << "alpha["<<i <<"]= "<< alpha[i] << endl;
}



infile.close();
cout << endl;

return 0;
}


举报

相关推荐

0 条评论