Re: Pointers, arrays , all muddled up !
From: Irrwahn Grausewitz (irrwahn33_at_freenet.de)
Date: 03/22/04
- Previous message: CBFalconer: "Re: Pointers, arrays , all muddled up !"
- In reply to: Vassilis Pandis: "Pointers, arrays , all muddled up !"
- Next in thread: Irrwahn Grausewitz: "Re: Pointers, arrays , all muddled up !"
- Reply: Irrwahn Grausewitz: "Re: Pointers, arrays , all muddled up !"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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)
- Previous message: CBFalconer: "Re: Pointers, arrays , all muddled up !"
- In reply to: Vassilis Pandis: "Pointers, arrays , all muddled up !"
- Next in thread: Irrwahn Grausewitz: "Re: Pointers, arrays , all muddled up !"
- Reply: Irrwahn Grausewitz: "Re: Pointers, arrays , all muddled up !"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|