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, 05:42 PM
"Robert Baron"
 
Default C++ programming: keeping count of data items read from file

On Tue, May 6, 2008 at 1:14 PM, H.S. <hs.samix@gmail.com> wrote:

Jordi Gutiérrez Hermoso wrote:


On 06/05/2008, H.S. <hs.samix@gmail.com> wrote:


In a C++ program I am reading a data file for later processing and

computations. While reading that data file, I want to keep track of data

items (doubles) read.




Use std::list and other standard data structures.




I am using them where ever I can. In this case however, I am using the data with ARPACK libraries and need to put it in a FORTRAN style 2D array. Everywhere else though, I am using C++ STL.




It's C++. Don't use C arrays, ugh.



If you are comfortable with IRC, I find that ##c++ on Freenode is full

of bright people.



And what does this have to do with Debian?




er .. ahem .. yeah, sorry about that, should have had OT in the subject. But now that you mention this, let me explain a bit why I did not post it in C++ newsgroup. If you have visited that, it is full of people who want to discuss only the standard. I have had that experience, so gave it a shot here.




Will try the IRC. Thanks,

->HS

PS: I just put "OT" in the subject line.What is so terrible about counting the items as they come in?
The other way to do it is to count up the items in the 2d array (and it shouldn't matter if it is ..., well whatever).Trivial.
Rob.
 
Old 05-06-2008, 05:50 PM
"H.S."
 
Default C++ programming: keeping count of data items read from file

Robert Baron wrote:



What is so terrible about counting the items as they come in?



As I mentioned earlier, the issue is how do I count items read in one
line, or before the next EOL? Counting total items is not a problem.


Perhaps a different way to say this is, how do I detect if I have
reached an EOL while reading doubles from a file stream.





--
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:06 PM
"Michael Marsh"
 
Default C++ programming: keeping count of data items read from file

On Tue, May 6, 2008 at 1:50 PM, H.S. <hs.samix@gmail.com> wrote:
> As I mentioned earlier, the issue is how do I count items read in one line,
> or before the next EOL? Counting total items is not a problem.
>
> Perhaps a different way to say this is, how do I detect if I have reached
> an EOL while reading doubles from a file stream.

Can you read full lines out into, eg, a stringstream, and parse your
doubles out of that? You'd hit an EOF at the end of each line in that
case. I'm not sure how you'd get stream out line-at-a-time, though
there may be a stream operator that sets the appropriate behavior.

Just a random idea off the top of my head.

--
Michael A. Marsh
http://www.umiacs.umd.edu/~mmarsh
http://mamarsh.blogspot.com
http://36pints.blogspot.com


--
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:06 PM
Ron Johnson
 
Default C++ programming: keeping count of data items read from file

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

On 05/06/08 12:50, H.S. wrote:
> Robert Baron wrote:
>
>>
>> What is so terrible about counting the items as they come in?
>
>
> As I mentioned earlier, the issue is how do I count items read in one
> line, or before the next EOL? Counting total items is not a problem.
>
> Perhaps a different way to say this is, how do I detect if I have
> reached an EOL while reading doubles from a file stream.

Is this a binary file or a text file?

- --
Ron Johnson, Jr.
Jefferson LA USA

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

iD8DBQFIIJ4uS9HxQb37XmcRAjSqAKCLE6X+D5GA0GbuHl04Ju hDVMQ1SACfcYzk
3/cqC9ntXmxUBk8Y2SuLCQ8=
=uwVN
-----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, 06:20 PM
Mark Allums
 
Default C++ programming: keeping count of data items read from file

H.S. wrote:

Hello,

In a C++ program I am reading a data file for later processing and
computations. While reading that data file, I want to keep track of data
items (doubles) read.


The data file is just a text file with N lines with C doubles in each
line (N and C are known a priori). For now, I just read from the file
stream in to a 2D array variable by reading each double at a time.


Now I am trying to introduce some sanity checking into this reading
block. Here is what I am trying to do:
1. Verify how many doubles I have read in each line. Must be C. If they
are not C, then the input file is corrupt.
2. Verify that the total number of data items are NxC. This is simple, I
just keep a track of how many numbers I have read.


So, how do I go about doing (1) above? I was thinking of somehow
checking if I have reached the end of line somehow (EOL?) but haven't
found a method to do so. All I have found is EOF.


thanks,
->HS




Not directly helpful, but some suggestions:

1. You might want to learn PERL or Python or Ruby, and do it there.
2. If it has to be C++, learn enough PERL to write a filter for the data
file, and transform it so that it has one double per line.
3. Debug the data generator /in situ/ with a good debugger, and bypass
the need to do the sanity checking.

4. Find a good C++ reference, and use it. There are several.

Slightly more helpful:

1. Read one line at a time in as a string, then operate on the string.
2. C++ has the ability to do everything that C does in a low level way,
but why? Use the C++ way, or use the C way:


#include <cstdio>
#include <iostream>
. . .
using namespace std;
. . .
ios::sync_with stdio();
. . .
int blah = fscanf(somefile,"%f %f %f %f
", d1,d2,d3,d4);
if (blah != correctvalue)
{
dosomething();
closefiles();
cout << "error in data file
";
exit(1);
}
. . .
// etc.


(The ios::sync_with_stdio(); line may differ slightly on different C++
implementations. I haven't used it in a while. May be spelled synch_.
Too lazy to look it up.


The fscanf line may just be wrong. I quit writing C programs years ago.
Too old, memory failing.)




--
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, 06:23 PM
"H.S."
 
Default C++ programming: keeping count of data items read from file

Michael Marsh wrote:

On Tue, May 6, 2008 at 1:50 PM, H.S. <hs.samix@gmail.com> wrote:

As I mentioned earlier, the issue is how do I count items read in one line,
or before the next EOL? Counting total items is not a problem.

Perhaps a different way to say this is, how do I detect if I have reached
an EOL while reading doubles from a file stream.


Can you read full lines out into, eg, a stringstream, and parse your
doubles out of that? You'd hit an EOF at the end of each line in that
case. I'm not sure how you'd get stream out line-at-a-time, though
there may be a stream operator that sets the appropriate behavior.


Yup, that could be done by getting a line till the end of "
" character
and then parsing the line. I was just wondering if there was any other
way (was trying to avoid parsing).





Just a random idea off the top of my head.




--
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:25 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:
> > -----BEGIN PGP SIGNED MESSAGE-----
> > Hash: SHA1
> >
> > On 05/06/08 11:42, H.S. wrote:
> >> Hello,
> >>
> >> In a C++ program I am reading a data file for later processing and
> >> computations. While reading that data file, I want to keep track
> >> of data items (doubles) read.
> >>
> >> The data file is just a text file with N lines with C doubles in
> >> each line (N and C are known a priori). For now, I just read from
> >> the file stream in to a 2D array variable by reading each double
> >> at a time.
> >>
> >> Now I am trying to introduce some sanity checking into this
> >> reading block. Here is what I am trying to do:
> >> 1. Verify how many doubles I have read in each line. Must be C. If
> >> they are not C, then the input file is corrupt.
> >> 2. Verify that the total number of data items are NxC. This is
> >> simple, I just keep a track of how many numbers I have read.
> >>
> >> So, how do I go about doing (1) above? I was thinking of somehow
> >> checking if I have reached the end of line somehow (EOL?) but
> >> haven't found a method to do so. All I have found is EOF.
> >
> > This smells suspiciously like CompSci homework.
>
> Nope, it isn't. The program implements an algorithm in my research. I
> have the program running, but now I am also generating the data file
> automatically. The above request is to catch any errors creeping in
> to the data file due to the new program I am writing to generate it.

How are you generating the data file? Can't you output a specific
character before each EOL char so you can search for it?

If you're generating your own file, can't you use serialization? (Or am
I confusing that with Java, forgot which does that.)

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:25 PM
"H.S."
 
Default C++ programming: keeping count of data items read from file

Ron Johnson wrote:


Is this a binary file or a text file?


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


--
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:27 PM
Hal Vaughan
 
Default C++ programming: keeping count of data items read from file

On Tuesday 06 May 2008, Ron Johnson wrote:
> On 05/06/08 12:50, H.S. wrote:
> > Robert Baron wrote:
> >> What is so terrible about counting the items as they come in?
> >
> > As I mentioned earlier, the issue is how do I count items read in
> > one line, or before the next EOL? Counting total items is not a
> > problem.
> >
> > Perhaps a different way to say this is, how do I detect if I have
> > reached an EOL while reading doubles from a file stream.
>
> Is this a binary file or a text file?

Text file. He already specified that.


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:29 PM
"H.S."
 
Default C++ programming: keeping count of data items read from file

Mark Allums wrote:



Not directly helpful, but some suggestions:

1. You might want to learn PERL or Python or Ruby, and do it there.


hmm .. not sure how this will pan out in the long run, but for now, I am
trying to keep it all within one program.


2. If it has to be C++, learn enough PERL to write a filter for the data
file, and transform it so that it has one double per line.
3. Debug the data generator /in situ/ with a good debugger, and bypass
the need to do the sanity checking.


Totally agree with this. The sanity checking was mainly for the data
files when some other user may use at a later date.




4. Find a good C++ reference, and use it. There are several.

Slightly more helpful:

1. Read one line at a time in as a string, then operate on the string.
2. C++ has the ability to do everything that C does in a low level way,
but why? Use the C++ way, or use the C way:


#include <cstdio>
#include <iostream>
. . .
using namespace std;
. . .
ios::sync_with stdio();
. . .
int blah = fscanf(somefile,"%f %f %f %f
", d1,d2,d3,d4);
if (blah != correctvalue)
{
dosomething();
closefiles();
cout << "error in data file
";
exit(1);
}
. . .
// etc.


(The ios::sync_with_stdio(); line may differ slightly on different C++
implementations. I haven't used it in a while. May be spelled synch_.
Too lazy to look it up.


The fscanf line may just be wrong. I quit writing C programs years ago.
Too old, memory failing.)


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.


Never mind the syntax accuracy, I can fix that if need be. Looks like I
may need to give this approach shot. Thanks.


->HS




--
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 02:38 PM.

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