Enter your email address:


Monday, September 2, 2013

Home » , » Write a program to replace a specified line in a text file.

Write a program to replace a specified line in a text file.

Questions: Top-30-c-programs-asked-in-interview_6304.html

17.Write a program to replace a specified line in a text file.
Program: Program to replace a specified line in a text file.
#include
int main(void) {
FILE *fp1, *fp2;
// 'filename'i s a 40 character string to store filename
char filename[4 0];
char c;
int del_line, temp = 1;
//asks user for file name
printf("En ter file name:");
// receives file name from user and stores in'filename'
scanf("%s" , filename);
fp1 = fopen(file name,"r");
//open file in read mode
c = getc(fp1);
//print the contents of file .
while (c != EOF){
printf("%c ", c);
c = getc(fp1);
}
//ask user for line number to be deleted.
printf("\n Enter line number to be deleted and replaced") ;
scanf("%d" ,&del_line) ;
//take fp1 to start point.
rewind(fp1 );
//open copy.c in write mode
fp2 = fopen("cop y.c","w");
c = getc(fp1);
while (c != EOF){
if (c =='\n'){
temp++;
}
// till the line to be deleted comes,copy the content from one file to other
if (temp != del_line){
putc(c, fp2);
}
else //when the line to be deleted comes
{
while ((c = getc(fp1)) !='\n'){
}
//read and skip the line ask for new text
printf("En ter new text");
//flush the input stream
fflush(std in);
putc('\n', fp2);
//put'\n'in new file
while ((c = getchar()) !='\n')
putc(c, fp2);
//take the data from user and place it in new file
fputs("\n" , fp2);
temp++;
}
// continue this till EOF is encountere d
c = getc(fp1);
}
//close both files
fclose(fp1 );
fclose(fp2 );
//remove original file
remove(fil ename);
//rename new file with old name opens the file in read mode
rename("co py.c", filename);
fp1 = fopen(file name,"r");
//reads the character from file
c = getc(fp1);
// until last character of file is encountered
while (c != EOF){
printf("%c ", c);
// all characters are printed
c = getc(fp1);
}
//close the file pointer
fclose(fp1 );
return 0;
}
Output:
Enter file name:abc.t xt
hi.
hello
how are you?
hope the same
Enter line number of the line to be deleted and replaced:4
Enter new text: sayonara see you soon
hi.
hello
how are you?
sayonara see you soon
Explanatio n:
In this program, the user is asked to type the name of the file. The File by name entered by user is opened in
read mode. The line number of the line to be replaced is asked as input. Next the data to be replaced is asked. A
new file is opened in write mode named"copy.c". Now the contents of original file are transferre d into new file
and the line to be modified is deleted. New data is stored in its place and remaining lines of the original file are
also transferre d. The copied file with modified contents is replaced with the original file's name. Both the file
pointers are closed and the original file is again opened in read mode and the contents of the original file is
printed as output.
Share this games :

0 comments:

Post a Comment

Related Posts