Not reading in what was written out...

  • Thread starter joshuawilsonster
  • Start date
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
 
P

Peter Nilsson

All, I'm hoping you can provide some help. I have opened a temporary
file, written to it, verified the correct amount was written,

Did you flush or close the temp file before attempting to read from it?
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.

...
========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];

That's potentially a large amount of automatic data. On many
systems, this will blow the hardware stack.
...
while(feof(stdin) == 0){

This is nearly always wrong. See the clc FAQ.
 
F

Fred Kleinschmidt

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) );
}


You haven't closed the file. What you wrote may still be in a buffer
somewhere, not actually in the file on disk.
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
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top