Redirecting input from file

D

dmoran21

You can misread excellent advice as an insult if you like, but that's
your problem, not mine.

All, I apologize for my behavior...I'm just really frustrated. I
appreciate the help.
 
D

Default User

William said:
A point which I addressed later in my post.

Yeah, yeah. I didn't read the whole thing. The caveat would have been
better up front, but you did mention it.




Brian
 
D

dmoran21

Nope - typo - "stdio.h"

That's what I thought. I got rid of the seg faults, and am trying to
fix an infinite loop...I think I know where the problem is.

Dave
 
B

Ben Bacarisse

<snip rest of sig>

DON'T QUOTE SIGS. I feel justified in shouting this time.
Look, I'm doing the best I can. Not only am I working on this, but
I've got other projects I'm working on and I'm REALLY TRYING to learn.
The insinuation that I'm not is insulting.

I don't think anyone is suggesting that. I _would_ suggest that you are
not working *effectively*. For example, I suggested

while (count < array_length &&
fscanf(fpin, "%ld,%f", &date[count], &precipvals[count]) == 2) {

which solves at least three of your problems at once. If you did not
follow why, you would have been better off asking "why?", "how does
that work?", "what does that mean?". Instead you have re-written the
program with (as it happens) a few more errors.

Pretty much the only reason I post these comments (and I image the
same is true for others) is for the occasional "thanks, it works" or
"ah, I get it now!" reply. To see *all* comments vanish is
disheartening. To get help, you need people to want to reply, and
that means they need to feel there is some point in doing so.
 
D

dmoran21

DON'T QUOTE SIGS. I feel justified in shouting this time.

Well Justified if I may add. Sorry
while (count < array_length &&
fscanf(fpin, "%ld,%f", &date[count], &precipvals[count]) == 2) {
I get most of this, but what does it mean when fscanf == 2?

Thanks
 
R

Richard Heathfield

(e-mail address removed) said:

Ok I found the version with the checks. I thought it'd be easier to
deal with getting it to run and then going back to put in the checks.

The checks are partly there to help you eliminate possibilities as to
what might be going wrong. (The other reason is to give a user of your
production system a clear indication of what has caused some particular
runtime problem, so that maybe he or she can fix the problem without
having to look at the source code.)

So it's actually easier to put the checks in *first*, together with
clear messages:

printf("couldn't open %s for reading\n", argv[1]);

when those checks detect a problem.

That way, you know (from the absence of such messages at runtime) what
is *not* the problem, and that can help you focus on what remains.
 
D

dmoran21

That way, you know (from the absence of such messages at runtime) what
is *not* the problem, and that can help you focus on what remains.

Basically, what I'm doing is taking your version and modifying it.
I've got a segfault and I think I know where it is...Working on it now.
 
B

Ben Bacarisse

while (count < array_length &&
fscanf(fpin, "%ld,%f", &date[count], &precipvals[count]) == 2) {
I get most of this, but what does it mean when fscanf == 2?

It means that two values were read, converted and stored. Your fscanf
can return 0, 1, 2 or EOF. Only in only one of these cases (2) is it
safe to do anything with the data.

It is also only safe to continue to try to read more if your got two
inputs. If fscanf returns 0, 1 the most likely reason is a format
error in the input (maybe "123;1.2" rather than "123,1.2") and trying to
get more will fail (because the ";" is still there waiting for you to
decide what to do with it). fscanf won't ever return EOF in that case
and your loop, as originally written, would never have terminated.
 
D

dmoran21

It means that two values were read, converted and stored. Your fscanf
can return 0, 1, 2 or EOF. Only in only one of these cases (2) is it
safe to do anything with the data.

It is also only safe to continue to try to read more if your got two
inputs. If fscanf returns 0, 1 the most likely reason is a format
error in the input (maybe "123;1.2" rather than "123,1.2") and trying to
get more will fail (because the ";" is still there waiting for you to
decide what to do with it). fscanf won't ever return EOF in that case
and your loop, as originally written, would never have terminated.


Thanks, I'm about to leave for a long weekend. Will let you know how
it goes after I get back and work on it some more.

Dave
 
E

Eric Sosman

Here's the code; it now compiles, but seg faults when I run it. (Error
proofing will be done later)
[...]

At the risk of strewing your path with still more
complications, let me suggest a simplification: You
do *not* need arrays of all the data points and their sums.

For each date,value input pair your program produces
(tries to produce) one line of output. The output line
holds the most recently-read date, the most recently-
read value, and the sum of that value and at most two
others from earlier input pairs. You do not need to
retain the data from ten inputs ago to produce this line
of output; the data from ten inputs ago doesn't even
affect this output line in any way. So why store it?

Instead, you could deal with one date, three values,
and one sum:

float pval1, pval2, pval3;
float psum;
char date[12];
...
pval2 = pval3 = 0;
while (fscanf(fpin, "%12s,&f", date, &pval1) == 2) {
psum = pval1 + pval2 + pval3;
printf(..., date, pval1, psum);
fprintf(..., date, pval1, psum);
pval3 = pval2;
pval2 = pval1;
}
...

Lo! The dynamic allocation and its associated
complications have disappeared, along with the count
variable and its maintenance and testing. Since all
of these have given you trouble thus far, perhaps you'll
see reason to get rid of them and thereby simplify.
 
C

CBFalconer

DON'T QUOTE SIGS. I feel justified in shouting this time.

Well Justified if I may add. Sorry
while (count < array_length &&
fscanf(fpin, "%ld,%f", &date[count], &precipvals[count]) == 2) {

I get most of this, but what does it mean when fscanf == 2?

You mean you are using fscanf without reading its description?
From N869:

[#15] If end-of-file is encountered during input, conversion
is terminated. If end-of-file occurs before any characters
matching the current directive have been read (other than
leading white space, where permitted), execution of the
current directive terminates with an input failure;
otherwise, unless execution of the current directive is
terminated with a matching failure, execution of the
following directive (other than %n, if any) is terminated
with an input failure.

.... snip ...

Returns

[#18] The fscanf function returns the value of the macro EOF
if an input failure occurs before any conversion.
Otherwise, the function returns the number of input items
assigned, which can be fewer than provided for, or even
zero, in the event of an early matching failure.
 
K

Kenny McCormack

Here's the code; it now compiles, but seg faults when I run it. (Error
proofing will be done later)

(Joining the fray late)

Can I ask, just why are you doing this (in C) ?
Given that neither of you (I.e., you & your boss) are programmers, and
seem, quite rightly (and I don't mean anything the least bit negative in
saying this) have no desire to become one:

Why not do this simple task in something like AWK (or Perl or Python or
Ruby, or whatever, depending on how "au courant" you want to be; my
preference is for AWK) ?

From what I can tell, you could do this in 2 or 3 lines of AWK.
Further, AWK is available for all the usual platforms. If you're
working on Unix, it is there automatically; if Windows, you may have to
fire up a browser and execute a dozen or so mouse clicks to get it
running on your machine.
 
K

Kenny McCormack

(Joining the fray late)

Can I ask, just why are you doing this (in C) ?
Given that neither of you (I.e., you & your boss) are programmers, and
seem, quite rightly (and I don't mean anything the least bit negative in
saying this) have no desire to become one:

Why not do this simple task in something like AWK (or Perl or Python or
Ruby, or whatever, depending on how "au courant" you want to be; my
preference is for AWK) ?

From what I can tell, you could do this in 2 or 3 lines of AWK.
Further, AWK is available for all the usual platforms. If you're
working on Unix, it is there automatically; if Windows, you may have to
fire up a browser and execute a dozen or so mouse clicks to get it
running on your machine.

And, to continue, trying to do a simple reports pgm in C is just asking
to run into all the "C gotchas" - seg faults, undefined behavior, etc,
etc, for no good reason, when there are existing tools that have already
taken care of this stuff.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,144
Latest member
KetoBaseReviews
Top