|
Prev: time function
Next: Increment/ by C++
From: ma740988 on 3 Aug 2006 21:12 The contents of my_file.txt is as follows: 0,524288,FFF,524288,1000 1,624288,F4F,32768,2000 -1,-1,-1,-1,-1 Now given: # include <iostream> # include <vector> # include <string> # include <fstream> # include <sstream> struct wfts { int id; int vp_size ; int vp_mask; int fp_size ; int fp_mask; wfts ( int id, int vp_size, int vp_mask_, int fp_size, int fp_mask ) : id ( -1 ) , vp_size ( 0 ) , vp_mask ( -1 ) , fp_size ( 0 ) , fp_mask ( -1 ) {} // perhaps a friend to dump things.. }; bool read_file ( std::ifstream& ifs, std::vector < wfts >& wts_vec ) { bool success ( false ); std::string line; wts_vec.clear(); //ifs.ignore(); while ( std::getline( ifs, line, ',' ) ) { std::cout << "std::string line = " << line << std::endl; std::istringstream iss; iss.str ( line ) ; int id ( 0 ); int vp_size ( 0 ) ; int vp_mask ( 0 ); int fp_size ( 0 ) ; int fp_mask ( 0 ); iss >> id >> vp_size >> vp_mask >> fp_size >> fp_mask; if ( id != -1 && vp_size != -1 && vp_mask != -1 && fp_size != -1 && fp_mask != -1 ) { wts_vec.push_back ( wfts ( id, vp_size, vp_mask, fp_size, fp_mask ) ); } } if ( wts_vec.size() ) success = true; return ( success ); } int main() { std::ifstream ifs ( "my_file.txt" ); std::vector < wfts > wfts_vec; if ( read_file ( ifs, wfts_vec ) ) { std::cout << " Id " << " " << " vp_size " << " " << " vp_mask " << " " << " fp_size " << " " << " fp_mask " << std::endl; for ( std::vector < wfts >::size_type idx ( 0 ); idx < wfts_vec.size(); ++idx ) { std::cout << wfts_vec [ idx ].id << " " << wfts_vec [ idx ].vp_size << " " << wfts_vec [ idx ].vp_mask << " " << wfts_vec [ idx ].fp_size << " " << wfts_vec [ idx ].fp_mask << " " << std::endl; } } The output doesn't reflect what I anticipate The fundamental issue surrounds isstringstream but I'm unsure why. Any help appreaciated. An aside: I would have thought I could do this one in my sleep by now. :) Time to brush up on those streams again.
From: LR on 3 Aug 2006 22:30 ma740988 wrote: > The contents of my_file.txt is as follows: > 0,524288,FFF,524288,1000 > 1,624288,F4F,32768,2000 > -1,-1,-1,-1,-1 > > > Now given: > > # include <iostream> > # include <vector> > # include <string> > # include <fstream> > # include <sstream> > > > struct wfts { > int id; > int vp_size ; > int vp_mask; > int fp_size ; > int fp_mask; > > wfts ( > int id, > int vp_size, > int vp_mask_, > int fp_size, > int fp_mask ) > : id ( -1 ) > , vp_size ( 0 ) > , vp_mask ( -1 ) > , fp_size ( 0 ) > , fp_mask ( -1 ) > {} I don't quite understand the above ctor. You seem to pass values, but set everything to constants. Why not wfts() : id(0), vp_size(0), ... etc {} and wfts(int id_arg, int vp_size_arg, ... etc) : id(id_arg), vp_size(vp_size_arg), ... etc {} and a ctor like this wfts(const std::vector<std::string> &f) { // lot's of checking to do here // You may have problems with converting // things like FFF to an int. // You may also want to make sure that // f has the right number of elements. // } and a copy ctor wfts(const wfts &w).... > // perhaps a friend to dump things.. > }; Add some comparison operators (easier to do this in terms of <) bool operator<(const wfts &w1, const wfts &w2) { .... } bool operator!=(const wfts &w1, const wfts &w2) { const bool result = (w1 < w2) || (w2 < w1); return result; } bool operator!=(const wfts &w1, const wfts &w2) { ... } > > > bool > read_file ( std::ifstream& ifs, > std::vector < wfts >& wts_vec ) > { > bool success ( false ); > std::string line; > > wts_vec.clear(); > > //ifs.ignore(); > while ( std::getline( ifs, line, ',' ) ) > { The above line reads input from ifs up to the first ',' and puts it in line. Try something like this. // read a line while(std::getline(ifs,line)) { std::vector<std::string> fields; std::istringstream ifields(line); std::string t; while(std::getline(ifields,t,',')) { fields.push_back(t); } static const wfts wfts_final(-1,-1,-1,-1,-1); const wfts w(fields); if(w != wfts_final) wts_vec.push_back(w); // if the -1,-1... line might not be the last, // then perhaps you want to break? if(w == wfts_final) break; But NB, I haven't tried to compile any of this code, so buggen probably lurk within. LR
|
Pages: 1 Prev: time function Next: Increment/ by C++ |