Creating and writing to files "on the fly"

Discussion in 'C Programming' started by George Marsaglia, Aug 3, 2003.

  1. I have a set of, say, 2000 points in the 8-dimensional simplex
    S={(x_1,x_2,...,x_8),x_1+x_2+...+x_8=1, x's>=0}.
    To help analyze that 8-dimensional set, I wish to project
    the set of points onto each of the 'faces' {x_i+x_j+x_k=1}
    for the 56 choices i,j,k, that is, i=1 to 6; j=i+1 to 7; k=j+1 to 8.
    Finally, use gnuplot to load and plot each of the 56 files of 3-d points.

    I have C code that will, for given i,j,k, find the projection
    (x,y,z) of each of the 2000 points. Those 2000 (x,y,z)'s
    must be sent to a file identified by the indices i,j,k.
    For i=1,j=2,k=3, list the 2000 projected points in file P123,
    for i=1,j=2,k=4, list the points in file P124,
    for i=1,j=2,k=5, list the points in file P125,
    ....
    for i=6,j=7,k=8, list the points in file P678.

    With my limited C experience, I did this by creating
    an array
    char c[56][5]={"P123","P124","P125", ...,"P678"};
    giving the name of each of the 56 files,
    then an outline of the code is

    jj=0; for(i=1;i<7;i++}{for(j=i+1;j<8;j++}{(for(k=j+1;k<9;k++){
    {for n to 2000, project nth point onto face x_i+x_j+x+k=1,
    print resulting x,y,z to file C[jj], then increment, jj++}
    }}}

    Is there a way to avoid creating the character array
    of file names, and instead, for given i,j,k, define
    and open the file for that specific i,j,k,
    write the 2000 (x,y,z)'s to it, then close that file?

    Thus for i=2,j=5,k=7, open file P257, write to P257, close P257,
    ...
    for i=3,j=6,k=8, open file P368, write to P368, close P368,
    etc.


    My current program works, and the resulting 56 files, when
    loaded by gnuplot and shown in 1-second intervals,
    make a very interesting display, (available on request).
    But I hope---through experts here---to learn of ways to
    create and write to files "on the fly", for the C part
    of the study.

    (I learned of gnuplot only a few days ago, when searching for ways
    to plot from C, as Maple was rather slow for this problem.
    Those not knowing of "gnuplot" may wish to look it up. I use
    the version found from a Google search for
    gnuplot MS Windows .)

    George Marsaglia
     
    George Marsaglia, Aug 3, 2003
    #1
    1. Advertising

  2. George Marsaglia

    Kurt Watzka Guest

    George Marsaglia wrote:
    >
    > I have a set of, say, 2000 points in the 8-dimensional simplex
    > S={(x_1,x_2,...,x_8),x_1+x_2+...+x_8=1, x's>=0}.
    > To help analyze that 8-dimensional set, I wish to project
    > the set of points onto each of the 'faces' {x_i+x_j+x_k=1}
    > for the 56 choices i,j,k, that is, i=1 to 6; j=i+1 to 7; k=j+1 to 8.
    > Finally, use gnuplot to load and plot each of the 56 files of 3-d points.
    >
    > I have C code that will, for given i,j,k, find the projection
    > (x,y,z) of each of the 2000 points. Those 2000 (x,y,z)'s
    > must be sent to a file identified by the indices i,j,k.
    > For i=1,j=2,k=3, list the 2000 projected points in file P123,
    > for i=1,j=2,k=4, list the points in file P124,
    > for i=1,j=2,k=5, list the points in file P125,
    > ...
    > for i=6,j=7,k=8, list the points in file P678.
    >
    > With my limited C experience, I did this by creating
    > an array
    > char c[56][5]={"P123","P124","P125", ...,"P678"};
    > giving the name of each of the 56 files,
    > then an outline of the code is
    >
    >
    > Is there a way to avoid creating the character array
    > of file names, and instead, for given i,j,k, define
    > and open the file for that specific i,j,k,
    > write the 2000 (x,y,z)'s to it, then close that file?


    char filename[5] = "P";

    for (i = 1; i <= 6; ++i)
    {
    filename[1] = '0' + i;
    for (j = i + 1; j <= 7; ++j)
    {
    filename[2] = '0' + j;
    for (k = j + 1; k <= 8; ++k)
    {
    filename[3] = '0' + k;
    /* work with filename */
    }
    }
    }

    or

    for (i = 1; i <= 6; ++i)
    {
    for (j = i + 1; j <= 7; ++j)
    {
    for (k = j + 1; k <= 8; ++k)
    {
    sprintf(filename, "P%d%d%d", i, j, k);
    /* work with filename */
    }
    }
    }

    > George Marsaglia


    Not _the_ George Marsaglia, I suppose

    Kurt Watzka
     
    Kurt Watzka, Aug 3, 2003
    #2
    1. Advertising

  3. George Marsaglia wrote:

    [...]
    > I have C code that will, for given i,j,k, find the projection
    > (x,y,z) of each of the 2000 points.

    [...]
    >
    > With my limited C experience, I did this by creating
    > an array
    > char c[56][5]={"P123","P124","P125", ...,"P678"};
    > giving the name of each of the 56 files,

    [...]

    > Is there a way to avoid creating the character array
    > of file names, and instead, for given i,j,k, define
    > and open the file for that specific i,j,k,
    > write the 2000 (x,y,z)'s to it, then close that file?


    {
    /* "int i, j, k;" assumed, as is "#include <stdio.h>" */
    char fname[FILENAME_MAX];
    FILE *f;
    for (i = 1; i < 7; i++)
    for (j = 2; j < 8; j++)
    for (k = 3; k < 9; k++) /* modify to omit magic numbers */
    {
    sprintf(fname,"P%d%d%d", i, j, k;
    if (!(f = fopen(fname,"w"))) { /* handle error */ }
    else {
    /* process & write data for this i, j, k */
    fclose(f);
    }
    }
    }


    --
    Martin Ambuhl
     
    Martin Ambuhl, Aug 3, 2003
    #3
    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. Murtix Van Basten

    creating on-the-fly asp:table in the code file

    Murtix Van Basten, Jun 25, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    6,534
    Murtix Van Basten
    Jun 25, 2003
  2. LIN

    creating ASPx Pages on the FLY

    LIN, Aug 2, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    418
  3. Replies:
    22
    Views:
    711
    CBFalconer
    Sep 30, 2007
  4. jchrisf79
    Replies:
    8
    Views:
    283
    Andrew Thompson
    Dec 16, 2007
  5. Brian

    Fly outmenu on the fly

    Brian, Apr 8, 2005, in forum: Javascript
    Replies:
    0
    Views:
    131
    Brian
    Apr 8, 2005
Loading...

Share This Page