write a multidimensional array in a file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • steph_de_marseille@yahoo.fr

    write a multidimensional array in a file

    I would like to write a [250]x[250] array in a file.

    If the array has a small numbers of columns I know I can use a loop
    like:

    int i,j;
    double array[50][2];
    FILE *file1;
    file1=fopen("da ta.dat","w");
    for(i=0;i<50;i+ +){
    fprintf(file1," %f %f\ %f %f \n",array[i][0],array[i][1]);
    }
    }
    fclose(file1);

    but in a case a 250 (for example) columns????
    I guess there is another way than writing 250 times array[][]???

    I did it in fortran

    open(unit=1,fil e='tab4')
    do 102 k=1,2*nrow+1
    write(unit=1,fm t=200)(Y(k,i),i =1,ncol)
    102 continue
    200 format(2X,200F1 0.4)
    close(1)

  • Netocrat

    #2
    Re: write a multidimensiona l array in a file

    On Tue, 12 Jul 2005 23:51:49 -0700, steph_de_marsei lle wrote:
    [color=blue]
    > I would like to write a [250]x[250] array in a file.[/color]

    *note that you say this but your demonstration code uses an array of size
    [50][2] so that's what I've used.

    If the file will be written and read back on the same machine (ie you
    don't need to worry about portability), and it doesn't need to be humanly
    readable, you can open the file in binary mode and read/write the whole
    array as it is represented in memory to the file in one operation. In this
    case use "wb" rather than "w".

    if (! fwrite(array, sizeof(array), 1, file1)) {
    perror("fwrite" );
    exit(EXIT_FAILU RE);
    }

    Then to get back the data, you can declare:
    double (*arrayp)[2] = malloc(sizeof(* arrayp) * 50);
    if (!arrayp) {
    perror("malloc" );
    exit(EXIT_FAILU RE);
    }

    Open the file in "rb" mode, and use:
    if (! fread(arrayp, sizeof(*arrayp) * 50, 1, file1)) {
    perror("fread") ;
    exit(EXIT_FAILU RE);
    }

    Then to access elements use arrayp[x][y] as usual.
    [color=blue]
    > If the array has a small numbers of columns I know I can use a loop like:
    >
    > int i,j;
    > double array[50][2];
    > FILE *file1;
    > file1=fopen("da ta.dat","w");[/color]

    You should check for fopen returning an error.
    [color=blue]
    > for(i=0;i<50;i+ +){
    > fprintf(file1," %f %f\ %f %f \n",array[i][0],array[i][1]);
    > }[/color]

    This fprintf statement has a few problems: firstly you have 4 %f's and
    only two double arguments. The "\ " doesn't seem to serve any purpose.
    Also you aren't checking for fprintf returning an error.

    Finally you may or may not want to limit the number of digits being
    printed if it is for human reading and precision is not an issue. In this
    case use %10.4f or something like it, where the 10 is the maximum total
    number of digits and the 4 is the maximum number of digits after the
    decimal point.

    If you simply want to print the array to the file in a portable, humanly
    readable format, then this double loop may serve your needs. The only
    minor annoyance is that there will be a trailing space at the end of each
    line, but you can eliminate that with a little extra code.

    for (i = 0; i < 50; i++) {
    for (j = 0; j < 2; j++) {
    if (fprintf(file1, "%f ", array[i][j]) < 0) {
    perror("fprintf ");
    exit(EXIT_FAILU RE);
    }
    }
    if (fprintf(file1, "\n") < 0) {
    perror("fprintf ");
    exit(EXIT_FAILU RE);
    }
    }

    You can read this file back into an array but I won't post code for that
    since it's a little more involved and you haven't said that you need to do
    that anyway.
    [color=blue]
    > fclose(file1);[/color]

    Again, check for fclose returning an error.
    [color=blue]
    > but in a case a 250 (for example) columns???? I guess there is another
    > way than writing 250 times array[][]???
    >
    > I did it in fortran
    >
    > open(unit=1,fil e='tab4')
    > do 102 k=1,2*nrow+1
    > write(unit=1,fm t=200)(Y(k,i),i =1,ncol)
    > 102 continue
    > 200 format(2X,200F1 0.4)
    > close(1)[/color]

    If I understood Fortran I would know better what you are trying to
    achieve; but it looks to me like this will produce a humanly readable
    file. Anyhow that gives you two options.

    Comment

    • steph_de_marseille@yahoo.fr

      #3
      Re: write a multidimensiona l array in a file

      Thanks Netocrat for your answer,

      In fact, in the case of the 250X250 array, I wrote a "not humanly
      readable" binary file, with 2 columns and 250x250lines, but i would
      like to have a "humanly readable" file with 250 lines and 250 columns,
      which looks like my array.

      Comment

      • Netocrat

        #4
        Re: write a multidimensiona l array in a file

        On Wed, 13 Jul 2005 01:58:23 -0700, steph_de_marsei lle wrote:
        [color=blue]
        > Thanks Netocrat for your answer,
        >
        > In fact, in the case of the 250X250 array, I wrote a "not humanly
        > readable" binary file, with 2 columns and 250x250lines, but i would like
        > to have a "humanly readable" file with 250 lines and 250 columns, which
        > looks like my array.[/color]

        The loops that I posted will do this if you change the numbers from 50 and
        2 to 250. I don't know how readable the file will be with 250 numbers on
        one line though.

        Comment

        • Netocrat

          #5
          Re: write a multidimensiona l array in a file

          On Wed, 13 Jul 2005 19:20:40 +1000, Netocrat wrote:
          [color=blue]
          > On Wed, 13 Jul 2005 01:58:23 -0700, steph_de_marsei lle wrote:
          >[color=green]
          >> Thanks Netocrat for your answer,
          >>
          >> In fact, in the case of the 250X250 array, I wrote a "not humanly
          >> readable" binary file, with 2 columns and 250x250lines, but i would like
          >> to have a "humanly readable" file with 250 lines and 250 columns, which
          >> looks like my array.[/color]
          >
          > The loops that I posted will do this if you change the numbers from 50 and
          > 2 to 250. I don't know how readable the file will be with 250 numbers on
          > one line though.[/color]

          Troll suckered me even as I was cluing on...

          Comment

          • Barry Schwarz

            #6
            Re: write a multidimensiona l array in a file

            On 12 Jul 2005 23:51:49 -0700, steph_de_marsei lle@yahoo.fr wrote:
            [color=blue]
            >I would like to write a [250]x[250] array in a file.
            >
            >If the array has a small numbers of columns I know I can use a loop
            >like:
            >
            >int i,j;
            >double array[50][2];
            >FILE *file1;
            >file1=fopen("d ata.dat","w");
            >for(i=0;i<50;i ++){[/color]

            You would replace the limit in this with 250.
            [color=blue]
            > fprintf(file1," %f %f\ %f %f \n",array[i][0],array[i][1]);[/color]

            This contains a syntax error and invokes undefined behavior.
            Backslash-blank is not a valid escape sequence like \n. You have four
            format specifiers but only two arguments t6o satisfy them.

            Getting back to your real question, you would replace the single call
            to fprintf with a loop like the following

            for (j = 0; j < 250; j++)
            fprintf(file1," %f ", array[i][j]);

            and then add a final (for this line) call to printf to terminate the
            line

            fprintf(file1," \n");

            which will execute once per line after all the variables for that line
            have been written.
            [color=blue]
            > }
            >}
            >fclose(file1 );
            >
            >but in a case a 250 (for example) columns????
            >I guess there is another way than writing 250 times array[][]???
            >
            >I did it in fortran
            >
            >open(unit=1,fi le='tab4')
            > do 102 k=1,2*nrow+1
            > write(unit=1,fm t=200)(Y(k,i),i =1,ncol)
            >102 continue
            >200 format(2X,200F1 0.4)
            > close(1)[/color]



            <<Remove the del for email>>

            Comment

            • steph_de_marseille@yahoo.fr

              #7
              Re: write a multidimensiona l array in a file

              thanks a lot to both of you!

              Comment

              • Dave Thompson

                #8
                Re: write a multidimensiona l array in a file

                On Wed, 13 Jul 2005 13:48:26 -0700, Barry Schwarz <schwarzb@deloz .net>
                wrote:
                [color=blue]
                > On 12 Jul 2005 23:51:49 -0700, steph_de_marsei lle@yahoo.fr wrote:[/color]
                <snip>[color=blue][color=green]
                > > fprintf(file1," %f %f\ %f %f \n",array[i][0],array[i][1]);[/color]
                >
                > This contains a syntax error and invokes undefined behavior.
                > Backslash-blank is not a valid escape sequence like \n. You have four
                > format specifiers but only two arguments t6o satisfy them.[/color]

                Nit: the (or any) syntax error also invokes UB, but if this phrasing
                was meant to say that the extra arguments to fprintf() cause UB,
                that's wrong. They're safely discarded (7.19.6.1p2) just as they are
                for other (user) vararg functions. Unless of course the arguments
                cause UB within themselves, like two assignments to same object.

                - David.Thompson1 at worldnet.att.ne t

                Comment

                • Flash Gordon

                  #9
                  Re: write a multidimensiona l array in a file

                  Dave Thompson wrote:[color=blue]
                  > On Wed, 13 Jul 2005 13:48:26 -0700, Barry Schwarz <schwarzb@deloz .net>
                  > wrote:
                  >
                  >[color=green]
                  >>On 12 Jul 2005 23:51:49 -0700, steph_de_marsei lle@yahoo.fr wrote:[/color]
                  >
                  > <snip>
                  >[color=green][color=darkred]
                  >>> fprintf(file1," %f %f\ %f %f \n",array[i][0],array[i][1]);[/color]
                  >>
                  >>This contains a syntax error and invokes undefined behavior.
                  >>Backslash-blank is not a valid escape sequence like \n. You have four
                  >>format specifiers but only two arguments t6o satisfy them.[/color]
                  >
                  >
                  > Nit: the (or any) syntax error also invokes UB, but if this phrasing
                  > was meant to say that the extra arguments to fprintf() cause UB,
                  > that's wrong. They're safely discarded (7.19.6.1p2) just as they are
                  > for other (user) vararg functions. Unless of course the arguments
                  > cause UB within themselves, like two assignments to same object.[/color]

                  You misread the above. The format specifier says there are 4 doubles,
                  but only two actual arguments have been provided, leaving to instances
                  of %f without corresponding parameters.
                  --
                  Flash Gordon
                  Living in interesting times.
                  Although my email address says spam, it is real and I read it.

                  Comment

                  • Dave Thompson

                    #10
                    Re: write a multidimensiona l array in a file

                    On Mon, 18 Jul 2005 02:21:50 GMT, I wrote:
                    [color=blue]
                    > On Wed, 13 Jul 2005 13:48:26 -0700, Barry Schwarz <schwarzb@deloz .net>
                    > wrote:
                    >[color=green]
                    > > On 12 Jul 2005 23:51:49 -0700, steph_de_marsei lle@yahoo.fr wrote:[/color]
                    > <snip>[color=green][color=darkred]
                    > > > fprintf(file1," %f %f\ %f %f \n",array[i][0],array[i][1]);[/color]
                    > >
                    > > This contains a syntax error and invokes undefined behavior.
                    > > Backslash-blank is not a valid escape sequence like \n. You have four
                    > > format specifiers but only two arguments t6o satisfy them.[/color]
                    >
                    > Nit: the (or any) syntax error also invokes UB, but if this phrasing
                    > was meant to say that the extra arguments to fprintf() cause UB,
                    > that's wrong. They're safely discarded (7.19.6.1p2) just as they are
                    > for other (user) vararg functions. Unless of course the arguments
                    > cause UB within themselves, like two assignments to same object.
                    >[/color]
                    AARGH! My brain apparently ran backwards temporarily. Sorry.
                    This is (would be) missing not extra arguments and is indeed UB.
                    - David.Thompson1 at worldnet.att.ne t

                    Comment

                    Working...