Store each row in each element of the vector.

Source: Internet
Author: User

# Include <iostream>
# Include <vector>
# Include <fstream>
# Include <string>
Using namespace STD;
/* Store each row in the file in each element in the vector */
Int filetovector (string filename, vector <string> & SVEC)
{
Ifstream infile (filename. c_str ());
If (! Infile) return 1; // file loading failed
String S;
While (Getline (infile, S) // read data row by row and press it into the vector.
{
SVEC. push_back (s );
}
Infile. Close ();
If (infile. EOF () return 4; // indicates the end of the object.
If (infile. Bad () return 2; // System Error
If (infile. Fail () return 3; // read failed
}

Int main ()
{
Vector <string> SVEC;
String filename, S;
Cout <"Enter filename:" <Endl;
Cin> filename;
Switch (filetovector (filename, SVEC ))
{
Case 1:
Cout <"file loading failed" <FILENAME <Endl;
Break;
Case 2:
Cout <"system error" <Endl;
Break;
Case 3:
Cout <"read failed" <Endl;
Break;
Default:
Break;
}
Cout <"vector:" <Endl;
// Print the content in the vector
For (vector <string >:: iterator iter = SVEC. Begin (); iter! = SVEC. End (); ++ ITER)
{
Cout <* ITER <Endl;
}
Return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.