Re: [OT] C++ programming: keeping count of data items read from file
Jordi Gutiérrez Hermoso wrote:
On 06/05/2008, H.S. <hs.samix@gmail.com> wrote:
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
From the snippet of code you gave below, it doesn't look necessary to
know how many doubles I want to read before grabbing the whole line in
to a list. In fact, looks like the line will straight away give me how
many double are there in a line and I can compare this number with the
one I expect ... providing me with a sanity check.
unless you explain further why you need to keep the data in the ARPACK
format.
APRACK FORTRAN library needs the input data in a 2D array (the arrays
need to be arranged in column-major format). But, to answer your query,
I don't *have* to read it in an array, I could read it in a list and
then copy it to an array before I call ARPACK routines.
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).
I try to keep is according the C++ standard as far as I can. For all my
programs, I use the "-ansi" flag with g++.
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.
Totally agree about the streams point.
Use getline(istream, string).
Yes, this is what I was just now reading about.
Suppose ifs is some istream (e.g ifstream ifs("file.data");).
The following seems to be quite nice, and "C++ way" to do it. I am going
to try that now. Thanks a ton.
->HS
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: