ich hab das jetzt mal so zu lösen probiert, wobei die Lösung funktioniert, aber wahrscheinlich keinen Eleganzpreis gewinnen wird ...
Code:
void read_and_replace(FILE *f, script *scripts, int script_count) {
char *line;
int length = 0;
line = (char *) malloc(sizeof(char) * LINE);
while (fgets(line, LINE, f) != NULL) {
for (int i = 0; i < script_count; i++) {
length = strlen(line) - strlen(scripts[i].old) + strlen(scripts[i].new) + 1;
char *temp = (char *) malloc(sizeof(char) * length);
char *pos = (char *) malloc(sizeof(char) * length);
char *out = (char *) malloc(sizeof(char) * length);
pos = strstr(line, scripts[i].old);
while (pos != NULL) {
temp = strncpy(temp, line, strlen(line) - strlen(pos));
temp = strcat(temp, scripts[i].new);
out = strcat(out, temp);
line = strcpy(line, pos + strlen(scripts[i].old));
pos = strstr(line, scripts[i].old);
}
out = strcat(out, line);
line = strcpy(line, out);
}
fprintf(stdout, "%s", line);
}
}
Würde mich über Hinweise freuen, wenn ich etwas gröber ungünstig gelöst habe.
Bookmarks