C PROGRAM TO DELETE SPECIFIC LINE FROM FILE
- From the user input the file name or path and the specific line number user want delete from file.Here filename is “data.txt” and ‘deleteline’is variable which store line number entered by user.
The file is opened in read mode.
- Created the file name ‘Copyfile” and open in write mode.
- To keep the track of current line temporary variable is used and at starting it initialized as temp=0;
- Read the data from the file which is in read mode .
- If the current line is not equal to the delete line then copy the the data to file which is in write mode.
- Temp variable is incremented. (temp++);
- The above three steps are repeated till end of file(EOF);
- Once all data is read and copied from file close both the files(fclose)
- At last read the file where the data was copied.
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
FILE *fp,*fp1;//fp and fp1 are file pointers;
char ch;
char filename[100];
int temp,deleteline;
fp=fopen(“data.tax”,”r”);
if(fp!=NULL)
{
printf(“file is opened \n”);
}
else
{
printf(“file is not opened \n”);
}
while(ch!=EOF)
{
ch=fgetc(fp);
printf(“%c”,ch);
}
printf(“enter the line you want to delete\n”);
scanf(“%d”,&deleteline);
fp1=fopen(“copyfile2.c”,”w”);
ch=fgetc(fp);
while(ch !=EOF)
{
while(ch !=EOF)
{
ch=fgetc(fp);
if(ch==’\n’)
{
temp++;
if(temp!=deleteline)
{
fputc(ch,fp1);
}
}
}
fclose(fp);
fclose(fp1);
fp=fopen(“copyfile2.c”,”r”);
while(ch!=EOF)
{
ch=fgetc(fp);
printf(“%c”,ch);
}
fclose(fp);
return 0;
}
About us:
Professional Training Institute is one of the top Embedded System training Institute in Bangalore, Our unique training practical based training approach makes every student as knowledgeable and experienced. Our student Practical hands on training in Embedded system makes student employable. And also till now are having 100% placement records in our Embedded system.