-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileHandling4.c
77 lines (75 loc) · 1.98 KB
/
FileHandling4.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
/*
* C Program to Replace a specified Line in a Text File
*/
#include <stdio.h>
int main(void)
{
FILE *fileptr1, *fileptr2;
char filechar[40];
char c;
int delete_line, temp = 1;
printf("Enter file name: ");
scanf("%s", filechar);
fileptr1 = fopen(filechar, "r");
c = getc(fileptr1);
//print the contents of file .
while (c != EOF)
{
printf("%c", c);
c = getc(fileptr1);
}
printf(" \n Enter line number to be deleted and replaced");
scanf("%d", &delete_line);
//take fileptr1 to start point.
rewind(fileptr1);
//open replica.c in write mode
fileptr2 = fopen("replica.c", "w");
c = getc(fileptr1);
while (c != EOF)
{
if (c == 'n')
{
temp++;
}
//till the line to be deleted comes,copy the content to other
if (temp != delete_line)
{
putc(c, fileptr2);
}
else
{
while ((c = getc(fileptr1)) != 'n')
{
}
//read and skip the line ask for new text
printf("Enter new text");
//flush the input stream
fflush(stdin);
putc('n', fileptr2);
//put 'n' in new file
while ((c = getchar()) != 'n')
putc(c, fileptr2);
//take the data from user and place it in new file
fputs("n", fileptr2);
temp++;
}
//continue this till EOF is encountered
c = getc(fileptr1);
}
fclose(fileptr1);
fclose(fileptr2);
remove(filechar);
rename("replica.c", filechar);
fileptr1 = fopen(filechar, "r");
//reads the character from file
c = getc(fileptr1);
//until last character of file is encountered
while (c != EOF)
{
printf("%c", c);
//all characters are printed
c = getc(fileptr1);
}
fclose(fileptr1);
return 0;
}