-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRead ,Write and Append in a text file in c.c
105 lines (90 loc) · 2.34 KB
/
Read ,Write and Append in a text file in c.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include<stdio.h>
#include<stdlib.h>
int main()
{
printf("Enter 1,2,3,4 for the corresponding command:\n");
printf("1. WRITE\n2. READ\n3. Append\n4. Exit\n");
int command;
char name[20];
char ID[20];
char Department[20];
char CGPA[20];
while(1)
{
scanf("%d",&command);
if(command==1)
{
FILE *f;
f= fopen("textfile.txt","w");
printf("Enter your name: ");
scanf("%s",name);
fputs(name,f);
fputs(" ",f);
printf("Enter your ID: ");
scanf("%s",ID);
fputs(ID,f);
fputs(" ",f);
printf("Enter your Department: ");
scanf("%s",Department);
fputs(Department,f);
fputs(" ",f);
printf("Enter your CGPA: ");
scanf("%s",CGPA);
fputs(CGPA,f);
fputs("\n",f);
fclose(f);
}
else if(command==2)
{
FILE *f;
f= fopen("textfile.txt","r");
if(f==NULL)
{
printf("File Not Exist\n");
continue;
}
char ch=fgetc(f);
while(ch!=EOF)
{
printf("%c",ch);
ch=fgetc(f);
}
}
else if(command==3)
{
FILE *f;
f= fopen("textfile.txt","a");
if(f==NULL)
{
printf("File Not Exist\n");
continue;
}
printf("Enter your name: ");
scanf("%s",name);
fputs(name,f);
fputs(" ",f);
printf("Enter your ID: ");
scanf("%s",ID);
fputs(ID,f);
fputs(" ",f);
printf("Enter your Department: ");
scanf("%s",Department);
fputs(Department,f);
fputs(" ",f);
printf("Enter your CGPA: ");
scanf("%s",CGPA);
fputs(CGPA,f);
fputs("\n",f);
fclose(f);
}
else if(command==4)
{
exit(0);
}
else
{
printf("Invalid Input. Enter again.\n");
}
}
return 0;
}