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

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



On 06/05/2008, H.S. <hs.samix@gmail.com> wrote:
> If you have visited that, it is full of people who want to discuss only the
> standard.

The standard is nice. The standard is great. I love the standard. It
can do everything, and when it can't, then you use Boost who does the
rest.

Wrapping other languages with C++ can be messy, but even standard C++
helps with that. Now, other languages aren't C++, are they?

My answers regarding C++ are almost always going to be to use the
standard library objects and functions and to use them generously,
unless you have an *irrefutable* reason for why you should use
homebrewed subpar methods instead of standard C++. If you're going to
be reading doubles one by one, and you want to store those doubles and
know how many you have, I see little reason to not use an std::list
unless you explain further why you need to keep the data in the ARPACK
format.

Fwiw, there's very little C++ code out there that's really standard
and beautiful. I can think of Battle for Wesnoth as some of the nicest
C++ code out there (and it uses Boost).

On 06/05/2008, H.S. <hs.samix@gmail.com> wrote:
>  Yup, that fscanf method looks interesting. I used that only when I program
> in C, but it might be judicious to use it in C++ in this situation.

It's not. Streams are better and keep you away from nasty errors and segfaults.

Use getline(istream, string).

Suppose ifs is some istream (e.g ifstream ifs("file.data");).

Then you do something like

      string s;
      while(getline(ifs,s)){
          stringstream ss;
          ss << s;
          double x;
          list this_line;
          while(ss >> x){
              // Read the doubles one by one into some data structure
              this_line.push_back(x);
           }
           if(this_line.size() != rows){
              //Handle error here somehow
           }
        }

If you need more fine control than this, you use boost::tokenizer.

HTH,
- Jordi G. H.


Reply to: