Re: Pointers, arrays , all muddled up !

From: Irrwahn Grausewitz (irrwahn33_at_freenet.de)
Date: 03/22/04

  • Next message: Scott Robert Ladd: "Re: Programmer knowledge"
    Date: Mon, 22 Mar 2004 15:18:36 +0100
    
    

    pandisv@yahoo.co.uk (Vassilis Pandis) wrote:
    >Hi. I am trying to write a program (to solve a programming problem)
    >that will read a file and store its contents in a 2 dimensional array,
    >but I seem to have trouble and despite my efforts, I have not found
    >the problem. The file begins with two numbers, n and m which indicate
    >the number of lines and the length of these lines respectively.
    <snip>
    >Anyway, the problem is that although ( as far as I believe) the
    >database is setup correctly, a problem arises when the line is copied
    >into the database. The code is here and the debugging output follows
    >it. I also include the test file. I would like to note that within the
    >test file are many databases which must be copied, hence the 'while'
    >loop.
    <code snipped>

    Sorry, but your code indeed contains too many errors to sensibly
    correct them in place. Below you'll find my _quick'n'dirty_
    approach to your assignment. Please note that it may contain
    several errors, too, and certainly can be improved in many ways.

    Now my suggestion: do a paper printout of both programs, get a
    scratchpad, a well sharpened pencil and a good cup of tea (or
    whatever you like), and manually step through the algorithms
    to understand what actions they actually perform. If you want
    my code to be checked for errors thoroughly (read: shred into
    pieces :-)), feel free to repost/cross-post it to comp.lang.c -
    that's where the C experts hang out. Code follows.

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    #define BIG_ENOUGH 256
    #define INFILENAME "input"

    int do_error( const char* msg )
    {
        fprintf( stderr, "Error: %s\n", msg );
        return EXIT_FAILURE;
    }

    int main( void )
    {
      int res = EXIT_SUCCESS;
      size_t i;
      size_t n;
      size_t m;
      size_t numlines = 0;
      char line[ BIG_ENOUGH ];
      char **db = NULL;
      FILE *fp;

      if ( (fp = fopen( INFILENAME, "r" )) == NULL )
      {
        res = do_error( "fopen" );
      }
      else
      {
        while( fgets( line, sizeof line, fp ) != NULL )
        {
          if ( sscanf( line, "%u%u", &n, &m ) < 2 )
          {
            res = do_error( "illegal file format" );
            break;
          }
          printf( "n:%u, m:%u\n", n, m );
          if ( n == 0 )
            break;
          if ( (db = realloc(db, (numlines+n) * sizeof *db)) == NULL )
          {
            res = do_error( "realloc" );
            break;
          }
          for ( i = numlines; i < numlines + n; i++ )
          {
            if ( (db[ i ] = malloc( m + 1 )) == NULL )
            {
              res = do_error( "malloc" );
              break;
            }
            if ( fgets( line, sizeof line, fp ) == NULL )
            {
              res = do_error( "premature end of input file" );
              break;
            }
            strcpy( db[ i ], line );
            printf( "%s", db[ i ] );
          }
          numlines = i;
          if ( res != EXIT_SUCCESS )
            break;
        }
        fclose( fp );
      }
      
      for ( i = 0; i < numlines; i++ )
        free( db[ i ] );
      free( db );

      return res;
    }

    Suggestions and/or corrections welcome.

    HTH
    Regards

    -- 
    Irrwahn 
    (irrwahn33@freenet.de)
    

  • Next message: Scott Robert Ladd: "Re: Programmer knowledge"

    Relevant Pages

    • Need help with multi-dimensional arrays and functions
      ... I'm attempting to write a program to read in database files. ... parameters telling me the dimensions of the array. ... int i,j,nrecords,nfields,nchars; ... printf; ...
      (comp.lang.c)
    • Re: [PATCH]: New implementation of scsi_execute_async()
      ... int res = 0; ... struct scatterlist *hdr; ... +static int blk_rq_handle_align(struct request *rq, struct scatterlist **psgl, ...
      (Linux-Kernel)
    • Re: Replacing merge fields in headers/footers
      ... > programmatically update data in a document from a database. ... > private void UpdateField(Word.Field theField, int theFileID, int ... Unsolicited questions ...
      (microsoft.public.word.vba.general)
    • Re: Replacing merge fields in headers/footers
      ... > programmatically update data in a document from a database. ... > private void UpdateField(Word.Field theField, int theFileID, int ... Unsolicited questions ...
      (microsoft.public.dotnet.framework.interop)
    • Re: Inheritance & static members
      ... > int id; ... Perhaps your design is more obvious to those with more database experience ... Ensuring that each child of Name_Lookup has ... of the static member: ...
      (comp.lang.cpp)