1#include <iostream>2#include <fstream>3#include <vector>4#include <string>5 6 using namespacestd;7 8ifstream& ReadLine (ifstream&inch, vector<string> &veclines)9 {Ten stringLine ; One while(!inch. EOF ()) A { -Getlineinch, line); -cout<<line<<Endl; the Veclines.push_back (line); - } - return inch; - } +ifstream& Readstr (ifstream&inch, vector<string> &vecstrs) - { + stringstr; A while(!inch. EOF ()) at { - inch>>str; -cout<<str<<Endl; - vecstrs.push_back (str); - } - return inch; in } - intMain () to { +Ifstream InFile ("1.txt"); - if(!inFile) the return-1; *vector<string>Veclines; $ ReadLine (infile,veclines);Panax Notoginseng infile.close (); - infile.clear (); theInfile.open ("1.txt"); + if(!inFile) A return-1; thevector<string>Vecstrs; + readstr (infile,vecstrs); - infile.close (); $ infile.clear (); $ return 0; -}
Summarize:
(1) Ifstream uses Getline to get a row of elements within a file.
(2) Ifstream inheritance IStream can use >> to get a single string.
Results:
C + + Primer exercises 8.9 ifstream How to use