//Q1. Create a file named test.
txt and write some text “Welcome to my college” to the
file.
#include<stdio.h>
#include<stdlib.h>
int main()
{
FILE *fptr;
char filepath[50],ch;
printf("Enter a file path and file name:");
scanf("%s",filepath);
fptr=fopen(filepath,"w");
if(fptr==NULL)
{
printf("Cannot create a file.");
exit(0);
}
// fputs("Welcome to my college.",fptr);
printf("Enter text. hit enter to stop.\n");
fflush(stdin);
while((ch=getchar())!='\n');
{
fputc(ch, fptr);
}
fclose(fptr);
return 0;
}
//Write a program to open file “test.txt” created in previous question,
//read its content and display it on screen.
#include<stdio.h>
#include<stdlib.h>
int main()
FILE *fptr;
char filepath[50],ch;
printf("Enter a file path and file name:");
scanf("%s",filepath);
fptr=fopen(filepath,"r");
if(fptr==NULL)
printf("Cannot Read a file.");
exit(0);
while((ch=fgetc(fptr))!=EOF)
putchar(ch);
fclose(fptr);
return 0;
}
//4.Write a program to append some text to a certain file. Take name from user.
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
FILE *fptr;
char filepath[30], str[100];
printf("ENter a file path:");
scanf("%s",filepath);
fptr=fopen(filepath,"a");
if(fptr==NULL)
{
printf("Cannot create a file.");
exit(0);
}
puts("Enter a string:\n");
fflush(stdin);
gets(str);
fputs(str,fptr);
printf("File is written...");
fclose(fptr);
return 0;
}
Q5. Write a program to create a file named student .txt in d:\test\ drive and write name,
roll and marks of a student to this file.
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
FILE *p;
char filepath[50],name[50];
int age;
float height;
printf("Enter File path:");
scanf("%s",filepath);
p=fopen(filepath, "w+");
if(p==NULL){
printf("Cannot create a file.");
getch();
exit(0);
}
printf("Enter your name:");
fflush(stdin);
gets(name);
printf("Enter your age:");
scanf("%d",&age);
printf("Enter your height:");
scanf("%f",&height);
fprintf(p,"%s %d %.2f",name,age,height);
rewind(p);
printf("Your Details:\n");
while(fscanf(p,"%s %d%f",name,&age,&height)!=EOF)
{
printf("Name:%s\tAge:%d\tHeight:%.2f\n",name,age,height);
}
fclose(p);
getch();
return 0;
}
//6.Write a program read name, address, and telephone, save it in a file and display them
on the screen.
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
int main() {
FILE *fptr;
char name[50], address[100], filepath[50], tel[15];
printf("Enter a filepath: ");
scanf("%s", filepath);
fptr = fopen(filepath, "w+");
if (fptr == NULL) {
printf("Cannot create a file");
getch();
exit(0);
}
printf("Enter a Name: ");
scanf("%s", name);
printf("Enter your address: ");
scanf(" %s", address);
fflush(stdin);
printf("Enter your telephone: ");
scanf("%d", &tel);
fprintf(fptr, "%s %s %d\n", name, address, tel);
rewind(fptr);
while (fscanf(fptr, "%s %s %d", name, address, tel) != EOF) {
printf("Name: %s\tAddress: %s\tTelephone: %d\n", name, address, tel);
}
fclose(fptr);
return 0;
}
//7.Write a program to store string to the data file “new1.dat” and display the same
content on the
// monitor reading from the data file.#include<stdio.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
int main() {
FILE *fptr;
char str[100], filepath[50];
printf("Enter a filepath: ");
scanf("%s", filepath);
fptr = fopen(filepath, "w+b");
if (fptr == NULL) {
printf("Cannot create a file");
getch();
exit(0);
}
puts("Enter a stirng.");
fflush(stdin);
gets(str);
fputs(str, fptr);
printf("file is written....\n");
fclose(fptr);
if(fptr==NULL)
{
printf("Cannot read file");
getch();
exit(0);
}
fgets(str,100,fptr);
puts("Content from file...");
puts(str);
fclose(fptr);
return 0;
}
//8.Write a program to write and read even numbers between 10 and 50 to a file named
evennumbers.txt in
//d:\test\evennumbers.txt.
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
int main() {
FILE *fptr;
char filepath[50];
printf("Enter a filepath: ");
scanf("%s", filepath);
fptr = fopen(filepath, "w+");
if (fptr == NULL) {
printf("Cannot create a file");
getch();
exit(0);
}
int i, even_num=50;
for(i=10;i<=50;i++)
{
if(i%2==0){
fprintf(fptr,"%d\n",i);
}
}
rewind(fptr);
printf("content form file.....\n");
while((fscanf(fptr,"%d",&i))!=EOF)
{
printf("%d, ",i);
}
return 0;
}
//9.Write a program to create a data file and write the natural numbers 1 to 20 to this file
and then read the numbers from the file to display the twice of the stored numbers.
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
int main() {
FILE *fptr;
char filepath[50];
printf("Enter a filepath: ");
scanf("%s", filepath);
fptr = fopen(filepath, "w+");
if (fptr == NULL) {
printf("Cannot create a file");
getch();
exit(0);
}
int i;
for(i=1;i<=20;i++)
{
fprintf(fptr,"%d\n",i);
}
rewind(fptr);
printf("Content from file displaying twice of numbers.....\n");
while((fscanf(fptr,"%d",&i))!=EOF)
{
printf("%d ",i*2);
}
return 0;
}
//10.Write a program that asks user for 10 numbers and stores them in an array. Write this
array to a file and
//read it to display on the screen
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
int main() {
FILE *fptr;
char filepath[50];
printf("Enter a filepath: ");
// scanf("%s", filepath);
fptr = fopen("F:\\111\\info1.txt", "w+");
if (fptr == NULL) {
printf("Cannot create a file");
getch();
exit(0);
}
int i, num[10];
for(i=0;i<10;i++)
{
printf("Enter number %d:",i+1);
scanf("%d ", &num[i]);
}
for(i=0;i<10;i++)
{
fprintf(fptr, "%d\n",num[i]);
}
rewind(fptr);
for(i=0;i<10;i++)
{
fscanf(fptr,"%d",&num[i]);
printf("%d ", num[i]);
}
return 0;
}
//11.Write a program to store information about students (the attributes of students are
ID, name, gender and age)
//in file with following operation.
//a. Read information of ‘n’ numbers of students from the user and store it in the file.
//b. Display all the records of student whose gender is female.
//c. Display names of all students.
//d. Display the record of oldest student from student’s information stored in the file.
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
int main() {
FILE *fptr;
int num;
char name[50], gender[20], filepath[50], tel[15];
printf("Enter a filepath: ");
scanf("%s", filepath);
fptr = fopen(filepath, "w+");
if (fptr == NULL) {
printf("Cannot create a file");
getch();
exit(0);
}
printf("Enter number of students:");
scanf("%d",&num);
int i, id[num], age[num];
for(i=0;i<num;i++)
{
printf("Enter your ID %d:",i+1);
scanf("%d",&id[i]);
printf("Enter your name %d:",i+1);
scanf("%s",name);
printf("Enter your gender %d:",i+1);
scanf("%s",gender);
printf("Enter your age %d:",i+1);
scanf("%d",&age[i]);
}
//a. Read information of ‘n’ numbers of students from the user and store it in the
file.
//storing in file
for(i=0;i<num;i++)
{
fprintf(fptr, "%d%s%s%d",id[i],name[i],gender[i],age[i]);
}
//reading from file
for(i=0;i<num;i++)
{
fscanf(fptr,"%d %s %s %d",&id[i],name[i],gender[i],&age[i]);
printf("ID:%d\tName:%s\tGender:%s\tAge:
%d",id[i],name[i],gender[i],age[i]);
}
//b. Display all the records of student whose gender is female.
for(i=0;i<num;i++)
{
if (strcmp(gender, "female") == 0)
{
fscanf(fptr,"%d %s %s %d",&id[i],name[i],gender[i],&age[i]);
printf("ID:%d\tName:%s\tGender:%s\tAge:
%d",id[i],name[i],gender[i],age[i]);
}
}
return 0;
}