tmpfile and freopen

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

Barry Schwarz

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 ).

I don't understand your question but your code raises some questions.
#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){

feof() does not tell you there is no more data. It tells you that you
have already tried to read past the last data. You could use
while(istat > 0 ){
to the same effect but with one less run through the loop.
if (intype == FLOAT) {
istat = fread(floatarray,sizeof(float),INDIM,stdin);
iaccum+=istat;
for (i=0; i<istat; i++){
dfloatarray = (double) floatarray;


What purpose do you think the cast servers?
}
}

....(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.


Remove del for email
 

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,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top