-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path17_FileOperations.c
56 lines (51 loc) · 1.01 KB
/
17_FileOperations.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
/*
17. Write a program to perform file operations.
*/
#include <stdio.h>
void open(char path[20], char mode) {
FILE * fp = fopen(path, mode);
}
void read(char path[20]) {
FILE * fp = fopen(path, "r");
char ch;
while((ch = getc(fp)) != EOF) {
printf("%c", ch);
}
fclose(fp);
}
void write(char path[20], char string[]) {
FILE * fp = fopen(path, "w");
fprintf(fp, "%s", string);
fclose(fp);
}
int main() {
int input;
char path[20], string[256];
char mode;
printf("Enter the name of the file: ");
scanf("%s", path);
while(1) {
printf("\n");
printf("Enter 1 to Open\nEnter 2 to Read\nEnter 3 to Write: ");
scanf("%d", &input);
switch(input) {
case 1:
printf("Enter the mode: ");
scanf("%c", &mode);
open(path, mode);
break;
case 2:
read(path);
break;
case 3:
printf("Enter Some Text: ");
scanf("%s", string);
write(path, string);
break;
default:
printf("Wrong Input!");
break;
}
}
return 0;
}