473,406 Members | 2,816 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,406 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 #1
116 4494
dm******@cox.net wrote On 06/28/07 11:30,:
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.
There are several problems, and perhaps more than I've
spotted in a quick scan. I've noted a few of them below:
#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;
The casts are harmless, but unnecessary.
FILE *fpin;
FILE *fpout;

fpin = fopen(argv[1],"r+");
Why "r+" instead of just "r", since all you do is
read from the file? Also, you should check the values
returned by both fopen() calls to see if fopen() failed.
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);
More unnecessary but harmless casts. A worse problem
is that you don't check any of the malloc() calls for
failure. Worst of all is that you request space for
so-and-so many ints, but the things you're actually going
to store are floats and longs!
>
/* Calculation Section */
while(condition != EOF) {
condition = fscanf(fpin,"%l,%f", &date[count], &precipvals[count]);
if(feof(fpin)) {
fclose(fpin);
return -3;
That's a pretty weird exit value. It may mean something
on your system, but if it does the meaning is far from
universal. The only "portable" exit values are EXIT_SUCCESS
and zero (both meaning "success") and EXIT_FAILURE.
}
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];
}
Since count was initialized to zero and there's nothing
to change its value, this loop just reads and reads and reads
and does nothing. Also, if fscanf() ever runs across something
it can't interpret, it'll return the number of items it was
able to convert (zero or one) and leave the unintelligible
junk still un-read. On the next call it will still be unable
to convert the junk, so it will return zero and leave the junk
un-read. On the next call it will still be unable ...

The upshot: If there's anything wrong anywhere in the input,
this loop will never finish. If the input data is perfectly
formatted, this loop will read all the way through the file
and do essentially nothing. When (if) it reaches the end of
the input, feof(pin) will return true and the program will
exit.
}
As far as I can see, there's no way for the program to
arrive at this point. But if it did ...
/* Output section */
for(count = 1; count <= arraylength; count++) {
On the final trip through this loop, count will equal
arraylength and you'll try to use the [arraylength] elements
of your arrays. Since you've only allocated space for [0]
through [arraylength-1] (if that; see above), that'll be
a problem.

Also, the only array elements that have ever had anything
stored in them are the [0] elements of date and precipvals.
Nothing has ever been stored in the higher-numbered elements
of those two arrays, nor in any element of precip3hrs.

Finally, assuming you fix up the input loop so it actually
does something useful, what happens if the input has 3000
lines instead of 5000? (Or worse: What if it has 6000?)
Do you really want to loop through the entire array, or just
through the array elements that correspond to inputs?
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++) {
Same remarks as for the previous loop.
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);
Believe it or not, fclose() can fail. Failure of the
first one probably doesn't hurt your program any, but if
the second fails it very likely means that some part of the
output file didn't get written. Do you want to proclaim
"Success!" if that happens?
/* Deallocation */
free(date);
date = (long*)NULL;
free(precipvals);
precipvals = (float*)NULL;
free(precip3hrs);
precip3hrs = (float*)NULL;
More useless casts. (Useless assignments, too, IMHO.)
return 0;
}
--
Er*********@sun.com
Jun 28 '07 #2
AG
Hi Dave, some hints below :
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);
check if malloc returned a NULL pointer.
>
/* Calculation Section */
while(condition != EOF) {
condition = fscanf(fpin,"%l,%f", &date[count], &precipvals[count]);
Could you show us the file ? It seems that count is never incremented
and always equal 1 ?
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 == arraylength, then you do an overflow. You can write inside
precipvals using index [0 to arrayval-1].
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]);
}
same comment about overflows.
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 #3
dm******@cox.net said:
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.
I've made some obvious corrections - removed all the casts, added a bit
of error checking, fixed a couple of broken loop bounds...

But I can't test because I don't have the data that you have.

So here's the semi-corrected code. I'm not saying it fixes your problem,
but you might find that it at least prints out a complaint at runtime,
in which case it would be useful to know which complaint it prints.

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

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

if(argc 2)
{
fpin = fopen(argv[1], "r+");
if(fpin != NULL)
{
fpout = fopen(argv[2], "w");
if(fpout != NULL)
{
/* Array Allocation */
precipvals = malloc(arraylength *
sizeof *precipvals);
if(precipvals != NULL)
{
precip3hrs = malloc(arraylength *
sizeof *precip3hrs);
if(precip3hrs != NULL)
{
date = malloc(arraylength *
sizeof *date);
if(date != NULL)
{
/* Calculation Section */
while(condition != EOF)
{
condition =
fscanf(fpin,
"%ld,%f",
&date[count],
&precipvals[count]);
if(feof(fpin))
{
fclose(fpin);
fprintf(stderr,
"Last datum read, program closing now.\n");
return EXIT_FAILURE;
}
if(count == 1)
{
precip3hrs[count] = precipvals[count];
}
else if(count == 2)
{
precip3hrs[count] =
precipvals[count] + precip3hrs[count - 1];
}
else 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]);
}
else if(count == 2)
{
printf
("One hour precipitation: %2.2f"
" Three hour precipitation %2.2f\n",
precipvals[count],
precip3hrs[count]);
}
else 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]);
}
}
free(date);
}
else
{
fprintf(stderr, "Allocation failure: date\n");
}
free(precip3hrs);
}
else
{
fprintf(stderr, "Allocation failure: precip3hrs\n");
}
free(precipvals);
}
else
{
fprintf(stderr, "Allocation failure: precipvals\n");
}
fclose(fpout);
}
else
{
fprintf(stderr, "Can't open %s for writing.\n", argv[2]);
}
fclose(fpin);
}
else
{
fprintf(stderr, "Can't open %s for reading\n", argv[1]);
}
}
else
{
fprintf(stderr, "Need more args.\n");
}

return 0;
}


--
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
Jun 28 '07 #4
On Jun 28, 11:29 am, Richard Heathfield <r...@see.sig.invalidwrote:
dmora...@cox.net said:
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.

I've made some obvious corrections - removed all the casts, added a bit
of error checking, fixed a couple of broken loop bounds...

But I can't test because I don't have the data that you have.

So here's the semi-corrected code. I'm not saying it fixes your problem,
but you might find that it at least prints out a complaint at runtime,
in which case it would be useful to know which complaint it prints.

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

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

if(argc 2)
{
fpin = fopen(argv[1], "r+");
if(fpin != NULL)
{
fpout = fopen(argv[2], "w");
if(fpout != NULL)
{
/* Array Allocation */
precipvals = malloc(arraylength *
sizeof *precipvals);
if(precipvals != NULL)
{
precip3hrs = malloc(arraylength *
sizeof *precip3hrs);
if(precip3hrs != NULL)
{
date = malloc(arraylength *
sizeof *date);
if(date != NULL)
{
/* Calculation Section */
while(condition != EOF)
{
condition =
fscanf(fpin,
"%ld,%f",
&date[count],
&precipvals[count]);
if(feof(fpin))
{
fclose(fpin);
fprintf(stderr,
"Last datum read, program closing now.\n");
return EXIT_FAILURE;
}
if(count == 1)
{
precip3hrs[count] = precipvals[count];
}
else if(count == 2)
{
precip3hrs[count] =
precipvals[count] + precip3hrs[count - 1];
}
else 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]);
}
else if(count == 2)
{
printf
("One hour precipitation: %2.2f"
" Three hour precipitation %2.2f\n",
precipvals[count],
precip3hrs[count]);
}
else 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]);
}
}
free(date);
}
else
{
fprintf(stderr, "Allocation failure: date\n");
}
free(precip3hrs);
}
else
{
fprintf(stderr, "Allocation failure: precip3hrs\n");
}
free(precipvals);
}
else
{
fprintf(stderr, "Allocation failure: precipvals\n");
}
fclose(fpout);
}
else
{
fprintf(stderr, "Can't open %s for writing.\n", argv[2]);
}
fclose(fpin);
}
else
{
fprintf(stderr, "Can't open %s for reading\n", argv[1]);
}
}
else
{
fprintf(stderr, "Need more args.\n");
}

