C language implementation deletes the specified line (content) of the file __c language

Source: Internet
Author: User
Tags strcmp
Code:

#include <stdio.h>

#include <string.h>


int main ()
{
FILE *FF,*FP;
int temp=0;//temp is mainly used for a row to determine whether to delete, 0 means not delete, 1 means delete
Char a[1000];//place a row of fgets
Char b[1000];//to save user input
Char c[5]= "*\n";//This sets the specific character to "*"


printf (Please enter the start line to delete: \ n ");
scanf ("%s", b);//Enter the content to be deleted
Strcat (b, "\ n");//Because the Fgets function automatically adds a line break after the character being read, to compare A and B with strcmp, where b plus "\ n"


Ff=fopen ("Index.txt", "R")//Read Open the original file Index.txt
Fp=fopen ("Result.txt", "w");/write open temporary file Result.txt


if (Ff==null | | fp==null) {
printf ("Open file failed \ n");
Exit (0);/quit
}


while (Fgets (A,1000,FF))//Line to execute the contents of index.tzt
{
if (strcmp (a,b)!=0 && temp ==0)//if not equal to the input and temp is not equal to 0, do not delete
{
printf ("%s", a);//The content that is not deleted is exported to the console (consistent with the content of the result file)
Fputs (A,FP);//write content not deleted to Result.txt
}
else//find the line that matches the input
{
temp = 1;
if (strcmp (a,c) ==0)//encounters the next *, it is no longer deleted from the line
temp = 0;
}
}
Fclose (FF);//Close index file
Fclose (FP)//Close result file
return 0;

Execute Program:

Enter List1 to indicate the start of the line from the List1, until "*", the contents of the middle are all deleted

index.txt File
result.txt File


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.