EXERCISE-16
1. Write a program in C to append multiple lines at the end of a text file.
SOURCE CODE:
#include<stdio.h>
int main() {
FILE *fp;
char ch;
fp = fopen("Sample.txt", "w");
printf("Enter the text press ctrl+z for end of file: \n");
while ((ch=getchar())!=EOF) {
putc(ch,fp);
}
fclose(fp);
fp = fopen("Sample.txt", "a");
printf("Enter the text to append to a file press ctrl+z for end of file: \n");
while ((ch=getchar())!=EOF) {
putc(ch,fp);
}
fclose(fp);
fp = fopen("Sample.txt", "r");
printf("File content after appending : \n");
while ((ch=fgetc(fp))!=EOF) {
putchar(ch);
}
printf("\n");
fclose(fp);
return 0;
}
OUTPUT:
Enter the text press ctrl+z for end of file:
Hello
^Z
Enter the text to append to a file press ctrl+z for end of file:
Welcome
here you learn file concepts.
^Z
File content after appending :
Hello
Welcome
here you learn file concepts.
2. Write a program in C to copy a file in another name.
SOURCE CODE:
#include<stdio.h>
int main() {
FILE *fp1, *fp2;
char ch;
fp1 = fopen("Sample.txt", "w");
printf("Enter the text press ctrl+z for end of file: \n");
while ((ch=getchar())!=EOF) {
putc(ch,fp1);
}
fclose(fp1);
fp1 = fopen("Sample.txt", "r");
fp2 = fopen("CopiedFile.txt", "w");
while((ch=fgetc(fp1))!=EOF){
putc(ch,fp2);
}
putc(ch,fp2);
fclose(fp1);
fclose(fp2);
fp2 = fopen("CopiedFile.txt", "r");
printf("Copied text is : \n");
while((ch=fgetc(fp2))!=EOF){
putchar(ch);
}
fclose(fp2);
printf("\n");
return 0;
}
OUTPUT:
Enter the text press ctrl+z for end of file:
welcome
learn file concepts
^Z
Copied text is :
welcome
learn file concepts
3. Write a program in C to remove a file from the disk.
SOURCE CODE:
#include<stdio.h>
int main() {
FILE *fp;
char ch, fileName[30]="Sample.txt";
int status;
fp = fopen(fileName, "w");
printf("Enter the text press ctrl+z for end of file: \n");
while ((ch=getchar())!=EOF) {
putc(ch,fp);
}
fclose(fp);
fp = fopen(fileName,"r");
if(fp==NULL){
printf("File doesn't exists.\n");
return 0;
}
else{
printf("File exists.\n");
}
fclose(fp);
status = remove(fileName);
if(status==0)
printf("%s file is deleted.",fileName);
else
printf("Unable to delete %s file.",fileName);
return 0;
}
OUTPUT:
Enter the text press ctrl+z for end of file:
hello
^Z
File exists.
Sample.txt file is deleted.
SOURCE CODE:
#include<stdio.h>
int main() {
FILE *fp;
char ch;
fp = fopen("Sample.txt", "w");
printf("Enter the text press ctrl+z for end of file: \n");
while ((ch=getchar())!=EOF) {
putc(ch,fp);
}
fclose(fp);
fp = fopen("Sample.txt", "a");
printf("Enter the text to append to a file press ctrl+z for end of file: \n");
while ((ch=getchar())!=EOF) {
putc(ch,fp);
}
fclose(fp);
fp = fopen("Sample.txt", "r");
printf("File content after appending : \n");
while ((ch=fgetc(fp))!=EOF) {
putchar(ch);
}
printf("\n");
fclose(fp);
return 0;
}
OUTPUT:
Enter the text press ctrl+z for end of file:
Hello
^Z
Enter the text to append to a file press ctrl+z for end of file:
Welcome
here you learn file concepts.
^Z
File content after appending :
Hello
Welcome
here you learn file concepts.
2. Write a program in C to copy a file in another name.
SOURCE CODE:
#include<stdio.h>
int main() {
FILE *fp1, *fp2;
char ch;
fp1 = fopen("Sample.txt", "w");
printf("Enter the text press ctrl+z for end of file: \n");
while ((ch=getchar())!=EOF) {
putc(ch,fp1);
}
fclose(fp1);
fp1 = fopen("Sample.txt", "r");
fp2 = fopen("CopiedFile.txt", "w");
while((ch=fgetc(fp1))!=EOF){
putc(ch,fp2);
}
putc(ch,fp2);
fclose(fp1);
fclose(fp2);
fp2 = fopen("CopiedFile.txt", "r");
printf("Copied text is : \n");
while((ch=fgetc(fp2))!=EOF){
putchar(ch);
}
fclose(fp2);
printf("\n");
return 0;
}
OUTPUT:
Enter the text press ctrl+z for end of file:
welcome
learn file concepts
^Z
Copied text is :
welcome
learn file concepts
3. Write a program in C to remove a file from the disk.
SOURCE CODE:
#include<stdio.h>
int main() {
FILE *fp;
char ch, fileName[30]="Sample.txt";
int status;
fp = fopen(fileName, "w");
printf("Enter the text press ctrl+z for end of file: \n");
while ((ch=getchar())!=EOF) {
putc(ch,fp);
}
fclose(fp);
fp = fopen(fileName,"r");
if(fp==NULL){
printf("File doesn't exists.\n");
return 0;
}
else{
printf("File exists.\n");
}
fclose(fp);
status = remove(fileName);
if(status==0)
printf("%s file is deleted.",fileName);
else
printf("Unable to delete %s file.",fileName);
return 0;
}
OUTPUT:
Enter the text press ctrl+z for end of file:
hello
^Z
File exists.
Sample.txt file is deleted.
No comments