Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > tmpfile and freopen

Reply
Thread Tools

tmpfile and freopen

 
 
joshuawilsonster@gmail.com
Guest
Posts: n/a
 
      06-05-2006
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

 
Reply With Quote
 
 
 
 
Barry Schwarz
Guest
Posts: n/a
 
      06-15-2006
On 5 Jun 2006 11:15:51 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
freopen, fclose and stdout Alex Vinokur C Programming 4 01-11-2013 07:22 AM
freopen on both stdout and stderr Guillaume Dargaud C Programming 6 11-26-2007 02:40 PM
freopen() Teh Charleh C Programming 1 12-07-2003 11:38 PM
freopen & cout Alex Vinokur C++ 1 12-03-2003 03:26 AM
reading file created with tmpfile? Oliver Knoll C Programming 4 07-18-2003 08:20 PM



Advertisments