Redirecting input from file

D

dmoran21

Ben Bacarisse said:



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
 
R

Richard Heathfield

(e-mail address removed) said:

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

dmoran21

(e-mail address removed) said:



Well, that's okay. Not perfect, but pretty good.


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.


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
 
B

Barry Schwarz

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
 
R

Richard Heathfield

(e-mail address removed) said:

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

dmoran21

(e-mail address removed) said:




Either:

(a) you forgot to #include <stdlib.h> Nope, 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
 
D

dmoran21

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
 
F

Flash Gordon

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

CBFalconer

Richard said:
.... 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.? :)
 
B

Barry Schwarz

(e-mail address removed) said:



Either:

(a) you forgot to #include <stdlib.h>

Then the message would say "cannot convert int to float*"


Remove del for email
 
O

Old Wolf

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

In C++ there is an implicit conversion from
any object pointer type to void*, so there is
no need to cast any argument to free().

(Follow-ups set to c.l.c++)
 
C

CBFalconer

Old said:
In C++ there is an implicit conversion from any object pointer type
to void*, so there is no need to cast any argument to free().

(Follow-ups set to c.l.c++)

Wrong. In C++ there is NO such implicit conversion. That is in C
only. Follow-ups set to c.l.c.
 
O

Old Wolf

Wrong. In C++ there is NO such implicit conversion.

ISO/IEC 14882:1998, 4.10/2:
An rvalue of type "pointer to cv T," where T is an
object type, can be converted to an rvalue of type
"pointer to cv void." The result of converting a
"pointer to cv T" to a "pointer to cv void" points
to the start of the storage location where the object
of type T resides

You must be thinking of something else (perhaps
the fact that there's no implicit conversion
from void * to other pointer types).
That is in C only. Follow-ups set to c.l.c.

Not sure why you want to discuss C++ in c.l.c.
 
D

Default User

CBFalconer said:
Old Wolf wrote:

Wrong. In C++ there is NO such implicit conversion. That is in C
only. Follow-ups set to c.l.c.


Yeah, there is. What's lacking is the implicit conversion from void* to
object pointer. Hence malloc() and friends require a cast in C++.




Brian
 
D

Default User

Old Wolf wrote:

In C++ there is an implicit conversion from
any object pointer type to void*, so there is
no need to cast any argument to free().

(Follow-ups set to c.l.c++)

Don't set follow-ups unless when the post wasn't cross there in the
first place.



Brian
 
R

Richard Bos

CBFalconer said:
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.? :)

No; he's implying that of every 108976 casts written since C89 was
published, no more than 10 were The Right Thing. This would mean that
0.00917633240346...% of casts were right; but since there is only a
limited amount of certainty in the starting data, we should clip this
to, say, five significant digits, leaving 0.0091763% of right casts, and
therefore 99.9908237% wrong ones.

I must say, what with the enormous amount of badly written OS code out
there, much of it C-wannabe-C++, those figures sound reasonable to me.

Richard
 
D

dmoran21

Here is my code as of now, as well as an input file and my output. I
am aware that I've got more error proofing to do, but this is what I
have right now.

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

int main(int argc, char *argv[])
{
/* Variable Declarations */
const int arraylength = 5000;
const int exiterrorcode = -1;
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");

if(fpin == NULL || fpout == NULL) {
printf("Insufficient arguments\n");
exit(exiterrorcode);
}

/* Array Allocation */
precipvals = (float*)malloc(sizeof(float) * arraylength);
precip3hrs = (float*)malloc(sizeof(float) * arraylength);
date = (long*)malloc(sizeof(long) * 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(!(precipvals[count] == 0 && precip3hrs[count] == 0)) {
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(!(precipvals[count] == 0 && precip3hrs[count] == 0)) {
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);

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

return 0;
}

Input file
200701010000,3.58
200701010100,2.33
200701010200,4.55
200701010300,.29
200701010400,5.18
200701010500,0
200701010600,0
200701010700,0
200701010800,0
200701010900,.34

Output file
One hour precipitation: 3.58 Three hour precipitation 3.58
One hour precipitation: 2.33 Three hour precipitation 5.91
One hour precipitation: 4.55 Three hour precipitation 10.46
One hour precipitation: 0.29 Three hour precipitation 7.17
One hour precipitation: 5.18 Three hour precipitation 10.02
One hour precipitation: 0.00 Three hour precipitation 5.47
One hour precipitation: 0.00 Three hour precipitation 5.18
One hour precipitation: 0.34 Three hour precipitation 0.34
One hour precipitation: 0.00 Three hour precipitation 0.34

TIA
Dave
 
D

dmoran21

Now that I look at the code some more, I think there's a few areas
that I can remove i.e if(count = 0) etc. but I'm not sure.

Dave
 
C

CBFalconer

Here is my code as of now, as well as an input file and my output.
I am aware that I've got more error proofing to do, but this is
what I have right now.

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

int main(int argc, char *argv[])
{
/* Variable Declarations */

To start with, control your indentation. 3 spaces is enough.
Don't use tabs for Usenet display.
const int arraylength = 5000;
const int exiterrorcode = -1;
int count = 0;
int condition = 1;
float* precipvals = (float*)NULL;
float* precip3hrs = (float*)NULL;
long* date = (long*)NULL;

The above casts are unnecessary, and only serve to hide possible
errors. This is not C++.
FILE *fpin;
FILE *fpout;

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

if(fpin == NULL || fpout == NULL) {
printf("Insufficient arguments\n");
exit(exiterrorcode);
}

I believe passing NULL to fopen results in undefined behaviour.
Use argc.
/* Array Allocation */
precipvals = (float*)malloc(sizeof(float) * arraylength);
precip3hrs = (float*)malloc(sizeof(float) * arraylength);
date = (long*)malloc(sizeof(long) * arraylength);

Again, the casts are unnecessary etc.
/* Calculation Section */
while(condition != EOF) {
condition = fscanf(fpin,"%ld,%f", &date[count],
&precipvals[count]);

Here unless 'condition' is 2 fscanf failed. I also gave up
following the code.
 

Ask a Question

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

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

Ask a Question

Members online

Forum statistics

Threads
473,787
Messages
2,569,629
Members
45,329
Latest member
InezZ76898

Latest Threads

Top