return 0;

}

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

Thanks for the help. I'll give it a go.

Dave

Jun 28 '07 #5
dm******@cox.net said:
Thanks for the help. I'll give it a go.
When you do, take note of the comments others have made in this thread,
especially regarding the loop counter! You'll need to fix that before
you can find the other bugs.

--
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
Jun 28 '07 #6
On Jun 28, 11:21 am, "AG" <a...@tb.frwrote:
Hi Dave, some hints below :
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);

check if malloc returned a NULL pointer.
/* Calculation Section */
while(condition != EOF) {
condition = fscanf(fpin,"%l,%f", &date[count], &precipvals[count]);

Could you show us the file ? It seems that count is never incremented
and always equal 1 ?
I fixed the increment. I discovered this when my boss and I were
looking at it today in my office.
>

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 == arraylength, then you do an overflow. You can write inside
precipvals using index [0 to arrayval-1].
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]);
}

same comment about overflows.
That's what I thought. My co worker said I needed the <=, I originally
just had <
>
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;
}
Thanks for all the tips. I'm at home now, but will check it out when I
get to the office in the morning.

Dave

Jun 28 '07 #7
On Thu, 28 Jun 2007 16:29:55 +0000, Richard Heathfield wrote:

if(argc 2) ...
if(fpin != NULL) ...
if(fpout != NULL) ...
if(precipvals != NULL) ...
if(precip3hrs != NULL) ...
if(date != NULL) ...
while(condition != EOF) ...
if(feof(fpin)) ...
if(count == 1) ...
else if(count == 2) ...
else if(count 2) ...
for(count = 1; count < arraylength; count++) ...
if(count == 1) ...
else if(count == 2) ...
else if(count 2) ...
for(count = 1; count < arraylength; count++) ...
if(count == 1) ...
if(count == 2) ...
if(count 2) ...
else ...
else ...
else ...
else ...
else ...
else ...

Are you serious? 160 lines of nested if/else and loops in one
function?
--
Roland Pibinger
"The best software is simple, elegant, and full of drama" - Grady Booch
Jun 28 '07 #8
Roland Pibinger said:

<snip>
Are you serious? 160 lines of nested if/else and loops in one
function?
Minimal edits consistent with adding the error checking that the OP
lacked. I wasn't about to refactor it for him as well - he can do that
himself, surely?

--
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
Jun 28 '07 #9
On Thu, 28 Jun 2007 09:56:02 -0700, "dm******@cox.net"
<dm******@cox.netwrote:
snip
>Thanks for the help. I'll give it a go.
Please don't quote 200 lines just to say thanks.
Remove del for email
Jun 29 '07 #10
dm******@cox.net wrote:
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.
I'm going to make style points fir which other people have other
aesthetics.
#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;
(a) You don't need to cast the NULL, so you shouldn't.

(b) You're going to assign to these two variables unconditionally
below, so there's no point in assigning null to them at all,
/especially/ since you're happy with initialised declarations.

Just do

float* precipvals = malloc( sizeof(int) * arraylength );
float* precip3hrs = malloc( sizeof(int) * arraylength );

(c) You don't need the casts on the `malloc` result, so (as you
see in (b) you can [and I would say /should/] remove them.

(d) You should check that `malloc` has succeeded.
long* date = (long*)NULL;
FILE *fpin;
FILE *fpout;

fpin = fopen(argv[1],"r+");
fpout = fopen(argv[2],"w");
(d) You might as well combine the declaration with the assignment.

(e) You should check that the `fopen`s succeed.

(f) In fact, since the files failing to fopen is death to your code
anyway, I'd suggest doing the fopens /first/, and structuring
your code like:

FILE *fpin = fopen(argv[1],"r+");
FILE *fpout = fopen(argv[2],"w");
if (fpin && fpout)
callFunctionThatDoesTheStuffWithTheFiles( fpin, fpout );
else
{
... code that handles files failing to fopen
}
if (fpin) close( fpin );
if (fpout) close( fpout );

That concentrates all the file opening-and-closing-and-complaining
in a short span of text.
/* Calculation Section */
Make the (snipped) code here a function with a nice name.
/* Output section */
Ditto.
/* Write to file */
Ditto.
/* Deallocation */
free(date);
date = (long*)NULL;
free(precipvals);
precipvals = (float*)NULL;
free(precip3hrs);
precip3hrs = (float*)NULL;
The assignments of null are unnecessary and distracting.
return 0;
}
--
Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England

Jun 29 '07 #11

"Richard Heathfield" <rj*@see.sig.invalidha scritto nel messaggio news:go*********************@bt.com...
dm******@cox.net said:
>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.

I've made some obvious corrections - removed all the casts, added a bit
of error checking, fixed a couple of broken loop bounds...

But I can't test because I don't have the data that you have.

So here's the semi-corrected code. I'm not saying it fixes your problem,
but you might find that it at least prints out a complaint at runtime,
in which case it would be useful to know which complaint it prints.

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

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

if(argc 2)
{
fpin = fopen(argv[1], "r+");
if(fpin != NULL)
{
fpout = fopen(argv[2], "w");
if(fpout != NULL)
{
/* Array Allocation */
precipvals = malloc(arraylength *
sizeof *precipvals);
if(precipvals != NULL)
{
precip3hrs = malloc(arraylength *
sizeof *precip3hrs);
if(precip3hrs != NULL)
{
date = malloc(arraylength *
sizeof *date);
if(date != NULL)
{
/* Calculation Section */
while(condition != EOF)
{
condition =
fscanf(fpin,
"%ld,%f",
&date[count],
&precipvals[count]);
if(feof(fpin))
{
fclose(fpin);
fprintf(stderr,
"Last datum read, program closing now.\n");
return EXIT_FAILURE;
}
if(count == 1)
{
precip3hrs[count] = precipvals[count];
}
else if(count == 2)
{
precip3hrs[count] =
precipvals[count] + precip3hrs[count - 1];
}
else 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]);
}
else if(count == 2)
{
printf
("One hour precipitation: %2.2f"
" Three hour precipitation %2.2f\n",
precipvals[count],
precip3hrs[count]);
}
else 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]);
}
}
free(date);
}
else
{
fprintf(stderr, "Allocation failure: date\n");
}
free(precip3hrs);
}
else
{
fprintf(stderr, "Allocation failure: precip3hrs\n");
}
free(precipvals);
}
else
{
fprintf(stderr, "Allocation failure: precipvals\n");
}
fclose(fpout);
}
else
{
fprintf(stderr, "Can't open %s for writing.\n", argv[2]);
}
fclose(fpin);
}
else
{
fprintf(stderr, "Can't open %s for reading\n", argv[1]);
}
}
else
{
fprintf(stderr, "Need more args.\n");
}

return 0;
}
Apparently this will *always* return 0, no matter what happens...
Why do you think that having umpteen nested if statements is any
less bad than ever calling exit(EXIT_FAILURE)? I remember about
your double ctx2174_frob_foo(foo *f, bar *b, baz *z, quux *q), but
this isn't much clearer...
Jun 29 '07 #12
Chris Dollin wrote On 06/29/07 04:10,:
[...]

float* precipvals = malloc( sizeof(int) * arraylength );
^^^^^ ^^^
float* precip3hrs = malloc( sizeof(int) * arraylength );
^^^^^ ^^^

Avoiding this error is one of the chief virtues of
the c.l.c. Orthodox Allocation Idiom:

AnyType *ptr = malloc(sizeof *ptr * arraylength);

--
Er*********@sun.com
Jun 29 '07 #13
Army1987 said:

<huge snip, which was really your job, not mine>
Apparently this will *always* return 0, no matter what happens...
Yeah. I said I fixed *some* of his problems, not all of them. The code
needs refactoring[1]. I am prepared to spend a considerable amount of
my time helping people out, for free, but I get to decide where I stop.
And on this occasion I chose to stop at the point where I'd stripped
out pointless casts, added a small handful of tests, and put the
free()s and fclose()s in the right places. Oh, and I corrected a couple
of loop termination conditions.

I didn't look closer at the code than that, but then I didn't claim that
I'd fixed his problem, either.

I did suggest that this might give better diagnostic information (which
is true in the long term, but it turns out that he had loop counter
problems, among other things). In a later reply, having read the loop
counter replies of others, I pointed the OP to them.

If you want the code re-factored, *you* do it.
[1] In fact, to be brutally honest, the code wants discarding and
rewriting from scratch by someone more experienced with C.

--
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
Jun 30 '07 #14

"Richard Heathfield" <rj*@see.sig.invalidha scritto nel messaggio news:Gb******************************@bt.com...
Army1987 said:

<huge snip, which was really your job, not mine>
>Apparently this will *always* return 0, no matter what happens...

Yeah. I said I fixed *some* of his problems, not all of them. The code
needs refactoring[1]. I am prepared to spend a considerable amount of
my time helping people out, for free, but I get to decide where I stop.
And on this occasion I chose to stop at the point where I'd stripped
out pointless casts, added a small handful of tests, and put the
free()s and fclose()s in the right places. Oh, and I corrected a couple
of loop termination conditions.

I didn't look closer at the code than that, but then I didn't claim that
I'd fixed his problem, either.

I did suggest that this might give better diagnostic information (which
is true in the long term, but it turns out that he had loop counter
problems, among other things). In a later reply, having read the loop
counter replies of others, I pointed the OP to them.

If you want the code re-factored, *you* do it.
Do you mean that, when the OP wrote:
fpin = fopen(argv[1],"r+");
fpout = fopen(argv[2],"w");
you found it easier to write that code than simplily to add

fpin = fopen(argv[1], "r+");
if (fpin == NULL) {
fprintf(stderr, "Cannot open '%s': %s\n", argv[1],
strerror(errrno));
exit(EXIT_FAILURE);
}
fpout = fopen(argv[2],"w");

if (fpout == NULL) {
fprintf(stderr, "Cannot open or create '%s': %s\n", argv[2],
strerror(errrno));
fclose(fpin);
exit(EXIT_FAILURE);
}
Jun 30 '07 #15
Army1987 said:
>
"Richard Heathfield" <rj*@see.sig.invalidha scritto nel messaggio
news:Gb******************************@bt.com...
>Army1987 said:

<huge snip, which was really your job, not mine>
>>Apparently this will *always* return 0, no matter what happens...

Yeah. I said I fixed *some* of his problems, not all of them. The
code needs refactoring[1]. I am prepared to spend a considerable
amount of my time helping people out, for free, but I get to decide
where I stop. And on this occasion I chose to stop at the point where
I'd stripped out pointless casts, added a small handful of tests, and
put the free()s and fclose()s in the right places. Oh, and I
corrected a couple of loop termination conditions.

I didn't look closer at the code than that, but then I didn't claim
that I'd fixed his problem, either.

I did suggest that this might give better diagnostic information
(which is true in the long term, but it turns out that he had loop
counter problems, among other things). In a later reply, having read
the loop counter replies of others, I pointed the OP to them.

If you want the code re-factored, *you* do it.
Do you mean that, when the OP wrote:
fpin = fopen(argv[1],"r+");
fpout = fopen(argv[2],"w");
you found it easier to write that code than simplily to add

fpin = fopen(argv[1], "r+");
if (fpin == NULL) {
fprintf(stderr, "Cannot open '%s': %s\n", argv[1],
strerror(errrno));
exit(EXIT_FAILURE);
}
fpout = fopen(argv[2],"w");

if (fpout == NULL) {
fprintf(stderr, "Cannot open or create '%s': %s\n", argv[2],
strerror(errrno));
fclose(fpin);
exit(EXIT_FAILURE);
}
I'm saying that I found it easier to add this code:

if(fpin != NULL)
{

and this code:

fclose(fpin);
}
else
{
fprintf(stderr, "Can't open %s for reading\n", argv[1]);
}

....do much the same for fpout, and then run the result through indent.

I reckon my solution involves less typing than yours. :-)

--
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
Jun 30 '07 #16
I've got one more error that I can't seem to get worked out. I have a
segmentation fault and upon playing with my code, I think that it
crashes when I try to deallocate the "date" array.

Here's the most recent version of my code. Everything else seems to
work properly.

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

int main(int argc, char *argv[])
{
/* Variable Declarations */
const int arraylength = 5;
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,"%ld,%f", &date[count],
&precipvals[count]);

if(count == 0) {
precip3hrs[count] = precipvals[count];
}
if(count == 1) {
precip3hrs[count] = precipvals[count] + precip3hrs[count-1];
}
if(count 1) {
precip3hrs[count] = precipvals[count] + precipvals[count-1] +
precipvals[count-2];
}
count++;
}

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

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

return 0;
}

Thanks,
Dave

Jul 2 '07 #17
dm******@cox.net said:
I've got one more error that I can't seem to get worked out. I have a
segmentation fault and upon playing with my code, I think that it
crashes when I try to deallocate the "date" array.

Here's the most recent version of my code. Everything else seems to
work properly.
You seem to have removed all the improvements I made to your code last
time you posted the program here. To save us both time, I won't suggest
any others.

FCOL.

--
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 2 '07 #18
On Jul 2, 11:02 am, Richard Heathfield <r...@see.sig.invalidwrote:
dmora...@cox.net said:
I've got one more error that I can't seem to get worked out. I have a
segmentation fault and upon playing with my code, I think that it
crashes when I try to deallocate the "date" array.
Here's the most recent version of my code. Everything else seems to
work properly.

You seem to have removed all the improvements I made to your code last
time you posted the program here. To save us both time, I won't suggest
any others.

FCOL.

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

I'm sorry. This was the code I got from my boss, not the one I was
working on. Let me see if I can find the one I was working on.

Dave

Jul 2 '07 #19

<dm******@cox.netha scritto nel messaggio news:11**********************@n60g2000hse.googlegr oups.com...
I've got one more error that I can't seem to get worked out. I have a
segmentation fault and upon playing with my code, I think that it
crashes when I try to deallocate the "date" array.

Here's the most recent version of my code. Everything else seems to
work properly.
Absolutely not.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
Why?
int main(int argc, char *argv[])
{
/* Variable Declarations */
const int arraylength = 5;
I'd use size_t (if for some reason I had to avoid the preprocessor)...
const int errorcode = -1;
Why the hell do you need it?
float sum = 0;
int count = 0;
int condition = 1;
float* precipvals = (float*)NULL;
float* precip3hrs = (float*)NULL;
long* date = (long*)NULL;
These casts don't buy anything.
FILE *fpin;
FILE *fpout;
if (argv < 3) {
fprintf(stderr, "Usage:\t%s infile outfile\n", argv[0]);
return EXIT_FAILURE;
}
/* or fpin = stdin, fpout = stdout, or ask the user what to do...*/
fpin = fopen(argv[1],"r");
if (fpin == NULL) { /* handle... */ }
fpout = fopen(argv[2],"w");
same as above...
/* Array Allocation */
precipvals = (float*)malloc(sizeof(int) * arraylength);
Why the hell sizeof(int)? And why do you need the cast?
precip3hrs = (float*)malloc(sizeof(int) * arraylength);
date = (long*)malloc(sizeof(int) * arraylength);

/* Calculation Section */
while(condition != EOF) {
while (condition < 2)
It is well possible that the file is ill-formed and you'll be able
to read date[count] but not precipvals[count]...
condition = fscanf(fpin,"%ld,%f", &date[count],
&precipvals[count]);

if(count == 0) {
precip3hrs[count] = precipvals[count];
}
if(count == 1) {
precip3hrs[count] = precipvals[count] + precip3hrs[count-1];
}
if(count 1) {
precip3hrs[count] = precipvals[count] + precipvals[count-1] +
precipvals[count-2];
}
count++;
if (count >= arraylength) break;
}

/* Output section */
for(count = 0; count < arraylength; count++) {
if(count == 0) {
printf("One hour precipitation: %2.2f Three hour
precipitation %2.2f\n",precipvals[count],precip3hrs[count]);
}
if(count == 1) {
printf("One hour precipitation: %2.2f Three hour
precipitation %2.2f\n",precipvals[count],precip3hrs[count]);
}
Couldn't you bring these two out of the loop and make the loop
count from 2?
if(count 1) {
printf("One hour precipitation: %2.2f Three hour
precipitation %2.2f\n",precipvals[count],precip3hrs[count]);
}
}
/* Write to file */
for(count = 0; count < arraylength; count++) {
if(count == 0) {
fprintf(fpout, "One hour precipitation: %2.2f Three hour
precipitation %2.2f\n",precipvals[count],precip3hrs[count]);
}
if(count == 1) {
fprintf(fpout,"One hour precipitation: %2.2f Three hour
precipitation %2.2f\n",precipvals[count],precip3hrs[count]);
}
if(count 1) {
fprintf(fpout,"One hour precipitation: %2.2f Three hour
precipitation %2.2f\n",precipvals[count],precip3hrs[count]);
}
}
fclose(fpin);
fclose(fpout);
if (fclose(fpout) != 0)
perror("Something went wrong");
printf("Testing\n");

/* Deallocation */
free(precipvals);
Why didn't you use free((void *)precipvals); this time? :-)
precipvals = (float*)NULL;
free(precip3hrs);
precip3hrs = (float*)NULL;
free(date);
date = (long*)NULL;

return 0;
}

Jul 2 '07 #20
"dm******@cox.net" wrote:
>
.... snip ...
if(count 1) {
precip3hrs[count] = precipvals[count] + precipvals[count-1] +
precipvals[count-2];
}
count++;
}

/* Output section */
for(count = 0; count < arraylength; count++) {
if(count == 0) {
/* this continues below - cbf */ printf("One hour precipitation: %2.2f Three hour
precipitation %2.2f\n",precipvals[count],precip3hrs[count]);
.... more snip ...

Please do not use tabs - use spaces, 3 at a time. Otherwise you
can get the above mess, or, on some systems, all leading tabs are
deleted.

--
<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 3 '07 #21
On Jul 2, 2:07 pm, "Army1987" <please....@for.itwrote:
<dmora...@cox.netha scritto nel messaggionews:11**********************@n60g2000hse .googlegroups.com...I've got one more error that I can't seem to get worked out. I have a
segmentation fault and upon playing with my code, I think that it
crashes when I try to deallocate the "date" array.
Here's the most recent version of my code. Everything else seems to
work properly.

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

Why?
int main(int argc, char *argv[])
{
/* Variable Declarations */
const int arraylength = 5;

I'd use size_t (if for some reason I had to avoid the preprocessor)...
const int errorcode = -1;

Why the hell do you need it?
float sum = 0;
int count = 0;
int condition = 1;
float* precipvals = (float*)NULL;
float* precip3hrs = (float*)NULL;
long* date = (long*)NULL;

These casts don't buy anything.
FILE *fpin;
FILE *fpout;

if (argv < 3) {
fprintf(stderr, "Usage:\t%s infile outfile\n", argv[0]);
return EXIT_FAILURE;}

/* or fpin = stdin, fpout = stdout, or ask the user what to do...*/
fpin = fopen(argv[1],"r");

if (fpin == NULL) { /* handle... */ }
fpout = fopen(argv[2],"w");

same as above...
/* Array Allocation */
precipvals = (float*)malloc(sizeof(int) * arraylength);

Why the hell sizeof(int)? And why do you need the cast?
precip3hrs = (float*)malloc(sizeof(int) * arraylength);
date = (long*)malloc(sizeof(int) * arraylength);
/* Calculation Section */
while(condition != EOF) {

while (condition < 2)
It is well possible that the file is ill-formed and you'll be able
to read date[count] but not precipvals[count]...
condition = fscanf(fpin,"%ld,%f", &date[count],
&precipvals[count]);
if(count == 0) {
precip3hrs[count] = precipvals[count];
}
if(count == 1) {
precip3hrs[count] = precipvals[count] + precip3hrs[count-1];
}
if(count 1) {
precip3hrs[count] = precipvals[count] + precipvals[count-1] +
precipvals[count-2];
}
count++;

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

Couldn't you bring these two out of the loop and make the loop
count from 2?
if(count 1) {
printf("One hour precipitation: %2.2f Three hour
precipitation %2.2f\n",precipvals[count],precip3hrs[count]);
}
}
/* Write to file */
for(count = 0; count < arraylength; count++) {
if(count == 0) {
fprintf(fpout, "One hour precipitation: %2.2f Three hour
precipitation %2.2f\n",precipvals[count],precip3hrs[count]);
}
if(count == 1) {
fprintf(fpout,"One hour precipitation: %2.2f Three hour
precipitation %2.2f\n",precipvals[count],precip3hrs[count]);
}
if(count 1) {
fprintf(fpout,"One hour precipitation: %2.2f Three hour
precipitation %2.2f\n",precipvals[count],precip3hrs[count]);
}
}
fclose(fpin);
fclose(fpout);

if (fclose(fpout) != 0)
perror("Something went wrong");printf("Testing\n");
/* Deallocation */
free(precipvals);

Why didn't you use free((void *)precipvals); this time? :-)
precipvals = (float*)NULL;
free(precip3hrs);
precip3hrs = (float*)NULL;
free(date);
date = (long*)NULL;
return 0;
}

I'm just going by the way I was taught and how my book does it. I'm
not a professional programmer.

Dave

Jul 3 '07 #22
Please do not use tabs - use spaces, 3 at a time. Otherwise you
can get the above mess, or, on some systems, all leading tabs are
deleted.
Thanks for the tip.

Dave

Jul 3 '07 #23

<dm******@cox.netha scritto nel messaggio news:11*********************@g4g2000hsf.googlegrou ps.com...
On Jul 2, 2:07 pm, "Army1987" <please....@for.itwrote:
><dmora...@cox.netha scritto nel messaggionews:11**********************@n60g2000hse .googlegroups.com...I've got one more error
that I can't seem to get worked out. I have a
segmentation fault and upon playing with my code, I think that it
crashes when I try to deallocate the "date" array.
Here's the most recent version of my code. Everything else seems to
work properly.

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

Why?
int main(int argc, char *argv[])
{
/* Variable Declarations */
const int arraylength = 5;

I'd use size_t (if for some reason I had to avoid the preprocessor)...
const int errorcode = -1;

Why the hell do you need it?
float sum = 0;
int count = 0;
int condition = 1;
float* precipvals = (float*)NULL;
float* precip3hrs = (float*)NULL;
long* date = (long*)NULL;

These casts don't buy anything.
FILE *fpin;
FILE *fpout;

if (argv < 3) {
fprintf(stderr, "Usage:\t%s infile outfile\n", argv[0]);
return EXIT_FAILURE;}

/* or fpin = stdin, fpout = stdout, or ask the user what to do...*/
fpin = fopen(argv[1],"r");

if (fpin == NULL) { /* handle... */ }
fpout = fopen(argv[2],"w");

same as above...
/* Array Allocation */
precipvals = (float*)malloc(sizeof(int) * arraylength);

Why the hell sizeof(int)? And why do you need the cast?
precip3hrs = (float*)malloc(sizeof(int) * arraylength);
date = (long*)malloc(sizeof(int) * arraylength);
/* Calculation Section */
while(condition != EOF) {

while (condition < 2)
It is well possible that the file is ill-formed and you'll be able
to read date[count] but not precipvals[count]...
condition = fscanf(fpin,"%ld,%f", &date[count],
&precipvals[count]);
if(count == 0) {
precip3hrs[count] = precipvals[count];
}
if(count == 1) {
precip3hrs[count] = precipvals[count] + precip3hrs[count-1];
}
if(count 1) {
precip3hrs[count] = precipvals[count] + precipvals[count-1] +
precipvals[count-2];
}
count++;

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

Couldn't you bring these two out of the loop and make the loop
count from 2?
if(count 1) {
printf("One hour precipitation: %2.2f Three hour
precipitation %2.2f\n",precipvals[count],precip3hrs[count]);
}
}
/* Write to file */
for(count = 0; count < arraylength; count++) {
if(count == 0) {
fprintf(fpout, "One hour precipitation: %2.2f Three hour
precipitation %2.2f\n",precipvals[count],precip3hrs[count]);
}
if(count == 1) {
fprintf(fpout,"One hour precipitation: %2.2f Three hour
precipitation %2.2f\n",precipvals[count],precip3hrs[count]);
}
if(count 1) {
fprintf(fpout,"One hour precipitation: %2.2f Three hour
precipitation %2.2f\n",precipvals[count],precip3hrs[count]);
}
}
fclose(fpin);
fclose(fpout);

if (fclose(fpout) != 0)
perror("Something went wrong");printf("Testing\n");
/* Deallocation */
free(precipvals);

Why didn't you use free((void *)precipvals); this time? :-)
precipvals = (float*)NULL;
free(precip3hrs);
precip3hrs = (float*)NULL;
free(date);
date = (long*)NULL;
return 0;
}


I'm just going by the way I was taught and how my book does it. I'm
not a professional programmer.
You asked why the program segfaults. Most of my comments are
possible reasons why it does. And I can't see the reason why even
the most unprofessional programmer in the world would, e.g.,
allocate space for 5 ints and write 5 floats, or 5 longs, in it.
Jul 3 '07 #24
On Jul 3, 9:03 am, "Army1987" <please....@for.itwrote:
<dmora...@cox.netha scritto nel messaggionews:11*********************@g4g2000hsf.g ooglegroups.com...
On Jul 2, 2:07 pm, "Army1987" <please....@for.itwrote:
<dmora...@cox.netha scritto nel messaggionews:11**********************@n60g2000hse .googlegroups.com...I've got one more error
that I can't seem to get worked out. I have a
segmentation fault and upon playing with my code, I think that it
crashes when I try to deallocate the "date" array.
Here's the most recent version of my code. Everything else seems to
work properly.
Absolutely not.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
Why?
int main(int argc, char *argv[])
{
/* Variable Declarations */
const int arraylength = 5;
I'd use size_t (if for some reason I had to avoid the preprocessor)...
const int errorcode = -1;
Why the hell do you need it?
float sum = 0;
int count = 0;
int condition = 1;
float* precipvals = (float*)NULL;
float* precip3hrs = (float*)NULL;
long* date = (long*)NULL;
These casts don't buy anything.
FILE *fpin;
FILE *fpout;
if (argv < 3) {
fprintf(stderr, "Usage:\t%s infile outfile\n", argv[0]);
return EXIT_FAILURE;}
/* or fpin = stdin, fpout = stdout, or ask the user what to do...*/
fpin = fopen(argv[1],"r");
if (fpin == NULL) { /* handle... */ }
fpout = fopen(argv[2],"w");
same as above...
/* Array Allocation */
precipvals = (float*)malloc(sizeof(int) * arraylength);
Why the hell sizeof(int)? And why do you need the cast?
precip3hrs = (float*)malloc(sizeof(int) * arraylength);
date = (long*)malloc(sizeof(int) * arraylength);
/* Calculation Section */
while(condition != EOF) {
while (condition < 2)
It is well possible that the file is ill-formed and you'll be able
to read date[count] but not precipvals[count]...
condition = fscanf(fpin,"%ld,%f", &date[count],
&precipvals[count]);
if(count == 0) {
precip3hrs[count] = precipvals[count];
}
if(count == 1) {
precip3hrs[count] = precipvals[count] + precip3hrs[count-1];
}
if(count 1) {
precip3hrs[count] = precipvals[count] + precipvals[count-1] +
precipvals[count-2];
}
count++;
if (count >= arraylength) break;
}
/* Output section */
for(count = 0; count < arraylength; count++) {
if(count == 0) {
printf("One hour precipitation: %2.2f Three hour
precipitation %2.2f\n",precipvals[count],precip3hrs[count]);
}
if(count == 1) {
printf("One hour precipitation: %2.2f Three hour
precipitation %2.2f\n",precipvals[count],precip3hrs[count]);
}
Couldn't you bring these two out of the loop and make the loop
count from 2?
if(count 1) {
printf("One hour precipitation: %2.2f Three hour
precipitation %2.2f\n",precipvals[count],precip3hrs[count]);
}
}
/* Write to file */
for(count = 0; count < arraylength; count++) {
if(count == 0) {
fprintf(fpout, "One hour precipitation: %2.2f Three hour
precipitation %2.2f\n",precipvals[count],precip3hrs[count]);
}
if(count == 1) {
fprintf(fpout,"One hour precipitation: %2.2f Three hour
precipitation %2.2f\n",precipvals[count],precip3hrs[count]);
}
if(count 1) {
fprintf(fpout,"One hour precipitation: %2.2f Three hour
precipitation %2.2f\n",precipvals[count],precip3hrs[count]);
}
}
fclose(fpin);
fclose(fpout);
if (fclose(fpout) != 0)
perror("Something went wrong");printf("Testing\n");
/* Deallocation */
free(precipvals);
Why didn't you use free((void *)precipvals); this time? :-)
precipvals = (float*)NULL;
free(precip3hrs);
precip3hrs = (float*)NULL;
free(date);
date = (long*)NULL;
return 0;
}
I'm just going by the way I was taught and how my book does it. I'm
not a professional programmer.

You asked why the program segfaults. Most of my comments are
possible reasons why it does. And I can't see the reason why even
the most unprofessional programmer in the world would, e.g.,
allocate space for 5 ints and write 5 floats, or 5 longs, in it.

I got it to work now.

Dave

Jul 3 '07 #25
On Jul 3, 9:37 am, "dmora...@cox.net" <dmora...@cox.netwrote:
On Jul 3, 9:03 am, "Army1987" <please....@for.itwrote:
<dmora...@cox.netha scritto nel messaggionews:11*********************@g4g2000hsf.g ooglegroups.com...
On Jul 2, 2:07 pm, "Army1987" <please....@for.itwrote:
><dmora...@cox.netha scritto nel messaggionews:11**********************@n60g2000hse .googlegroups.com...I've got one more error
>that I can't seem to get worked out. I have a
segmentation fault and upon playing with my code, I think that it
crashes when I try to deallocate the "date" array.
Here's the most recent version of my code. Everything else seems to
work properly.
>Absolutely not.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
>Why?
int main(int argc, char *argv[])
{
/* Variable Declarations */
const int arraylength = 5;
>I'd use size_t (if for some reason I had to avoid the preprocessor)...
const int errorcode = -1;
>Why the hell do you need it?
float sum = 0;
int count = 0;
int condition = 1;
float* precipvals = (float*)NULL;
float* precip3hrs = (float*)NULL;
long* date = (long*)NULL;
>These casts don't buy anything.
FILE *fpin;
FILE *fpout;
>if (argv < 3) {
> fprintf(stderr, "Usage:\t%s infile outfile\n", argv[0]);
> return EXIT_FAILURE;}
>/* or fpin = stdin, fpout = stdout, or ask the user what to do...*/
fpin = fopen(argv[1],"r");
>if (fpin == NULL) { /* handle... */ }
fpout = fopen(argv[2],"w");
>same as above...
/* Array Allocation */
precipvals = (float*)malloc(sizeof(int) * arraylength);
>Why the hell sizeof(int)? And why do you need the cast?
precip3hrs = (float*)malloc(sizeof(int) * arraylength);
date = (long*)malloc(sizeof(int) * arraylength);
/* Calculation Section */
while(condition != EOF) {
>while (condition < 2)
>It is well possible that the file is ill-formed and you'll be able
>to read date[count] but not precipvals[count]...
condition = fscanf(fpin,"%ld,%f", &date[count],
&precipvals[count]);
if(count == 0) {
precip3hrs[count] = precipvals[count];
}
if(count == 1) {
precip3hrs[count] = precipvals[count] + precip3hrs[count-1];
}
if(count 1) {
precip3hrs[count] = precipvals[count] + precipvals[count-1] +
precipvals[count-2];
}
count++;
>if (count >= arraylength) break;
}
/* Output section */
for(count = 0; count < arraylength; count++) {
if(count == 0) {
printf("One hour precipitation: %2.2f Three hour
precipitation %2.2f\n",precipvals[count],precip3hrs[count]);
}
if(count == 1) {
printf("One hour precipitation: %2.2f Three hour
precipitation %2.2f\n",precipvals[count],precip3hrs[count]);
}
>Couldn't you bring these two out of the loop and make the loop
>count from 2?
if(count 1) {
printf("One hour precipitation: %2.2f Three hour
precipitation %2.2f\n",precipvals[count],precip3hrs[count]);
}
}
/* Write to file */
for(count = 0; count < arraylength; count++) {
if(count == 0) {
fprintf(fpout, "One hour precipitation: %2.2f Three hour
precipitation %2.2f\n",precipvals[count],precip3hrs[count]);
}
if(count == 1) {
fprintf(fpout,"One hour precipitation: %2.2f Three hour
precipitation %2.2f\n",precipvals[count],precip3hrs[count]);
}
if(count 1) {
fprintf(fpout,"One hour precipitation: %2.2f Three hour
precipitation %2.2f\n",precipvals[count],precip3hrs[count]);
}
}
fclose(fpin);
fclose(fpout);
>if (fclose(fpout) != 0)
> perror("Something went wrong");printf("Testing\n");
/* Deallocation */
free(precipvals);
>Why didn't you use free((void *)precipvals); this time? :-)
precipvals = (float*)NULL;
free(precip3hrs);
precip3hrs = (float*)NULL;
free(date);
date = (long*)NULL;
return 0;
}
I'm just going by the way I was taught and how my book does it. I'm
not a professional programmer.
You asked why the program segfaults. Most of my comments are
possible reasons why it does. And I can't see the reason why even
the most unprofessional programmer in the world would, e.g.,
allocate space for 5 ints and write 5 floats, or 5 longs, in it.

I got it to work now.

Dave

I also tried to remove the casts and it didn't run. Works with them in
so I'll leave them in for now. As for the 5 ints, floats, longs, I was
experimenting with a data set and forgot to fix it.

Dave

Jul 3 '07 #26
"dm******@cox.net" <dm******@cox.netwrites:
On Jul 3, 9:37 am, "dmora...@cox.net" <dmora...@cox.netwrote:
[...]
>I got it to work now.

I also tried to remove the casts and it didn't run. Works with them in
so I'll leave them in for now. As for the 5 ints, floats, longs, I was
experimenting with a data set and forgot to fix it.
It's really not necessary to quote an entire long article to add a
one-line followup. Trim irrelevant material.

If removing the casts caused the program to break, you've probably
still got bugs; the casts are likely hiding them, not correcting them.

It would also be helpful to be more specific than "it didn't run".
*How* didn't it run? Did it compile and fail during execution? How?

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jul 3 '07 #27
dm******@cox.net said:

<snip>
I also tried to remove the casts and it didn't run. Works with them in
so I'll leave them in for now.
The casts won't fix your problem. They may, however, disguise it.

--
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 3 '07 #28

"Army1987" <pl********@for.itha scritto nel messaggio news:f6**********@tdi.cu.mi.it...
>
<dm******@cox.netha scritto nel messaggio news:11*********************@g4g2000hsf.googlegrou ps.com...
>I'm just going by the way I was taught and how my book does it. I'm
not a professional programmer.
You asked why the program segfaults.
>#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+");
If argc is 1, argv[1] is a null pointer. If argc is 0, it doesn't
even exist. Either way, the behaviour is undefined, probably a
segmentation fault.
>fpout = fopen(argv[2],"w");
If argc is 2, argv[1] is a null pointer. If argc is <2, it doesn't
even exist. Either way, the behaviour is undefined, probably a
segmentation fault.
>/* Array Allocation */
precipvals = (float*)malloc(sizeof(int) * arraylength);
If sizeof(int) < sizeof(float), you'll run out of space before
actually writing 5 floats. The behaviour would then be undefined,
probably a segmentation fault.
>precip3hrs = (float*)malloc(sizeof(int) * arraylength);
Ditto.
>date = (long*)malloc(sizeof(int) * arraylength);
If sizeof(int) < sizeof(float), you'll run out of space before
actually writing 5 floats. The behaviour would then be undefined,
probably a segmentation fault.
>/* Calculation Section */
while(condition != EOF) {
condition = fscanf(fpin,"%l,%f", &date[count], &precipvals[count]);
If the first fopen() failed, fpin is a null pointer. The behaviour
is undefined, probably a segmentation fault. And so will it be for
each time you'll use fpin.
If the third malloc() failed, date is a null pointer. The behaviour
is undefined, probably a segmentation fault. And so will it be for
each time you'll use date.
If the second malloc() failed, precipvals is a null pointer. The
behaviour is undefined, probably a segmentation fault. And so will
it be for each time you'll use precipvals.
If count arraylength (times sizeof int, divided by sizeof float)
the behaviour is undefined, very likely a segmentation fault.
>if(feof(fpin)) {
fclose(fpin);
return -3;
That's non-portable. The behaviour is implementation-defined (even
if I admit I don't believe it is this one which causes the
segmentation fault, but on the DS9K...).
>}
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++) {
You probably meant count = 0; count < arraylength? (see below)
>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]);
If count == arraylength, the behaviour is undefined, probably a
segmentation fault. Arrays in C are indexed from 0 to size - 1.
>}
}

/* Write to file */
for(count = 1; count <= arraylength; count++) {
Ditto as above.
>if(count == 1) {
fprintf(fpout, "One hour precipitation: %2.2f Three hour
precipitation %2.2f\n",precipvals[count],precip3hrs[count]);
If the second fopen() failed, fpout is a null pointer. Guess what?
>}
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);
Here you have no chance of knowing wheter fpout was successfully
written to and closed. (But I admit this will definitely not cause
a segmentation fault...) Sorry, I couldn't resist to this one.
>/* Deallocation */
free(date);
date = (long*)NULL;
free(precipvals);
precipvals = (float*)NULL;
free(precip3hrs);
precip3hrs = (float*)NULL;
return 0;
}

Jul 3 '07 #29

<dm******@cox.netha scritto nel messaggio news:11**********************@k29g2000hsd.googlegr oups.com...
[snip code which casts the result of malloc]
I also tried to remove the casts and it didn't run.
Are you compiling it with a C++ compiler?
(If so, I'd be surprised of knowing free() works even without
casting the argument to void*).
Jul 3 '07 #30
On Jul 3, 2:12 pm, "Army1987" <please....@for.itwrote:
<dmora...@cox.netha scritto nel messaggionews:11**********************@k29g2000hsd .googlegroups.com...
[snip code which casts the result of malloc]I also tried to remove the casts and it didn't run.

Are you compiling it with a C++ compiler?
(If so, I'd be surprised of knowing free() works even without
casting the argument to void*).
I am compiling with gcc on Linux. As for the results of the runs, my
boss ran the program that I wrote and the output looks like it's OK. I
will admit that I've not dealt with arrays since I took a class in C
in 2004. I still have my notes and books from that class and just did
it the way the book and notes told me. The notes are also online at
cs1313.ou.edu if you want to check them out.

Dave

Jul 3 '07 #31
"dm******@cox.net" <dm******@cox.netwrites:
On Jul 3, 2:12 pm, "Army1987" <please....@for.itwrote:
><dmora...@cox.netha scritto nel
messaggionews:11**********************@k29g2000hs d.googlegroups.com...
[snip code which casts the result of malloc]
I also tried to remove the casts and it didn't run.
>>
Are you compiling it with a C++ compiler?
(If so, I'd be surprised of knowing free() works even without
casting the argument to void*).

I am compiling with gcc on Linux. As for the results of the runs, my
boss ran the program that I wrote and the output looks like it's OK. I
will admit that I've not dealt with arrays since I took a class in C
in 2004. I still have my notes and books from that class and just did
it the way the book and notes told me. The notes are also online at
cs1313.ou.edu if you want to check them out.
If you have code that casts the result of malloc, and removing the
cast causes it to fail, the most likely explanation is that you've
forgetten the required '#include <stdlib.h>'.

Telling us that "it didn't run" doesn't help anybody. You keep
telling us things that make it clear that there are problems in your
code, but you don't post the actual code so we can help you fix them.

You can post your actual current code and let us help you with it, or
you can keep it to yourself and rest secure in the knowledge that your
code will continue to work until it breaks at the most inconvenient
possible moment. It's your choice.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jul 4 '07 #32
On Jul 3, 7:22 pm, Keith Thompson <k...@mib.orgwrote:
"dmora...@cox.net" <dmora...@cox.netwrites:
On Jul 3, 2:12 pm, "Army1987" <please....@for.itwrote:
<dmora...@cox.netha scritto nel
messaggionews:11**********************@k29g2000hsd .googlegroups.com...
[snip code which casts the result of malloc]
I also tried to remove the casts and it didn't run.
Are you compiling it with a C++ compiler?
(If so, I'd be surprised of knowing free() works even without
casting the argument to void*).
I am compiling with gcc on Linux. As for the results of the runs, my
boss ran the program that I wrote and the output looks like it's OK. I
will admit that I've not dealt with arrays since I took a class in C
in 2004. I still have my notes and books from that class and just did
it the way the book and notes told me. The notes are also online at
cs1313.ou.edu if you want to check them out.

If you have code that casts the result of malloc, and removing the
cast causes it to fail, the most likely explanation is that you've
forgetten the required '#include <stdlib.h>'.

Telling us that "it didn't run" doesn't help anybody. You keep
telling us things that make it clear that there are problems in your
code, but you don't post the actual code so we can help you fix them.

You can post your actual current code and let us help you with it, or
you can keep it to yourself and rest secure in the knowledge that your
code will continue to work until it breaks at the most inconvenient
possible moment. It's your choice.

--
Keith Thompson (The_Other_Keith) k...@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
I will post the code when I have it up at work...I can't get to it at
home.

Dave

Jul 4 '07 #33
"dm******@cox.net" <dm******@cox.netwrites:
<snip>
I still have my notes and books from that class and just did
it the way the book and notes told me. The notes are also online at
cs1313.ou.edu if you want to check them out.
A rather depressing density of errors. OK, it is a course for non CS
students but even so... I was going to pick out some of my favourite
howlers but, after a little thought, that seems rather cruel.

--
Ben.
Jul 4 '07 #34
On Jul 3, 7:43 pm, Ben Bacarisse <ben.use...@bsb.me.ukwrote:
"dmora...@cox.net" <dmora...@cox.netwrites:

<snip>
I still have my notes and books from that class and just did
it the way the book and notes told me. The notes are also online at
cs1313.ou.edu if you want to check them out.

A rather depressing density of errors. OK, it is a course for non CS
students but even so... I was going to pick out some of my favourite
howlers but, after a little thought, that seems rather cruel.

--
Ben.
When I took the class, it was required to do things a certain way and
that's the way I've always done them because I've only learned that
way. Maybe I need to go buy another book.

Dave

Jul 4 '07 #35
On Tue, 03 Jul 2007 06:42:01 -0700, "dm******@cox.net"
<dm******@cox.netwrote:
>On Jul 2, 2:07 pm, "Army1987" <please....@for.itwrote:
Many relevant comments the OP chose to ignore.
><dmora...@cox.netha scritto nel messaggionews:11**********************@n60g2000hse .googlegroups.com...I've got one more error that I can't seem to get worked out. I have a
segmentation fault and upon playing with my code, I think that it
crashes when I try to deallocate the "date" array.
snip
#include <math.h>
If your book tells you to include unused headers ...
>>
snip
float* precipvals = (float*)NULL;
and to cast the NULL pointer constant ...
float* precip3hrs = (float*)NULL;
snip
precipvals = (float*)malloc(sizeof(int) * arraylength);
and to cast the return from malloc ...

and that sizeof(int) is enough space for a float ...
>>
snip
>I'm just going by the way I was taught and how my book does it. I'm
not a professional programmer.
then you very much need to find a better book.

Remove del for email
Jul 4 '07 #36
then you very much need to find a better book.

Remove del for email
I included the math.h header because I originally planned to do some
other calculations, but forgot to take it out when I changed my mind.

Dave

Jul 4 '07 #37
Ben Bacarisse said:
"dm******@cox.net" <dm******@cox.netwrites:
<snip>
>I still have my notes and books from that class and just did
it the way the book and notes told me. The notes are also online at
cs1313.ou.edu if you want to check them out.

A rather depressing density of errors. OK, it is a course for non CS
students but even so... I was going to pick out some of my favourite
howlers but, after a little thought, that seems rather cruel.
I looked at two of the PDFs (the intro, and the one on variables), and
found little that was actually wrong. Presumably it gets worse later
on.

--
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 4 '07 #38
dm******@cox.net wrote, On 04/07/07 02:18:
On Jul 3, 7:43 pm, Ben Bacarisse <ben.use...@bsb.me.ukwrote:
>"dmora...@cox.net" <dmora...@cox.netwrites:

<snip>
>>I still have my notes and books from that class and just did
it the way the book and notes told me. The notes are also online at
cs1313.ou.edu if you want to check them out.
A rather depressing density of errors. OK, it is a course for non CS
students but even so... I was going to pick out some of my favourite
howlers but, after a little thought, that seems rather cruel.

When I took the class, it was required to do things a certain way and
that's the way I've always done them because I've only learned that
way. Maybe I need to go buy another book.
I've not looked at the notes, but I would recommend K&R2 as a reference
whatever your current book is.
--
Flash Gordon
Jul 4 '07 #39

"Keith Thompson" <ks***@mib.orgha scritto nel messaggio news:ln************@nuthaus.mib.org...
"dm******@cox.net" <dm******@cox.netwrites:
>On Jul 3, 2:12 pm, "Army1987" <please....@for.itwrote:
>><dmora...@cox.netha scritto nel
messaggionews:11**********************@k29g2000h sd.googlegroups.com...
[snip code which casts the result of malloc]
I also tried to remove the casts and it didn't run.
>>>
Are you compiling it with a C++ compiler?
(If so, I'd be surprised of knowing free() works even without
casting the argument to void*).

I am compiling with gcc on Linux. As for the results of the runs, my
boss ran the program that I wrote and the output looks like it's OK. I
will admit that I've not dealt with arrays since I took a class in C
in 2004. I still have my notes and books from that class and just did
it the way the book and notes told me. The notes are also online at
cs1313.ou.edu if you want to check them out.

If you have code that casts the result of malloc, and removing the
cast causes it to fail, the most likely explanation is that you've
forgetten the required '#include <stdlib.h>'.
He did include it, at least in the code he posted.
Jul 4 '07 #40
On Jul 4, 1:31 am, Richard Heathfield <r...@see.sig.invalidwrote:
Ben Bacarisse said:
"dmora...@cox.net" <dmora...@cox.netwrites:
<snip>
I still have my notes and books from that class and just did
it the way the book and notes told me. The notes are also online at
cs1313.ou.edu if you want to check them out.
A rather depressing density of errors. OK, it is a course for non CS
students but even so... I was going to pick out some of my favourite
howlers but, after a little thought, that seems rather cruel.

I looked at two of the PDFs (the intro, and the one on variables), and
found little that was actually wrong. Presumably it gets worse later
on.

--
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
The book I have is called "C: How To Program" by Deitel and Deitel.
Perhaps C isn't the best language to learn for my profession. I am a
professional Mathematician and going back to school to finish my
meteorology degree.

Dave

Jul 4 '07 #41
dm******@cox.net said:

<snip>
The book I have is called "C: How To Program" by Deitel and Deitel.
Well, that's okay. Not perfect, but pretty good.
Perhaps C isn't the best language to learn for my profession.
Oh, C is always the best language to learn, no matter what your
profession. Whether you're a truck driver or a ballroom dancer or a
hypnotherapist or a wheeltapper, C is what you want.
I am a
professional Mathematician and going back to school to finish my
meteorology degree.
Sounds like a job for C and possibly Matlab.

Drop all those silly casts. If that gives you diagnostic messages at
compile time or breaks the behaviour at runtime, *tell us*, and we'll
show you how to fix it. 99.9908237% of the time, casts aren't the right
fix.

Whenever you attempt to acquire a resource (either open a file, or
allocate memory, or whatever it is), check that the attempt succeeded.

I fixed your code along these lines recently. As far as I can tell, you
ignored those fixes. I'm still waiting for evidence of willingness to
learn before I expend more debugging time here.

--
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 4 '07 #42
On Jul 4, 8:08 am, Richard Heathfield <r...@see.sig.invalidwrote:
dmora...@cox.net said:

<snip>
The book I have is called "C: How To Program" by Deitel and Deitel.

Well, that's okay. Not perfect, but pretty good.
Perhaps C isn't the best language to learn for my profession.

Oh, C is always the best language to learn, no matter what your
profession. Whether you're a truck driver or a ballroom dancer or a
hypnotherapist or a wheeltapper, C is what you want.
I am a
professional Mathematician and going back to school to finish my
meteorology degree.

Sounds like a job for C and possibly Matlab.

Drop all those silly casts. If that gives you diagnostic messages at
compile time or breaks the behaviour at runtime, *tell us*, and we'll
show you how to fix it. 99.9908237% of the time, casts aren't the right
fix.

Whenever you attempt to acquire a resource (either open a file, or
allocate memory, or whatever it is), check that the attempt succeeded.

I fixed your code along these lines recently. As far as I can tell, you
ignored those fixes. I'm still waiting for evidence of willingness to
learn before I expend more debugging time here.

--
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
I got my hands on the error messages that I got (I got into my e-mail
at work, but since I'm not in the office today, I can't get to my
code). I got the following:

Cannot convert 'void*' to 'float*' in function main(int, char * *)

I will post my code tomorrow when I'm in the office.

Dave

Jul 4 '07 #43
On Wed, 04 Jul 2007 06:50:40 -0700, "dm******@cox.net"
<dm******@cox.netwrote:
>On Jul 4, 8:08 am, Richard Heathfield <r...@see.sig.invalidwrote:
>dmora...@cox.net said:

<snip>
The book I have is called "C: How To Program" by Deitel and Deitel.

Well, that's okay. Not perfect, but pretty good.
Perhaps C isn't the best language to learn for my profession.

Oh, C is always the best language to learn, no matter what your
profession. Whether you're a truck driver or a ballroom dancer or a
hypnotherapist or a wheeltapper, C is what you want.
I am a
professional Mathematician and going back to school to finish my
meteorology degree.

Sounds like a job for C and possibly Matlab.

Drop all those silly casts. If that gives you diagnostic messages at
compile time or breaks the behaviour at runtime, *tell us*, and we'll
show you how to fix it. 99.9908237% of the time, casts aren't the right
fix.

Whenever you attempt to acquire a resource (either open a file, or
allocate memory, or whatever it is), check that the attempt succeeded.

I fixed your code along these lines recently. As far as I can tell, you
ignored those fixes. I'm still waiting for evidence of willingness to
learn before I expend more debugging time here.

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

I got my hands on the error messages that I got (I got into my e-mail
at work, but since I'm not in the office today, I can't get to my
code). I got the following:

Cannot convert 'void*' to 'float*' in function main(int, char * *)
This is a very strong indication that you are using a C++ compiler and
not a C compiler.
Remove del for email
Jul 4 '07 #44
dm******@cox.net said:

<snip>
I got my hands on the error messages that I got (I got into my e-mail
at work, but since I'm not in the office today, I can't get to my
code). I got the following:

Cannot convert 'void*' to 'float*' in function main(int, char * *)
Either:

(a) you forgot to #include <stdlib.h>
(b) you're using C++

if (a), include it.
if (b), either

(d) you're using C++ deliberately
(e) you're using C++ accidentally

if (d), ask in comp.lang.c++ rather than comp.lang.c
if (e), you may find that changing your filename suffix from .cpp to .c
is sufficient.

--
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 4 '07 #45
On Jul 4, 9:19 am, Richard Heathfield <r...@see.sig.invalidwrote:
dmora...@cox.net said:

<snip>
I got my hands on the error messages that I got (I got into my e-mail
at work, but since I'm not in the office today, I can't get to my
code). I got the following:
Cannot convert 'void*' to 'float*' in function main(int, char * *)

Either:

(a) you forgot to #include <stdlib.hNope, Included it.
(b) you're using C++ Possibly, but don't think so.

if (a), include it.
if (b), either

(d) you're using C++ deliberately Don't think so
(e) you're using C++ accidentally Possibly

if (d), ask in comp.lang.c++ rather than comp.lang.c
if (e), you may find that changing your filename suffix from .cpp to .c
is sufficient.

--
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
I won't reply again until I can get to my code. There's really nothing
I can do or share until then.

Dave

Jul 4 '07 #46
>
This is a very strong indication that you are using a C++ compiler and
not a C compiler.
That's a good possibility...I'll have to check the compiler.

Dave

Jul 4 '07 #47
dm******@cox.net wrote, On 04/07/07 14:50:
On Jul 4, 8:08 am, Richard Heathfield <r...@see.sig.invalidwrote:
<snip>
>--
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
Please do not quote peoples signatures, the bit generally after a "-- ".
In fact, trim anything not relevant to your reply as I have done.
I got my hands on the error messages that I got (I got into my e-mail
at work, but since I'm not in the office today, I can't get to my
code). I got the following:

Cannot convert 'void*' to 'float*' in function main(int, char * *)
That probably means you are compiling your code as C++ rather than C. To
find out how to drive the tools you are using ask in a group dedicated
to your tools or system, but common reasons are using an upper case C as
the extension instead of a lower case c, using the extension .cpp, or
having a switch set somewhere to say it is C++. C++ is a significantly
different language.
I will post my code tomorrow when I'm in the office.
Include information about whatever your symptoms are when you do, most
people don't attempt to remember the problems that are being discussed
in all the threads. Oh, and make sure you compile as C instead of C++
before posting anything else.
--
Flash Gordon
Jul 4 '07 #48
Richard Heathfield wrote:
>
.... snip ...
>
Drop all those silly casts. If that gives you diagnostic messages
at compile time or breaks the behaviour at runtime, *tell us*, and
we'll show you how to fix it. 99.9908237% of the time, casts
aren't the right fix. ^^^^^^^^^^
Are you implying that 1,000,000,000 (or 1e9) casts have been
generated by programmers since C89 was published? Or are you
counting multiple use of the same cast, when a program is
recompiled, copied, etc.? :-)

--
<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 4 '07 #49
On Wed, 04 Jul 2007 14:19:10 +0000, Richard Heathfield
<rj*@see.sig.invalidwrote:
>dm******@cox.net said:

<snip>
>I got my hands on the error messages that I got (I got into my e-mail
at work, but since I'm not in the office today, I can't get to my
code). I got the following:

Cannot convert 'void*' to 'float*' in function main(int, char * *)

Either:

(a) you forgot to #include <stdlib.h>
Then the message would say "cannot convert int to float*"
Remove del for email
Jul 4 '07 #50

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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.