[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: [OT] C++ programming: keeping count of data items read from file



James Allsopp wrote:
hi,
Try something like this, just add some pointers;
scan is just a simple object and l is a class vector.
HTH
jim

int nearest::readdata(std::string s, std::vector<scan> & l)
{
   //read in scuba core list
     std::ifstream input(s.c_str());
     std::string temp, pos, x ,y;
     char * t;
     std::cout <<"Reading " << s <<std::endl;
    while(!getline(input,temp).eof())
   {
       scan n;
         std::stringstream s(temp);
       s >> n.name;
       s >> x;
       s >> y;
       n.glon=strtod(x.c_str(),&t);
       n.glat=strtod(y.c_str(),&t);
         l.push_back(n);
   }
   input.close();
   return 0;
}


This is something like I meant when I said do it the C++ way, or the C way. Except that I gave a bad example of the C way, this is a better example, which is of a C++ way. It would help if I knew the C++ iostream stuff better. If this weren't so darned OT, I'd ask what a "scan" is.

(And is also an example of something that is wrong with the C++ standard library, when you need the c_str() member of string so often to get any real useful work done. Kind of defeats the purpose of having string in the first place.)

The totally unhelpful, but trying to be encouraging,

Mark Allums


Reply to: