tmpnam, strcpy - incompatible pointer type?

J

Josh Wilson

Hey gang,

So I have my stdin which is user defined file, and am wanting to write
it to a temporary file (seems odd I know, but there is a sincere
reason), and then use that file as the stdin for the remaining part of
the program. I have combed the FAQ and got some preliminary questions
answered, and tried to model this after what I have seen suggested to
people previously, but for my strcpy and freopen I get,
"warning: passing arg 1 of `strcpy' from incompatible pointer type"
and
"warning: passing arg 1 of `freopen' from incompatible pointer type".


#include <stdio.h>
#include <string.h>
FILE *tempfile;
char s[L_tmpnam];


strcpy(tempfile, tmpnam(s));
/*tempfile = stdout; */
do{
. . .
for (i=0;i<istat;i++) floatarray = (float) dfloatarray;
iostat = fwrite(floatarray,4,istat,tempfile);
if (iostat != istat) fprintf(stderr, "Failed on writing to temporary
file.");
} while(feof(stdin) == 0);
freopen(tempfile,"r",stdin);


But it doesn't have a problem w/ tempfile as arg 4 of fwrite? As you
can see, I have tried setting the tempfil as stdout, but that doesn't
change my error message. Any thoughts? Thanks as always!

JMW
 
E

Eric Sosman

Josh said:
Hey gang,

So I have my stdin which is user defined file, and am wanting to write
it to a temporary file (seems odd I know, but there is a sincere
reason), and then use that file as the stdin for the remaining part of
the program. I have combed the FAQ and got some preliminary questions
answered, and tried to model this after what I have seen suggested to
people previously, but for my strcpy and freopen I get,
"warning: passing arg 1 of `strcpy' from incompatible pointer type"
and
"warning: passing arg 1 of `freopen' from incompatible pointer type".


#include <stdio.h>
#include <string.h>
FILE *tempfile;
char s[L_tmpnam];


strcpy(tempfile, tmpnam(s));

This line is evidence that you have no understanding
of what a FILE* is for.

A FILE* points to a piece of implementation magic
that somehow keeps track of an I/O stream. The details
of how this is done are none of your business; you are
not to look too closely at the magic. Meddle not in
the affairs of implementors, for they are subtle and
quick to undefined behavior.

The way you associate a FILE* stream with a given
file name is to use the fopen() function. You do *not*
try to blast the file name's characters onto the magical
object to which the FILE* points. Just Say No.
/*tempfile = stdout; */
do{
. . .
for (i=0;i<istat;i++) floatarray = (float) dfloatarray;
iostat = fwrite(floatarray,4,istat,tempfile);
if (iostat != istat) fprintf(stderr, "Failed on writing to temporary
file.");
} while(feof(stdin) == 0);
freopen(tempfile,"r",stdin);


Repeat after me: A FILE* is not a file name. Write
this on the blackboard one hundred times, and then revisit
this line of code.
But it doesn't have a problem w/ tempfile as arg 4 of fwrite?

Why should it? fwrite() expects to receive a FILE* as
its fourth argument.
As you
can see, I have tried setting the tempfil as stdout, but that doesn't
change my error message. Any thoughts?

Thought: You really need to review C's I/O capabilities,
because the evidence suggests you don't yet understand them.
You are not yet ready to write this program.
 

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,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top