|
From: Matthews <dem...@uw...> - 2003-09-19 20:23:37
|
I'm working on a program to generate makefiles for me, and I currently have
it set up to read different sections of a file to generate the options.
One section is %input, which has a list of filenames to include, eg.
prj.txt
%input
main.c
vid.c
gui.c
I read these into a struct organized like so:
struct prj_info
{
...
char **input;
...
}
And I allocate space based on how many files there are (an array of
strings, basically)
My problem is that I get errors when running the following C code:
if(!strncmp("%include",t,strlen(t)-1))
{
count = 0;
skip_line(buf);
getword(buf,t);
longest_length = strlen(t);
info.input = (char *)malloc((sizeof(char)*longest_len)*1);
if(!info.input)
{
printf("OH NOS\n");
return 0;
}
//CRASH HERE
strcpy(info.input[count],t);
while(*buf!='%')
{
while(*buf=='#'||isspace(*buf)) skip_line(buf);
getword(buf,t);
if(strlen(t)>longest_length) longest_length = strlen(t);
count++;
realloc(info.input,(sizeof(char)*longest_length)*count);
if(!info.input)
{
printf("ERR0RZ\n");
return 0;
}
strcpy(info.input[count],t);
skip_line(buf);
}
}
Buf is the dumped contents of a file (like prj.txt), getword() places a
word from buf into t and skip_line() skips to the next line in buf.
The code compiles fine, but crashes at the first strcpy(). Any help would
be appreciated.
|