c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LINE_LENGTH 2048
void print_menu() {
printf("==============TEXT EDITOR==============\n");
printf("1. Append line\n");
printf("2. Insert line\n");
printf("3. Delete line\n");
printf("4. Modify line\n");
printf("5. Print all the lines\n");
printf("6. Exit\n");
printf("Enter your choice:\n");
}
int count_lines(FILE *fp) {
char ch;
int lines = 0;
while (!feof(fp)) {
ch = fgetc(fp);
if (ch == '\n') {
lines++;
}
}
return lines;
}
char** read_lines_from_file(FILE *fp, int lines) {
char **text = (char**)malloc(sizeof (char*) * lines);
char buffer[MAX_LINE_LENGTH];
int i = 0;
fseek(fp, 0, SEEK_SET);
while (fgets(buffer, MAX_LINE_LENGTH, fp)) {
text[i] = (char*)malloc(sizeof (char) * strlen(buffer));
strcpy(text[i], buffer);
i++;
}
return text;
}
void write_lines_to_file(FILE *fp, char **text, int lines) {
fseek(fp, 0, SEEK_SET);
for (int i = 0; i < lines; i++) {
fputs(text[i], fp);
}
}
void append_line(char **text, int *lines) {
char buffer[MAX_LINE_LENGTH];
printf("Enter the line to append:\n");
fgets(buffer, MAX_LINE_LENGTH, stdin);
text[*lines] = (char*)malloc(sizeof (char) * strlen(buffer));
strcpy(text[*lines], buffer);
(*lines)++;
}
void insert_line(char **text, int *lines) {
char buffer[MAX_LINE_LENGTH];
int index;
printf("Enter the line to insert:\n");
fgets(buffer, MAX_LINE_LENGTH, stdin);
printf("Enter the index to insert at:\n");
scanf("%d", &index);
getchar(); // ignore the newline character
for (int i = *lines; i > index; i--) {
text[i] = text[i - 1];
}
text[index] = (char*)malloc(sizeof (char) * strlen(buffer));
strcpy(text[index], buffer);
(*lines)++;
}
void delete_line(char **text, int *lines) {
int index;
printf("Enter the index to delete:\n");
scanf("%d", &index);
getchar(); // ignore the newline character
free(text[index]);
for (int i = index; i < *lines - 1; i++) {
text[i] = text[i + 1];
}
(*lines)--;
}
void modify_line(char **text, int lines) {
char buffer[MAX_LINE_LENGTH];
int index;
printf("Enter the index to modify:\n");
scanf("%d", &index);
getchar(); // ignore the newline character
printf("Enter the new line:\n");
fgets(buffer, MAX_LINE_LENGTH, stdin);
free(text[index]);
text[index] = (char*)malloc(sizeof (char) * strlen(buffer));
strcpy(text[index], buffer);
}
void print_lines(char **text, int lines) {
printf("============TEXT EDITOR============\n");
for (int i = 0; i < lines; i++) {
printf("%d: %s", i, text[i]);
}
}
int main() {
FILE *fp;
fp = fopen("test.txt", "r+");
int lines = count_lines(fp);
char **text = read_lines_from_file(fp, lines);
int choice = 0;
while (choice != 6) {
print_menu();
scanf("%d", &choice);
getchar(); // ignore the newline character
switch (choice) {
case 1:
append_line(text, &lines);
break;
case 2:
insert_line(text, &lines);
break;
case 3:
delete_line(text, &lines);
break;
case 4:
modify_line(text, lines);
break;
case 5:
print_lines(text, lines);
break;
case 6:
break;
default:
printf("Invalid choice.\n");
break;
}
write_lines_to_file(fp, text, lines);
}
fclose(fp);
for (int i = 0; i < lines; i++) {
free(text[i]);
}
free(text);
return 0;
}