Q1.
Read text from user, store in file and display
Aim: To read text until @ is entered, store it in a file and display it.
Program:
#include <stdio.h>
int main() {
FILE *fp;
char ch;
fp = fopen("[Link]","w");
printf("Enter the text with @ at end : ");
while((ch=getchar())!='@') {
fputc(ch,fp);
}
fclose(fp);
fp = fopen("[Link]","r");
printf("Given message is : ");
while((ch=fgetc(fp))!=EOF) {
putchar(ch);
}
fclose(fp);
return 0;
}
Q2. Read integers, store in file and display
Aim: To store integers entered by user until 0 is entered.
Program:
#include <stdio.h>
int main() {
FILE *fp;
int num;
fp = fopen("[Link]","w");
printf("Enter numbers up to 0 : ");
while(1) {
scanf("%d",&num);
if(num==0) break;
fprintf(fp,"%d ",num);
}
fclose(fp);
fp = fopen("[Link]","r");
printf("The given numbers are : ");
while(fscanf(fp,"%d",&num)!=EOF) {
printf("%d ",num);
}
fclose(fp);
return 0;
}
Q3. Copy contents of one file into another
Aim: To copy contents from one file to another file.
Program:
#include <stdio.h>
int main() {
FILE *fp1,*fp2;
char ch;
fp1 = fopen("[Link]","w");
printf("Enter the text with @ at end : ");
while((ch=getchar())!='@') {
fputc(ch,fp1);
}
fclose(fp1);
fp1 = fopen("[Link]","r");
fp2 = fopen("[Link]","w");
while((ch=fgetc(fp1))!=EOF) {
fputc(ch,fp2);
}
fclose(fp1);
fclose(fp2);
fp2 = fopen("[Link]","r");
printf("Copied text is : ");
while((ch=fgetc(fp2))!=EOF) {
putchar(ch);
}
fclose(fp2);
return 0;
}
Q4. Delete a file
Aim: To create a file, display its contents and delete it.
Program:
#include <stdio.h>
int main() {
FILE *fp;
char filename[20], ch;
printf("Enter a new file name : ");
scanf("%s",filename);
fp = fopen(filename,"w");
printf("Enter the text with @ at end : ");
getchar();
while((ch=getchar())!='@') {
fputc(ch,fp);
}
fclose(fp);
fp = fopen(filename,"r");
printf("Given message is : ");
while((ch=fgetc(fp))!=EOF) {
putchar(ch);
}
fclose(fp);
remove(filename);
printf("\n%s file is deleted successfully",filename);
return 0;
}
Q5. Store student data in file
Aim: To store multiple student records in a file and display them.
Program:
#include <stdio.h>
int main() {
FILE *fp;
int roll;
char name[50], ch;
float marks;
fp = fopen("[Link]","w");
do {
printf("Enter Roll number : ");
scanf("%d",&roll);
printf("Enter Name : ");
scanf("%s",name);
printf("Enter Marks : ");
scanf("%f",&marks);
fprintf(fp,"%d -> %s -> %f\n",roll,name,marks);
printf("Do you want to add another data (y/n) : ");
scanf(" %c",&ch);
} while(ch=='y'||ch=='Y');
fclose(fp);
fp = fopen("[Link]","r");
printf("Data in the file :\n");
while(fscanf(fp,"%d -> %s -> %f",&roll,name,&marks)!=EOF) {
printf("%d -> %s -> %f\n",roll,name,marks);
}
fclose(fp);
return 0;
}
Q6. Append data to an existing file
Aim: To append text to a file and display its content.
Program:
#include <stdio.h>
int main() {
FILE *fp;
char ch;
fp = fopen("[Link]","w");
printf("Enter the text with @ at end : ");
while((ch=getchar())!='@') {
fputc(ch,fp);
}
fclose(fp);
fp = fopen("[Link]","a");
printf("Enter the text to append with @ at end : ");
getchar();
while((ch=getchar())!='@') {
fputc(ch,fp);
}
fclose(fp);
fp = fopen("[Link]","r");
printf("File content after appending : ");
while((ch=fgetc(fp))!=EOF) {
putchar(ch);
}
fclose(fp);
return 0;
}
Q7. Store employee data in file
Aim: To store multiple employee records in a file and display them.
Program:
#include <stdio.h>
int main() {
FILE *fp;
int id;
char name[50], ch;
float salary;
fp = fopen("[Link]","w");
do {
printf("Enter employee id : ");
scanf("%d",&id);
printf("Enter Name : ");
scanf("%s",name);
printf("Enter Salary : ");
scanf("%f",&salary);
fprintf(fp,"%d -> %s -> %f\n",id,name,salary);
printf("Do you want to add another data (y/n) : ");
scanf(" %c",&ch);
} while(ch=='y'||ch=='Y');
fclose(fp);
fp = fopen("[Link]","r");
printf("Data in the file :\n");
while(fscanf(fp,"%d -> %s -> %f",&id,name,&salary)!=EOF) {
printf("%d -> %s -> %f\n",id,name,salary);
}
fclose(fp);
return 0;
}