-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTextEditor.c
56 lines (42 loc) · 1.33 KB
/
TextEditor.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_FILE_NAME 256
#define MAX_TEXT_SIZE 10000
int main() {
char file_name[MAX_FILE_NAME];
char text[MAX_TEXT_SIZE];
char ch;
FILE *file;
printf("Enter the name of the file: ");
scanf("%s", file_name);
// Open the file for reading
file = fopen(file_name, "r");
if (file == NULL) {
printf("File not found. Creating a new file.\n");
// Create the file for writing if it doesn't exist
file = fopen(file_name, "w");
if (file == NULL) {
printf("Error creating the file.\n");
return 1;
}
fclose(file);
// Re-open the file for reading and writing
file = fopen(file_name, "r+");
}
// Read and display the content of the file
printf("File content:\n");
while ((ch = fgetc(file)) != EOF) {
putchar(ch);
}
// Clear the input buffer
fseek(file, 0, SEEK_END);
printf("\nEnter new content (Press Ctrl+D to save and exit):\n");
// Read and write text until Ctrl+D is pressed
while (fgets(text, sizeof(text), stdin) != NULL) {
fputs(text, file);
}
printf("File saved and editor closed.\n");
fclose(file);
return 0;
}