add really crappy text editor

This commit is contained in:
Rokas Puzonas 2023-12-08 20:41:41 +02:00
parent cb07391456
commit 041cc01403
3 changed files with 38 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
main

3
Makefile Normal file
View File

@ -0,0 +1,3 @@
main: main.c
gcc -o main main.c

34
main.c Normal file
View File

@ -0,0 +1,34 @@
#include <stdio.h>
#include <string.h>
void edit_line(char *buffer, int current_line) {
for (int i = 0; i < current_line; i++) {
buffer = strchr(buffer, '\n') + 1;
}
char *line_end = strchr(buffer, '\n');
char saved[1024] = {0};
strcpy(saved, line_end);
scanf("%s", buffer);
strcpy(buffer + strlen(buffer), saved);
}
int main(int argc, char **argv) {
char buffer[1024] = { 0 };
FILE *f = fopen(argv[1], "r");
fread(buffer, sizeof(buffer), 1, f);
fclose(f);
printf("Contents:\n%s\n", buffer);
int current_line = 0;
scanf("%d", &current_line);
edit_line(buffer, current_line);
f = fopen(argv[1], "w");
fwrite(buffer, strlen(buffer), 1, f);
fclose(f);
return 0;
}