This is a C Program to delete a specific line from a text file.
This C Program deletes a specific line from a text file.
Take input from the user and performs delete operations in text file as shown in the program below.
Here is source code of the C Program to delete a specific line from a text file. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C Program Delete a specific Line from a Text File
*/
#include <stdio.h>
int main()
{
FILE *fileptr1, *fileptr2;
char filename[40];
char ch;
int delete_line, temp = 1;
printf("Enter file name: ");
scanf("%s", filename);
//open file in read mode
fileptr1 = fopen(filename, "r");
ch = getc(fileptr1);
` while (ch != EOF)
{
printf("%c", ch);
ch = getc(fileptr1);
}
//rewind
rewind(fileptr1);
printf(" \n Enter line number of the line to be deleted:");
scanf("%d", &delete_line);
//open new file in write mode
fileptr2 = fopen("replica.c", "w");
ch = getc(fileptr1);
while (ch != EOF)
{
ch = getc(fileptr1);
if (ch == '\n')
temp++;
//except the line to be deleted
if (temp != delete_line)
{
//copy all lines in file replica.c
putc(ch, fileptr2);
}
}
fclose(fileptr1);
fclose(fileptr2);
remove(filename);
//rename the file replica.c to original name
rename("replica.c", filename);
printf("\n The contents of file after being modified are as follows:\n");
fileptr1 = fopen(filename, "r");
ch = getc(fileptr1);
while (ch != EOF)
{
printf("%c", ch);
ch = getc(fileptr1);
}
fclose(fileptr1);
return 0;
}
This C Program, we are reading file name using ‘filename’ variable. Using ‘fileptr1’ variable Open the file in read mode. While loop is used to print the number of characters present in the file.
Then rewind() function is used to set the file position to the beginning of the file of the given stream. Enter the line number of the line to be deleted using ‘delete_line’ variable.
Then ‘fileptr2’ variable is used to open the new file in write mode. While loop is used to print the number of characters present in the file. Is condition statement is used to copy except the line to be deleted. The file.Putc() function is used to copy all lines in file replica.c.
Then close the files and rename the file replica.c to original name. Using while loop print the contents of the file after being modified.
$ cc pgm47.c $ a.out Enter file name: pgm1.c /* * C PROGRAM TO CONVERSION FROM Decimal to hexadecimal */ #include<stdio.h> int main() { long int decimalnum, remainder, quotient; int i = 1, j, temp; char hexadecimalnum[100]; printf("Enter any decimal number: "); scanf("%ld", &decimalnum); quotient = decimalnum; while (quotient != 0) { temp = quotient % 16; //To convert integer into character if (temp < 10) temp = temp + 48; else temp = temp + 55; hexadecimalnum[i++] = temp; quotient = quotient / 16; } printf("Equivalent hexadecimal value of decimal number %d: ", decimalnum); for (j = i - 1; j > 0; j--) printf("%c", hexadecimalnum[j]); return 0; } Enter line number of the line to be deleted: 10 The contents of file after being modified are as follows: * * C PROGRAM TO CONVERSION FROM Decimal to hexadecimal */ #include<stdio.h> int main() { long int decimalnum, remainder, quotient; int i = 1, j, temp; printf("Enter any decimal number: "); scanf("%ld", &decimalnum); quotient = decimalnum; while (quotient != 0) { temp = quotient % 16; //To convert integer into character if (temp < 10) temp = temp + 48; else temp = temp + 55; hexadecimalnum[i++] = temp; quotient = quotient / 16; } printf("Equivalent hexadecimal value of decimal number %d: ", decimalnum); for (j = i - 1; j > 0; j--) printf("%c", hexadecimalnum[j]); return 0; }
Sanfoundry Global Education & Learning Series – 1000 C Programs.
Here’s the list of Best Books in C Programming, Data-Structures and Algorithms
- Check C Books
- Check Computer Science Books
- Apply for Computer Science Internship
- Practice Computer Science MCQs
- Apply for C Internship