Not reading in what was written out... | | |
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,oty pe;
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[i] = (double) floatarray[i];
}
}
for (i=0;i<istat;i++){
floatarray[i] = (float) dfloatarray[i];
}
fprintf(stderr,"fwrite:
%d\n",fwrite(floatarray,sizeof(float),istat,tempfi le_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[i] = (double) floatarray[i];
}
}
/*....Continues on...*/
Any help is greatly appreciated! Thanks!
Josh | | | | re: Not reading in what was written out... joshuawilsonster@gmail.com wrote:[color=blue]
> All, I'm hoping you can provide some help. I have opened a temporary
> file, written to it, verified the correct amount was written,[/color]
Did you flush or close the temp file before attempting to read from it?
[color=blue]
> 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];[/color]
That's potentially a large amount of automatic data. On many
systems, this will blow the hardware stack.
[color=blue]
> ...
> while(feof(stdin) == 0){[/color]
This is nearly always wrong. See the clc FAQ.
--
Peter | | | | re: Not reading in what was written out...
<joshuawilsonster@gmail.com> wrote in message
news:1149718524.579038.214150@i39g2000cwa.googlegr oups.com...[color=blue]
> 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,oty pe;
> 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[i] = (double) floatarray[i];
> }
> }
>
> for (i=0;i<istat;i++){
> floatarray[i] = (float) dfloatarray[i];
> }
>
> fprintf(stderr,"fwrite:
> %d\n",fwrite(floatarray,sizeof(float),istat,tempfi le_fd) );
> }
>[/color]
You haven't closed the file. What you wrote may still be in a buffer
somewhere, not actually in the file on disk.
[color=blue]
> 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[i] = (double) floatarray[i];
> }
> }
>
> /*....Continues on...*/
>
>
>
>
> Any help is greatly appreciated! Thanks!
>
> Josh
>[/color]
--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Software Reuse Project | | | | re: Not reading in what was written out...
Awesome! Thanks so much for your help! |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,295 network members.
|