J
joshuawilsonster
All, I'm hoping you can provide some help. I have opened a temporary
file, written to it, verified the correct amount was written, done
freopen() for the file to stdin, then try read the information back,
but 0 amount is being read in. I have done ferror() on the stream and
it is still 0, and freopen() != NULL.
Output from passing in 20 short values and writing them out as float
(istat is the return of fread() ):
fwrite: 10
istat: 0 ferror:0
Output from passing in 6000 short values and writing them out as float
(istat is the return of fread() ):
fwrite: 3000
istat: 2048 ferror:0
istat: 0 ferror:0
========Code========
#define FLOAT 0
#define INDIM 32768
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
main (argc,argv)
int argc;
char **argv;
{
double dfloatarray[INDIM];
float floatarray[INDIM];
int i,j,k,iop,jop,kop,nneg,abs,istat,iaccum,intype,otype;
int iostat,icheck;
int norm=0;
FILE *outplace;
char tempfile[] = ".dconvert_tempXXXXXX";
FILE *tempfile_fd;
/*.... Parse command line....*/
intype = FLOAT;
iaccum = 0;
if (( tempfile_fd = fdopen( mkstemp(tempfile) , "w+" )) == NULL){
fprintf(stderr,"Could not create temporary file\n");
}
while(feof(stdin) == 0){
if (intype == FLOAT) {
istat = fread(floatarray,sizeof(float),INDIM,stdin);
iaccum+=istat;
for (i=0; i<istat; i++){
dfloatarray = (double) floatarray;
}
}
for (i=0;i<istat;i++){
floatarray = (float) dfloatarray;
}
fprintf(stderr,"fwrite:
%d\n",fwrite(floatarray,sizeof(float),istat,tempfile_fd) );
}
intype = FLOAT;
if(freopen(tempfile,"r+",stdin) == NULL) fprintf(stderr,"Didn't
reopen\n");
}
iaccum = 0;
while(1) {
if (intype == FLOAT) {
/*** This is where not reading the correct amount.... ***/
istat = fread(floatarray,4,INDIM,stdin);
fprintf(stderr,"istat: %d\tferror:%d\n",istat,ferror(stdin));
iaccum+=istat;
if (swapin == 1){
swapbytes(floatarray,istat,4);
}
for (i=0; i<istat; i++){
dfloatarray = (double) floatarray;
}
}
/*....Continues on...*/
Any help is greatly appreciated! Thanks!
Josh
file, written to it, verified the correct amount was written, done
freopen() for the file to stdin, then try read the information back,
but 0 amount is being read in. I have done ferror() on the stream and
it is still 0, and freopen() != NULL.
Output from passing in 20 short values and writing them out as float
(istat is the return of fread() ):
fwrite: 10
istat: 0 ferror:0
Output from passing in 6000 short values and writing them out as float
(istat is the return of fread() ):
fwrite: 3000
istat: 2048 ferror:0
istat: 0 ferror:0
========Code========
#define FLOAT 0
#define INDIM 32768
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
main (argc,argv)
int argc;
char **argv;
{
double dfloatarray[INDIM];
float floatarray[INDIM];
int i,j,k,iop,jop,kop,nneg,abs,istat,iaccum,intype,otype;
int iostat,icheck;
int norm=0;
FILE *outplace;
char tempfile[] = ".dconvert_tempXXXXXX";
FILE *tempfile_fd;
/*.... Parse command line....*/
intype = FLOAT;
iaccum = 0;
if (( tempfile_fd = fdopen( mkstemp(tempfile) , "w+" )) == NULL){
fprintf(stderr,"Could not create temporary file\n");
}
while(feof(stdin) == 0){
if (intype == FLOAT) {
istat = fread(floatarray,sizeof(float),INDIM,stdin);
iaccum+=istat;
for (i=0; i<istat; i++){
dfloatarray = (double) floatarray;
}
}
for (i=0;i<istat;i++){
floatarray = (float) dfloatarray;
}
fprintf(stderr,"fwrite:
%d\n",fwrite(floatarray,sizeof(float),istat,tempfile_fd) );
}
intype = FLOAT;
if(freopen(tempfile,"r+",stdin) == NULL) fprintf(stderr,"Didn't
reopen\n");
}
iaccum = 0;
while(1) {
if (intype == FLOAT) {
/*** This is where not reading the correct amount.... ***/
istat = fread(floatarray,4,INDIM,stdin);
fprintf(stderr,"istat: %d\tferror:%d\n",istat,ferror(stdin));
iaccum+=istat;
if (swapin == 1){
swapbytes(floatarray,istat,4);
}
for (i=0; i<istat; i++){
dfloatarray = (double) floatarray;
}
}
/*....Continues on...*/
Any help is greatly appreciated! Thanks!
Josh