said:
> After some changes, it runs fine but still it doesn't shows anything:
For me, however, it doesn't compile. This is hardly surprising, since
you appear to have ignored some of my advice.
foo.c:12: warning: function declaration isn't a prototype
foo.c: In function `ccarray_create':
foo.c:13: warning: implicit declaration of function `malloc'
foo.c:13: warning: cast does not match function type
foo.c:14: warning: cast does not match function type
foo.c: At top level:
foo.c:19: warning: no previous prototype for `ccarray_insert'
foo.c: In function `ccarray_insert':
foo.c:21: parse error before `/'
foo.c:22: warning: control reaches end of non-void function
foo.c: At top level:
foo.c:25: warning: no previous prototype for `ccarray_destroy'
foo.c: In function `ccarray_destroy':
foo.c:26: warning: implicit declaration of function `free'
foo.c: At top level:
foo.c:32: warning: no previous prototype for `ccarray_read_file'
foo.c: In function `ccarray_read_file':
foo.c:38: warning: cast does not match function type
foo.c:48: warning: control reaches end of non-void function
foo.c: At top level:
foo.c:51: warning: no previous prototype for `ccarray_print'
foo.c: In function `ccarray_print':
foo.c:58: warning: control reaches end of non-void function
foo.c: At top level:
foo.c:61: warning: function declaration isn't a prototype
foo.c: In function `main':
foo.c:63: warning: passing arg 2 of `ccarray_read_file' discards
qualifiers from pointer target type
make: *** [foo.o] Error 1
To explain the changes I made, line by line, would be tedious. Note,
however, the significant differences between your program and mine, not
least amongst which is the fact that my version works.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define WORD_LIST(X) "resource/" X
#define INIT_MAX_LINES 1024
#define INIT_LINE_LEN 100
typedef struct char_type
{
char **ptr;
size_t linecount;
size_t maxlines;
} ccarray;
ccarray *ccarray_create(void);
int ccarray_insert(ccarray * c, char *str, size_t pos);
int ccarray_destroy(ccarray * c);
int ccarray_read_file(ccarray * c, const char *filename);
int ccarray_print(ccarray * c);
char *dupstr(const char *s);
char *dupstr(const char *s)
{
size_t len = strlen(s) + 1;
char *new = malloc(len);
if(new != NULL)
{
memcpy(new, s, len);
}
return new;
}
ccarray *ccarray_create(void)
{
ccarray *c = malloc(sizeof *c);
if(c != NULL)
{
c->ptr = malloc(INIT_MAX_LINES * sizeof *c->ptr);
if(c->ptr == NULL)
{
free(c);
c = NULL;
}
else
{
size_t i = 0;
c->linecount = 0;
c->maxlines = INIT_MAX_LINES;
while(i < c->linecount)
{
c->ptr[i++] = NULL;
}
}
}
return c;
}
int ccarray_insert(ccarray * c, char *str, size_t pos)
{
int rc = 0;
if(pos >= c->maxlines)
{
size_t newsize = (c->maxlines * 2 > pos) ?
c->maxlines * 2 : (pos + 1);
char **tmp = realloc(c->ptr, newsize);
if(tmp == NULL)
{
rc = 1;
}
else
{
c->ptr = tmp;
c->maxlines = newsize;
}
}
if(rc == 0)
{
if(pos > c->linecount)
{
size_t i = c->linecount;
while(i < pos)
{
c->ptr[i++] = NULL;
}
c->linecount = pos + 1;
}
else if(pos == c->linecount)
{
++c->linecount;
}
c->ptr[pos] = dupstr(str);
if(c->ptr[pos] == NULL)
{
rc = 2;
}
}
return rc;
}
int ccarray_destroy(ccarray * c)
{
size_t i = 0;
while(i < c->linecount)
{
free(c->ptr[i++]);
}
free(c->ptr);
free(c);
return 0;
}
int ccarray_read_file(ccarray *c, const char *filename)
{
FILE *fptr;
char *line;
int i;
int rc = 0;
fptr = fopen(filename, "r");
if(fptr != NULL)
{
line = malloc(INIT_LINE_LEN);
if(line != NULL)
{
char safefmt[32] = {0};
i = 0;
sprintf(safefmt, "%%%ds", INIT_LINE_LEN - 1);
while(rc == 0 && fscanf(fptr, safefmt, line) == 1)
{
rc = ccarray_insert(c, line, i);
i++;
}
free(line);
}
else
{
rc = 4;
}
fclose(fptr);
}
else
{
rc = 3;
}
return rc;
}
int ccarray_print(ccarray * c)
{
int i = 0;
while(c->ptr[i] != NULL)
{
printf("Line: %s\n", c->ptr[i]);
i++;
}
return 0;
}
int main(void)
{
int rc = 0;
ccarray *c = ccarray_create();
if(c != NULL)
{
rc = ccarray_read_file(c, WORD_LIST("sample.txt"));
if(rc != 0)
{
fprintf(stderr, "read failed: %d\n", rc);
}
else
{
ccarray_print(c);
}
ccarray_destroy(c);
}
return 0;
}
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.