FAQ Search Today's Posts Mark Forums Read
» Video Reviews

» Linux Archive

Linux-archive is a website aiming to archive linux email lists and to make them easily accessible for linux users/developers.


» Sponsor

» Partners

» Sponsor

Go Back   Linux Archive > Debian > Debian User

 
 
LinkBack Thread Tools
 
Old 05-06-2008, 06:39 PM
Hal Vaughan
 
Default C++ programming: keeping count of data items read from file

On Tuesday 06 May 2008, H.S. wrote:
> Ron Johnson wrote:
> > Is this a binary file or a text file?
>
> hmm. Text. I made it clear in the original post.

Ron has trouble keeping up with things like that. It's so hot where he
lives his brain is often overheated with the lest bit of mental effort.

Hal


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
 
Old 05-06-2008, 06:52 PM
"H.S."
 
Default C++ programming: keeping count of data items read from file

Hal Vaughan wrote:

On Tuesday 06 May 2008, H.S. wrote:

Ron Johnson wrote:

Is this a binary file or a text file?

hmm. Text. I made it clear in the original post.


Ron has trouble keeping up with things like that. It's so hot where he
lives his brain is often overheated with the lest bit of mental effort.


Hal


Thats okay. It is not a problem here. But had he committed this
sacrilege in a newsgroups full of Linux "gurus" (you know, the RTFM type
Linux/Unix puritans), he would have been flamed to cinders.





--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
 
Old 05-06-2008, 07:10 PM
Hal Vaughan
 
Default C++ programming: keeping count of data items read from file

On Tuesday 06 May 2008, H.S. wrote:
> Hal Vaughan wrote:
> > On Tuesday 06 May 2008, H.S. wrote:
> >> Ron Johnson wrote:
> >>> Is this a binary file or a text file?
> >>
> >> hmm. Text. I made it clear in the original post.
> >
> > Ron has trouble keeping up with things like that. It's so hot
> > where he lives his brain is often overheated with the lest bit of
> > mental effort.
> >
> > Hal
>
> Thats okay. It is not a problem here. But had he committed this
> sacrilege in a newsgroups full of Linux "gurus" (you know, the RTFM
> type Linux/Unix puritans), he would have been flamed to cinders.

Interesting way to put it, since he is a puritan guru. (Or Linux guru,
or puritan Linux guru -- I'm not sure which word should modify which
here!)

Hal


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
 
Old 05-06-2008, 07:30 PM
James Allsopp
 
Default C++ programming: keeping count of data items read from file

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;
}

Hal Vaughan wrote:

On Tuesday 06 May 2008, H.S. wrote:


Hal Vaughan wrote:


On Tuesday 06 May 2008, H.S. wrote:


Ron Johnson wrote:


Is this a binary file or a text file?


hmm. Text. I made it clear in the original post.


Ron has trouble keeping up with things like that. It's so hot
where he lives his brain is often overheated with the lest bit of
mental effort.

Hal


Thats okay. It is not a problem here. But had he committed this
sacrilege in a newsgroups full of Linux "gurus" (you know, the RTFM
type Linux/Unix puritans), he would have been flamed to cinders.



Interesting way to put it, since he is a puritan guru. (Or Linux guru,
or puritan Linux guru -- I'm not sure which word should modify which
here!)


Hal






--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
 
Old 05-06-2008, 07:52 PM
"Jordi Gutiérrez Hermoso"
 
Default 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.


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
 
Old 05-06-2008, 08:57 PM
Mark Allums
 
Default 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


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
 
Old 05-06-2008, 09:01 PM
Ron Johnson
 
Default C++ programming: keeping count of data items read from file

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 05/06/08 13:25, H.S. wrote:
> Ron Johnson wrote:
>>
>> Is this a binary file or a text file?
>
> hmm. Text. I made it clear in the original post.

Sorry. It just seems (to an old C programmer) that this is pretty
simple problem, unless there's some tricky detail that you aren't
telling us.

- --
Ron Johnson, Jr.
Jefferson LA USA

We want... a Shrubbery!!
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)

iD8DBQFIIMcsS9HxQb37XmcRAhs6AKDDQ8KXvkspc5xLZoj9l2 9TrnvV6gCcCUO0
ZggE4aBh+kOQ0/gP712BRVM=
=A44m
-----END PGP SIGNATURE-----


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
 
Old 05-06-2008, 09:01 PM
"H.S."
 
Default 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.





--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
 
Old 05-06-2008, 09:13 PM
"H.S."
 
Default C++ programming: keeping count of data items read from file

Mark Allums wrote:

(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.)


Yes, that c_str() is a nuisance many times. I remember a few years back
when older C++ code refused to compile with a newer gcc version (there
are a major version change in gcc back then) and I had to manually put
that .c_str() at many places in a source that I was using.




The totally unhelpful, but trying to be encouraging,

Mark Allums





--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
 
Old 05-06-2008, 09:47 PM
Mark Allums
 
Default 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:

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.




Hmmmm... Well, fscanf() *is* evil. And subject possibly to buffer
overflows. But understandable to an old fart. Who still has _A Book
on C_ and Kernighan and Ritchie lying around somewhere. (And should
have looked it up to see if it was really suitable.)


Streams *are* better. But I would rather statically link to stdio than
to iostream, if the subject ever comes up, and the choice has to be made.


Fortunately, I don't have to write this code anymore. So I can
cheerfully forget how it works, and post red herrings while kibitzing.
(I could claim that I thought of it, but just didn't post an example
because I couldn't think of the getline() function. But I won't make
that claim, because it wouldn't be believed.)



Mark Allums,

Who mostly writes code in lua these days, when he writes any at all.


--
Mark Allums


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
 

Thread Tools




All times are GMT. The time now is 07:34 AM.

VBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2007 - 2008, www.linux-archive.org