469,298 Members | 2,148 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,298 developers. It's quick & easy.

tmpfile and freopen

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[i] = (double) floatarray[i];
}
}

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

Jun 5 '06 #1
1 2200
On 5 Jun 2006 11:15:51 -0700, jo**************@gmail.com wrote:
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[i] = (double) floatarray[i];
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
Jun 15 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Alex Vinokur | last post: by
4 posts views Thread by Oliver Knoll | last post: by
2 posts views Thread by Alex Vinokur | last post: by
3 posts views Thread by John Devereux | last post: by
7 posts views Thread by Harold Fellermann | last post: by
1 post views Thread by system55 | last post: by
3 posts views Thread by John Machin | last post: by
4 posts views Thread by Hlk.Turk | last post: by
2 posts views Thread by Guillaume Dargaud | last post: by
1 post views Thread by CARIGAR | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.