From: Mark Allums on
Jordi Gutiļæ½rrez Hermoso wrote:
> On 06/05/2008, H.S. <hs.samix(a)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(a)lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster(a)lists.debian.org
From: H.S. on
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.




--
To UNSUBSCRIBE, email to debian-user-REQUEST(a)lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster(a)lists.debian.org
From: Kevin B. McCarty on
"H.S." wrote:

> Michael Marsh wrote:

>> 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 "\n" character and then parsing the line. I was just wondering if there was any other way (was trying to avoid parsing).

std::getline() ? I think there is a version that outputs the result as
a string, from which you can then create an istringstream.

You can read numbers from an istringstream the same way as from cin with
">>" operators, so the amount of parsing should be minimal (as long as
the file is not corrupt).

best regards,

--
Kevin B. McCarty <kmccarty(a)gmail.com>
WWW: http://www.starplot.org/
WWW: http://people.debian.org/~kmccarty/
GPG: public key ID 4F83C751


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