J
joshuawilsonster
I have checked: http://c-faq.com/stdio/undofreopen.html and don't
believe it addresses my question. I am dealing with large files of
numbers and want to take in small chunks and act on them--sometimes
using information based on the whole file. My approach is to read in
the whole file in chunks, place it in tempfile = tmpfile(); and then
run this back through. However, sometimes I will bypass this option
and read the chunks outright (not needing to know information about the
whole set). Sometimes the original stdin may not be accessible and
this is why I need a temporary file (i.e. I read from /dev/random ).
#define INDIM 32768
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
{
int iaccum=0, istat=0,
FILE *tempfile;
double dfloatarray[INDIM];
float floatarray[INDIM];
.....
if (norm == 1){ /* if norm==0, then just need to read from
stdin */
tempfile = tmpfile();
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;
}
}
....(deals w/ different intype 's
freopen(tempfile,"r",stdin); /* I know this is incorrect type */
}
/* prog continues... need to read from stdin regardless if (norm==0 ||
norm==1) */
}
I am avoiding tmpnam or mkstemp, tmpfile() seems to be the best way to
go.
Thoughts appreciated.
JMW
believe it addresses my question. I am dealing with large files of
numbers and want to take in small chunks and act on them--sometimes
using information based on the whole file. My approach is to read in
the whole file in chunks, place it in tempfile = tmpfile(); and then
run this back through. However, sometimes I will bypass this option
and read the chunks outright (not needing to know information about the
whole set). Sometimes the original stdin may not be accessible and
this is why I need a temporary file (i.e. I read from /dev/random ).
#define INDIM 32768
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
{
int iaccum=0, istat=0,
FILE *tempfile;
double dfloatarray[INDIM];
float floatarray[INDIM];
.....
if (norm == 1){ /* if norm==0, then just need to read from
stdin */
tempfile = tmpfile();
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;
}
}
....(deals w/ different intype 's
freopen(tempfile,"r",stdin); /* I know this is incorrect type */
}
/* prog continues... need to read from stdin regardless if (norm==0 ||
norm==1) */
}
I am avoiding tmpnam or mkstemp, tmpfile() seems to be the best way to
go.
Thoughts appreciated.
JMW