-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDelete_Specific_Line_in_File.c
40 lines (40 loc) · 1.28 KB
/
Delete_Specific_Line_in_File.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
/*PROGRAM TO DELETE SPECIFIC LINES FROM A FILE, USING TEMPORARY SWAP FILE METHOD*/
#include<stdio.h>
#include<string.h>
void main(){
char ch,temp_file_name[]="files\\strings.txt"; // i.e. enter valid path/directory for existing file
FILE *file_ptr1=fopen(temp_file_name,"r"),*file_ptr2;
if (file_ptr1==NULL){
printf("Error: File not found!\a");
return;
}
// printf("Enter file name: "); //i.e. for user input
// scanf("%s",temp_file_name);
while (!feof(file_ptr1)){
fscanf(file_ptr1,"%c",&ch);
printf("%c",ch);
}
rewind(file_ptr1);
int delete_line,temp=1;
printf("\nEnter slot number to be deleted: ");
scanf("%d",&delete_line);
file_ptr2=fopen("files\\replica.txt","w");
while (!feof(file_ptr1)){
fscanf(file_ptr1,"%c",&ch);
if (ch=='\n')
temp++;
if (temp!=delete_line)
fprintf(file_ptr2,"%c",ch);
}
fclose(file_ptr1);
fclose(file_ptr2);
remove(temp_file_name);
rename("files\\replica.txt",temp_file_name);
printf("\n The contents of file after being modified are as follows:\n");
file_ptr1=fopen(temp_file_name,"r");
while(!feof(file_ptr1)){
fscanf(file_ptr1,"%c",&ch);
printf("%c",ch);
}
fclose(file_ptr1);
}