Each row is stored as an element of the vector object

From , 3 Years ago, written in C++, viewed 52 times.
URL https://pastebin.vip/view/059fdcd9
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <vector>
  5. using namespace std;
  6.  
  7. int fileToVector(string fileName, vector<string>& svec)
  8. {
  9.         ifstream infile(fileName.c_str());
  10.         if (!infile)
  11.                 return 1;
  12.         string s;
  13.         while (getline(infile,s))//infile>>s  前者是输出一行一行的文本,用这个只是输出一个一个的单词
  14.                 svec.push_back(s);
  15.         infile.close();
  16.         infile.clear();
  17.         if (infile.eof())
  18.                 return 4;
  19.         if (infile.bad())
  20.                 return 2;
  21.         if (infile.fail())
  22.                 return 3;
  23. }
  24. int main()
  25. {
  26.         vector<string> svec;
  27.         string fileName,s;
  28.         cout<<"Enter fileName:"<<endl;
  29.         cin>>fileName;
  30.         switch(fileToVector(fileName,svec))
  31.         {
  32.             case 1:
  33.                         cout<<"error: can not open file:" <<fileName<<endl;
  34.                         return -1;
  35.                 case 2:
  36.                         cout<<"error:system failure"<<endl;
  37.                         return -1;
  38.                 case 3:
  39.                         cout<<"error:read failure" <<endl;
  40.                         return -1;
  41.                 default:
  42.                         break;
  43.         }
  44.         cout<<"Vector"<<endl;
  45.         for (vector<string>::iterator iter = svec.begin(); iter != svec.end(); ++iter)
  46.                 cout<<*iter<<endl;
  47.         return 0;
  48. }
  49.  

Reply to "Each row is stored as an element of the vector object"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website