469,290 Members | 1,882 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,290 developers. It's quick & easy.

Redirecting input from file

Hi All, I am working on a program to take input from a txt file, do
some calculations, and then output the results to another txt file.
The program that I've written compiles fine for me, however, when I
run it, it stalls and does nothing. I'm wondering if there's something
obvious that I'm missing. My code is below and any help would be
appreciated.

Thanks,
Dave

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main(int argc, char *argv[])
{
/* Variable Declarations */
const int arraylength = 5000;
const int errorcode = -1;
float sum = 0;
int count = 0;
int condition = 1;
float* precipvals = (float*)NULL;
float* precip3hrs = (float*)NULL;
long* date = (long*)NULL;
FILE *fpin;
FILE *fpout;

fpin = fopen(argv[1],"r+");
fpout = fopen(argv[2],"w");

/* Array Allocation */
precipvals = (float*)malloc(sizeof(int) * arraylength);
precip3hrs = (float*)malloc(sizeof(int) * arraylength);
date = (long*)malloc(sizeof(int) * arraylength);

/* Calculation Section */
while(condition != EOF) {
condition = fscanf(fpin,"%l,%f", &date[count], &precipvals[count]);
if(feof(fpin)) {
fclose(fpin);
return -3;
}
if(count == 1) {
precip3hrs[count] = precipvals[count];
}
if(count == 2) {
precip3hrs[count] = precipvals[count] + precip3hrs[count-1];
}
if(count 2) {
precip3hrs[count] = precipvals[count] + precipvals[count-1] +
precipvals[count-2];
}
}

/* Output section */
for(count = 1; count <= arraylength; count++) {
if(count == 1) {
printf("One hour precipitation: %2.2f Three hour precipitation %2.2f
\n",precipvals[count],precip3hrs[count]);
}
if(count == 2) {
printf("One hour precipitation: %2.2f Three hour precipitation %2.2f
\n",precipvals[count],precip3hrs[count]);
}
if(count 2) {
printf("One hour precipitation: %2.2f Three hour precipitation %2.2f
\n",precipvals[count],precip3hrs[count]);
}
}

/* Write to file */
for(count = 1; count <= arraylength; count++) {
if(count == 1) {
fprintf(fpout, "One hour precipitation: %2.2f Three hour
precipitation %2.2f\n",precipvals[count],precip3hrs[count]);
}
if(count == 2) {
fprintf(fpout,"One hour precipitation: %2.2f Three hour
precipitation %2.2f\n",precipvals[count],precip3hrs[count]);
}
if(count 2) {
fprintf(fpout,"One hour precipitation: %2.2f Three hour
precipitation %2.2f\n",precipvals[count],precip3hrs[count]);
}
}
fclose(fpin);
fclose(fpout);

/* Deallocation */
free(date);
date = (long*)NULL;
free(precipvals);
precipvals = (float*)NULL;
free(precip3hrs);
precip3hrs = (float*)NULL;
return 0;
}

Jun 28 '07
116 4020
Without #including stdin, these can't work.
So it'd be #include <stdin.h>, correct?

Jul 6 '07 #101
On Jul 6, 11:32 am, "dmora...@cox.net" <dmora...@cox.netwrote:
Without #including stdin, these can't work.

So it'd be #include <stdin.h>, correct?

Nevermind, that's not right. I found it in another book.

Dave

Jul 6 '07 #102
"dm******@cox.net" wrote:
>
Without #including stdin, these can't work.

So it'd be #include <stdin.h>, correct?
Nope - typo - "stdio.h"

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
cbfalconer at maineline dot net
--
Posted via a free Usenet account from http://www.teranews.com

Jul 6 '07 #103

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

Jul 6 '07 #104
On Jul 6, 11:55 am, Ben Bacarisse <ben.use...@bsb.me.ukwrote:
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

Jul 6 '07 #105
dm******@cox.net said:

<snip>
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.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jul 6 '07 #106
On Jul 6, 12:14 pm, Richard Heathfield <r...@see.sig.invalidwrote:
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.

Jul 6 '07 #107
CBFalconer wrote On 07/06/07 12:47,:
"dm******@cox.net" wrote:
>>>Without #including stdin, these can't work.

So it'd be #include <stdin.h>, correct?


Nope - typo - "stdio.h"
Nope - typo squared - <stdio.h>, as in

#include <stdio.h>

--
Er*********@sun.com
Jul 6 '07 #108
On Jul 6, 12:31 pm, Eric Sosman <Eric.Sos...@sun.comwrote:
>
#include <stdio.h>
Got that.

Dave

Jul 6 '07 #109
"dm******@cox.net" <dm******@cox.netwrites:
On Jul 6, 11:55 am, Ben Bacarisse <ben.use...@bsb.me.ukwrote:
> 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.

--
Ben.
Jul 6 '07 #110
On Jul 6, 12:58 pm, Ben Bacarisse <ben.use...@bsb.me.ukwrote:
"dmora...@cox.net" <dmora...@cox.netwrites:
On Jul 6, 11:55 am, Ben Bacarisse <ben.use...@bsb.me.ukwrote:
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

Jul 6 '07 #111
dm******@cox.net wrote On 07/06/07 10:30,:
>
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.

--
Er*********@sun.com
Jul 6 '07 #112
Eric Sosman wrote On 07/06/07 14:22,:
[...]
while (fscanf(fpin, "%12s,&f", date, &pval1) == 2) {
"%12s,%f", of course. It's a good day for typos.

--
Er*********@sun.com
Jul 6 '07 #113
"dm******@cox.net" wrote:
On Jul 6, 11:55 am, Ben Bacarisse <ben.use...@bsb.me.ukwrote:
>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.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

Jul 6 '07 #114
In article <11*********************@n60g2000hse.googlegroups. com>,
dm******@cox.net <dm******@cox.netwrote:
>On Jul 5, 4:50 pm, "David Moran" <dmora...@NOSPAMcox.netwrote:
What was the actual error message? I don't recall seeing it
mentioned in this thread.

I'd have to look tomorrow when I get to the office.


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.

Jul 15 '07 #115
In article <f7**********@news.xmission.com>,
Kenny McCormack <ga*****@xmission.xmission.comwrote:
>In article <11*********************@n60g2000hse.googlegroups. com>,
dm******@cox.net <dm******@cox.netwrote:
>>On Jul 5, 4:50 pm, "David Moran" <dmora...@NOSPAMcox.netwrote:
>What was the actual error message? I don't recall seeing it
mentioned in this thread.

I'd have to look tomorrow when I get to the office.


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

Jul 15 '07 #116
On Jul 16, 10:17 am, gaze...@xmission.xmission.com (Kenny McCormack)
wrote:
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.
Agree in general, although if you are already good
at C then there is nothing wrong with using C for
the task. Some of the advantages of C over AWK are
a faster execution time (no need to invoke a new
copy of the shell), and it is not prone to break
when run in different versions of awk (awk? nawk?
oawk? gawk? mawk?)

Jul 15 '07 #117

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Christophe HELFER | last post: by
6 posts views Thread by Christophe Helfer | last post: by
3 posts views Thread by Jan Danielsson | last post: by
1 post views Thread by Gaijinco | last post: by
3 posts views Thread by Sudesh | last post: by
8 posts views Thread by Morpheus | last post: by
13 posts views Thread by =?Utf-8?B?Um9iS2lubmV5MQ==?= | last post: by
omerbutt
8 posts views Thread by omerbutt | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.