Re: need some help with arrays

From: Leor Zolman (leor_at_bdsoft.com)
Date: 02/26/04

  • Next message: nrk: "Re: Read a binary file until "\name\" is encountered..."
    Date: Thu, 26 Feb 2004 02:01:11 GMT
    
    

    On Thu, 26 Feb 2004 01:20:39 GMT, "pillip" <pillip@pillip.com> wrote:

    >I am trying to use fopen and fget to input two files and then output them
    >into one file. Each input file has two columns and 20 rows, however since
    >the first column in each input file is same ( numbers 0...19), i want the
    >output file to have 3 columns and 20 rows. Right now i am using a one
    >dimensional array "array1[SIZE]" to input each file:, and the output file
    >has 2 columns and 42 rows.
    >

    There are several approaches to this that would work well enough. The most
    general solution, which would work no matter how many rows the two files
    have, would be to read one line's worth of data (not necessarily "a line"
    as you're doing) from each file, create a line of output right away by
    processing that input, and going back for more (if any). In that case you'd
    just have three file streams going at the same time.

    The other approach, if you really want to read one line at a time, is to
    collect the data up in memory and combine it after all the data has been
    read. But unless there's some /really/ good reason for this, such as you
    have to read to the end of the data before being able to calculate
    something that goes at the beginning of the output stream, I'd recommend
    the first approach. It is certainly more memory-efficient, anyway.

    Since you've said you have two columns ("items") on each line of input, you
    may want to consider reading the data in using fscanf to put everything
    immediately into some variables, and then creating your output lines with
    fprintf. That would make it very easy to select what to write (i.e., skip
    the first item from each line of the 2nd file). It would also give you
    ready-made hooks for sanity-checking your input as you read it (the return
    values from fscanf, and the ability to check the values of the variables
    you've just read into).

    HTH,
            -leor

    >#include <stdlib.h>
    >#include <stdio.h>
    >
    >#define SIZE 20
    >
    >main()
    >{
    > char array1[SIZE], array2[SIZE];
    > FILE *fp;
    >
    >
    > if ( (fp = fopen("1a.data", "r")) == NULL)
    > {
    > fprintf(stderr, "Error opening file.");
    > exit(1);
    > }
    >
    > while ( !feof(fp) )
    > {
    > fgets(array1, SIZE, fp);
    > printf("%s",array1);
    > }
    > fclose(fp);
    >
    > printf("\n\n");
    >
    > if ( (fp = fopen("1b.data", "r")) == NULL)
    > {
    > fprintf(stderr, "Error opening file.");
    > exit(1);
    > }
    >
    > while ( !feof(fp) )
    > {
    > fgets(array2, SIZE, fp);
    > printf("%s",array2);
    > }
    > fclose(fp);
    >
    >
    >return(0);
    >}
    >

    Leor Zolman
    BD Software
    leor@bdsoft.com
    www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
    C++ users: Download BD Software's free STL Error Message
               Decryptor at www.bdsoft.com/tools/stlfilt.html


  • Next message: nrk: "Re: Read a binary file until "\name\" is encountered..."