'How to copy file in buffer without a string
I would like to copy a file in a buffer without a certain string
Here's the code
void remove_line(FILE *fp, char *string, char *output) {
char buff[6];
output = malloc(500);
int i = 0;
while (fgets(buff, 6, fp) != NULL) {
char temp[6];
strcpy(temp, buff);
temp[strcspn(temp, "\n")] = 0;
if (strcmp(temp, string) != 0) {
memcpy(output + i * 6, buff, 6);
i++;
}
}
printf("%s", output);
}
int main(int argc, char *argv[])
{
FILE *f;
f = fopen("file", "a+");
char *output = NULL;
char line[] = "hello";
remove_line(f, line, output);
fclose(f);
}
and the file contains :
hello
world
My goal is to only have "world" in buffer, but currently I have nothing.
Thanks you !
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
