hi, i recently began to program in c (i'm very noob) and i have problems appending chars to dynamic allocated strings.
i created this function to append strings:
and it works pretty well, except in this piece of code:
which reads a file with a format url,version\n...
i'm testing it with this test-file:
and prints:
where <corrupted/incorrect string> is a bunch of weird characters.
as you can see, the first print is correct, but the second is wrong and i can't spot the problem out.
can anybody help me with this?
thanks, greetings.
i created this function to append strings:
Code:
char *appstr(char *s, const char *d)
{
size_t slen = 0;
size_t dlen = 0;
char *ns;
if (s)
slen = strlen(s);
if (d)
dlen = strlen(d);
ns = malloc(sizeof(char) * (slen+dlen+1));
strcpy(ns, "");
if (s)
strcat(ns, s);
if (d)
strcat(ns, d);
free(s);
return ns;
}
and it works pretty well, except in this piece of code:
Code:
do {
ch = fgetc(dependency_file);
if (ch == '\n' || ch == EOF) {
dependency = Dependency_create_from_url(dependency_url);
Dependency_set_version(dependency, dependency_version);
printf("%s,%s\n", dependency_url, dependency_version);
if (DependencyContainer_add(dc, dependency) == DEPENDENCY_CONTAINER_VERSION_ERROR) {
if (display_errors == 1) {
printf(" [ERROR] Dependency %s is required in different versions\n", dependency->name);
}
}
free(dependency_url);
free(dependency_version);
dependency_url = NULL;
dependency_version = NULL;
in_version = 0;
} else if (ch == ',') {
in_version = 1;
} else {
if (in_version == 0) {
dependency_url = appstr(dependency_url, &ch);
} else {
dependency_version = appstr(dependency_version, &ch);
}
}
} while(ch != EOF);
which reads a file with a format url,version\n...
i'm testing it with this test-file:
Code:
[email protected]:Ruk33/WurstBuff.git,HEAD
[email protected]:Ruk33/WurstBuff.git,1234
and prints:
Code:
[email protected]:Ruk33/WurstBuff.git,HEAD
<corrupted/incorrect string>,<corrupted/incorrect string>
where <corrupted/incorrect string> is a bunch of weird characters.
as you can see, the first print is correct, but the second is wrong and i can't spot the problem out.
can anybody help me with this?
thanks, greetings.
Last edited: