473,322 Members | 1,523 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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 4481
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Christophe HELFER | last post by:
hi, I have some problem with redirecting input and output from a process. I can only use VB language (sorry...) Situation: I have to use the Cisco Network Registrar (DNS And DHCP server) ...
6
by: Christophe Helfer | last post by:
hi, I have some problem with redirecting input and output from a process. Situation: I have to use the Cisco Network Registrar (DNS And DHCP server) command line utility as redirecting its...
3
by: Jan Danielsson | last post by:
Hello, I thought I'd write a program to collect information from pf (packet filter) and insert it into a postgresql database for review on a web page. First I checked if this has been done already,...
1
by: Gaijinco | last post by:
Hi! I was coding something which had some lines like these: int main(){ int n; scanf("%d",&n); fflush(stdin); int* ans = new int; int idx=0;
3
by: Sudesh | last post by:
Hi, I am a newbie to C# and Im trying to redirect standard input, output and error of a console program written in C (MS VC 6.0) to a textbox on a form. The code for the redirecting looks like...
8
by: Morpheus | last post by:
I am trying to test a function that outputs text to the standard output, presumably using a cout call. I do not have access to the source, but need to test the output. Is this possible? I can...
13
by: =?Utf-8?B?Um9iS2lubmV5MQ==?= | last post by:
Hello, Here is a head scratcher for you all: We were wondering if it is possible to redirect file input to a place in memory (say a byte array for example). More specifically, if a function...
8
omerbutt
by: omerbutt | last post by:
hi there i a making a community page where i am editing the details for the communities added in the favourities of the user the, i am using html ajax javascript,php and mysql.Now the problem is that...
6
by: rpcchandu | last post by:
Hi Coders, I have to redirect from my server to the different server page by simulating the POST method submit from the Controller file, I tried using post_via_redirect, but could not succeed......
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.