#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LINE_LENGTH 1024
int main() {
char filename[MAX_LINE_LENGTH];
printf("Enter filename: ");
if (fgets(filename, MAX_LINE_LENGTH, stdin) == NULL) {
perror("Error reading filename");
return 1;
}
filename[strcspn(filename, "\n")] = '\0'; // remove trailing newline
FILE* outfile = fopen(filename, "a");
if (outfile == NULL) {
perror("Error opening file");
return 1;
}
char str[MAX_LINE_LENGTH];
printf("Enter string to append: ");
if (fgets(str, MAX_LINE_LENGTH, stdin) == NULL) {
perror("Error reading string");
fclose(outfile);
return 1;
}
str[strcspn(str, "\n")] = '\0'; // remove trailing newline
fprintf(outfile, "%s\n", str);
fclose(outfile);
FILE* infile = fopen(filename, "r");
if (infile == NULL) {
perror("Error opening file");
return 1;
}
char line[MAX_LINE_LENGTH];
while (fgets(line, MAX_LINE_LENGTH, infile) != NULL) {
printf("%s", line);
}
fclose(infile);
return 0;
}