0% found this document useful (0 votes)
21 views

File Handling Assignment-1

REG:FILE

Uploaded by

Bhoomi Dangar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

File Handling Assignment-1

REG:FILE

Uploaded by

Bhoomi Dangar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

1.

Write a program to read the integer value from keyboard and store the same content
in a file "data.txt" using write mode.

#include<stdio.h>

int main()

int n,p;

FILE *fp;

fp=fopen("data.txt","w+");

printf("Enter a number you want to enter in file: ");

scanf("%d",&n);

putw(n,fp);

printf("Value added succesfully!");

rewind(fp);

p=getw(fp);

printf("\nInserted value is %d",p);

fclose(fp);

return 0;

2. write a program to read the character from keyboard and store the same content in a
file using write and read mode.

#include<stdio.h>

int main()
{

char n,p;

FILE *fp;

fp=fopen("data2.txt","w+");

printf("Enter a character you want to enter in file:


");

scanf("%c",&n);

putc(n,fp);

printf("Value added successfully!");

rewind(fp);

p=getc(fp);

printf("\nInserted character is %c",p);

fclose(fp);

return 0;

3. Write a program to read the string from keyboard and store the same content in a file
using append mode.

#include<stdio.h>

int main()

char str[50],st[50];

FILE *fp;
fp=fopen("data3.txt","w+");

printf("Enter string: ");

fgets(str,50,stdin);

fputs(str,fp);

printf("Value added succesfully!");

rewind(fp);

fgets(st,50,fp);

printf("\nInserted string is %s",st);

fclose(fp);

return 0;

4. write a program to create a structure of book with book title, author name and price
and store all book records for n books in a file.

#include<stdio.h>

int main()

FILE *fp;

struct books

char book_title[50];

char author_name[50];

int book_price;
};

struct books book[100];

int n,i;

char str[1000];

fp=fopen("data4.txt","w+");

printf("Enter the number of records you want to


insert: ");

scanf("%d",&n);

for(i=0;i<n;i++)

printf("Enter details of book %d\n",i+1);

printf("Enter title of book: ");

scanf("%s",&book[i].book_title);

printf("Enter name of book's author: ");

scanf("%s",&book[i].author_name);

printf("Enter the price of book: ");

scanf("%d",&book[i].book_price);

fprintf(fp,"Book %d\n\tName of Book: %s\n\tName


of author:
%s",i+1,book[i].book_title,book[i].author_name);

fprintf(fp,"\n\tPrice: %d\n",book[i].book_price);

}
rewind(fp);

while(!feof(fp))

fgets(str,1000,fp);

printf("%s",str);

return 0;

5 Write a program create a structure of bank with accno, holder name and balance and
store them in a file for n holders.

#include<stdio.h>

int main()

FILE *fp;

struct bank

int accno;

char holder_name[50];

int balance;

};

struct bank account[100];

int n,i;
char str[1000];

fp=fopen("data5.txt","w+");

printf("Enter the number of records you want to


insert: ");

scanf("%d",&n);

for(i=0;i<n;i++)

printf("Enter details of account holder


%d\n",i+1);

printf("Enter account number: ");

scanf("%d",&account[i].accno);

printf("Enter name of account holder: ");

scanf("%s",&account[i].holder_name);

printf("Enter the balance of account: ");

scanf("%d",&account[i].balance);

fprintf(fp,"Account %d\n\tAccount number:


%d\n\tName of account holder:
%s",i+1,account[i].accno,account[i].holder_name);

fprintf(fp,"\n\tBalance:
%d\n",account[i].balance);

rewind(fp);

while(!feof(fp))
{

fgets(str,1000,fp);

printf("%s",str);

return 0;

6. Write a program to take three numbers from users using structure and write it into
binary file using fwrite().

//Write a program to take three numbers from users using


structure and write it into binary file using fwrite().

#include<stdio.h>

int main()

FILE *fp;
struct numbers{

int num1;

int num2;

int num3;

}number,sec_no;

fp=fopen("number.bin","wb+");

printf("Enter 3 numbers: ");

scanf("%d %d
%d",&number.num1,&number.num2,&number.num3);

fwrite(&number,sizeof(number),1,fp);

rewind(fp);

fread(&sec_no,sizeof(sec_no),1,fp);

printf("num1: %d\tnum2: %d\tnum3:


%d",number.num1,number.num2,number.num3);

fclose(fp);

7. Write a program to take a student details (stud_id, stud_name, stud_mobileno)as a


input using structure and write it into Binary file "studdetails.bin" and read or display it
using fread().

//Write a program to take a student details (stud_id,


stud_name, stud_mobileno)as a input using structure and
write it into Binary file "studdetails.bin" and read or
display it using fread().

#include<stdio.h>

int main()

struct college{

int stu_id;

char stud_name[30];

long long stud_mobileno;

}students[100],clg_studs[100];

FILE *fp;

int n,i;

fp=fopen("studentdetails.bin","wb+");

printf("Enter the number of records you want to


insert: ");

scanf("%d",&n);

for(i=0;i<n;i++)

printf("Enter student id: ");

scanf("%d",&students[i].stu_id);

printf("Enter the name of student: ");

scanf("%s",&students[i].stud_name);

printf("Enter the mobile number of student: ");


scanf("%lld",&students[i].stud_mobileno);

fwrite(&students,sizeof(students),n,fp);

rewind(fp);

fread(&clg_studs,sizeof(clg_studs),n,fp);

for(i=0;i<n;i++)

printf("Student id: %d",clg_studs[i].stu_id);

printf("\nName of student:
%s",clg_studs[i].stud_name);

printf("\nMobile number of student:


%lld\n",clg_studs[i].stud_mobileno);

fclose(fp);

return 0;

You might also like