tmpfile and freopen

Discussion in 'C Programming' started by joshuawilsonster@gmail.com, Jun 5, 2006.

  1. Guest

    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
     
    , Jun 5, 2006
    #1
    1. Advertising

  2. On 5 Jun 2006 11:15:51 -0700, 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 = (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
     
    Barry Schwarz, Jun 15, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex Vinokur

    freopen & cout

    Alex Vinokur, Dec 2, 2003, in forum: C++
    Replies:
    1
    Views:
    1,204
    Jack Klein
    Dec 3, 2003
  2. Oliver Knoll

    reading file created with tmpfile?

    Oliver Knoll, Jul 18, 2003, in forum: C Programming
    Replies:
    4
    Views:
    446
    Chris Torek
    Jul 18, 2003
  3. Alex Vinokur

    freopen, fclose and stdout

    Alex Vinokur, Dec 3, 2003, in forum: C Programming
    Replies:
    4
    Views:
    8,403
    vidyaani
    Jan 11, 2013
  4. Teh Charleh

    freopen()

    Teh Charleh, Dec 7, 2003, in forum: C Programming
    Replies:
    1
    Views:
    498
    Enrico `Trippo' Porreca
    Dec 7, 2003
  5. Guillaume Dargaud

    freopen on both stdout and stderr

    Guillaume Dargaud, Nov 23, 2007, in forum: C Programming
    Replies:
    6
    Views:
    2,177
    Charlie Gordon
    Nov 26, 2007
Loading...

Share This Page