c++ - Read file (containing int, string) to vector of pairs using ifstream -
I am trying to read a vector of a txt file by adding. The text file contains the following: The first line tells you how many strings are. The second row is an integer, it is telling the number of characters in the first (string). And then, the third line has a string, then the next "pattern" follows (how many characters and then the string itself) for example:
- 2
- 4
- CDEF
I thought, reading the first line separately (which works fine). Then make pairs for each int and string. The second part, in any way, does not work, so I really appreciate it if someone can help me find fault.
So here's my code:
#include & lt; Iostream & gt; # Include & lt; Stdlib.h & gt; # Include & lt; Fstream & gt; # Include & lt; Sstream & gt; #include & lt; String & gt; # Include & lt; Vector & gt; # Include & lt; Utility & gt; using namespace std; (Arg arg []] {if (argv [1]) {some code here ...} / arg [2] is formatted in some ways in the example above (argv [2]) {// declared declare: string Buffer string string; vector & lt; int, string & gt; & gt; patVec; pair & lt; int, string & gt; patPair; // ifstream first txt file = (argv [2] ifstream ifile2 (Argv [2]); // Get the first line and store in the variable number of the pattern - & gt; It gets alright (ifile2, floating); Int number of partiality = Atoi (temporary.c_str ()); // Proteus rest For file int ii (patwak [i = 0 ]; I & lt; = numberOfPatterns - 1; i ++) {Fill the vector with the strings of stringfree strings (buffer); // pairs of gateline (ifile2, buffer); gt; gt; & gt; Patpeer.fost; strings & Gt; Patpier.Secand; Patwak.Push_back (Patpier);}}}
Thanks in advance!
So here is the correct version of the relevant part, now works : Thanks for the prompt;)
// declared declared string buffer; string temporary; vector & lt; pair & lt; int, string & gt; & Gt; PatVec; Pair & lt; Int, string & gt; PatPair; Int Platenth; String pCharacters; // Get the first line and store in the count number of the pattern (ifile2, temp); Int numberofpattents = atio (temporary.c.r.) ()); Cout & lt; & Lt; "Number of Patterns" is & lt; & Lt; Number of Patterns & lt; & Lt; Endl; // Process for the rest file (Int i = 0; I & lt; = numberOfPatterns - 1; i ++) {// string string strushes (buffer); // get the pair (ifile2, buffer) with the vector; Stringstream intruder (buffer); Int_reader & gt; & Gt; Plength; Gateline (if2, buffer); Stringstream Strawberr (buffer); Str_reader & gt; & Gt; PCharacters; Patptier = MAPAir (PDAR ac); PatVec.push_back (patPair); Cout & lt; & Lt; "Length of Pattern" & lt; & Lt; I & lt; & Lt; "Is" & lt; & Lt; Wave and lieutenant; & Lt; Endl; Cout & lt; & Lt; "Characters are:" & lt; & Lt; PCR Actors & Lt; & Lt; Endl; } Ifile2.close (); } / P>
-
prepayment of variable.
-
No typed for pair
But it appears to work well.
#include & lt; String & gt; # Include & lt; Vector & gt; # Include & lt; Iostream & gt; # Include & lt; Fstream & gt; # Include & lt; Sstream & gt; # Include & lt; Cstdlib & gt; Int main (int argc, char * argv []) // declared declared: std :: string buffer; Std :: string temporary; Std :: vector & lt; Std :: pair & lt; Int, std :: string & gt; & Gt; PatVec; Std :: Pair & lt; Int, std :: string & gt; PatPair; // ifstream first txt file = (argv [2] std :: ifstream ifile2 ((const char *) (argv [2])); / Get the first line and store in the variable number of the pattern - & gt; The process balance for getline (ifile2, floating); int number of patterns = etio (temporary c_str ()); // (int i = 0; i & lt; = numberOfPatterns - 1; i ++) {getline ( Ifile2, buffer); Std :: stringstream int_reader (buffer); // Fill the vector with int_reader; gt; patPair.first; getline (ifile2, buffer); std :: stringstream str_reader (buffer); str_reader & Gt; patpair.second; PatVec.push_back (patpair);} Return 0;}
Comments
Post a Comment