Monday, January 20, 2020

File_Class XII


C provides 2 types of files called binary files and text files.
In text file, data stored in terms of text i.e. every character stored in terms of 1 byte. For example: “Class-12” is stored as a 9 bytes string including terminating character ‘\0’.

A binary file contains data that was written in the same format used to store in the main memory in binary format.
The file pointer declare as
            FILE   *f;
Opening, Reading,Writing and Appending on/from data File
1) fopen( ):- This function accepts two arguments as strings. The first argument denotes the name of the file to be opened and the second signifies the mode in which the file is to be opened.
The syntax is as follows
FILE  *f;
f=fopen(“file_name.extention”,  “mode_of_open”);
 Where, file extension  .doc, .docx, .txt etc
The mode of data file
Mode
Description
“r”
Read only mode
“w”
Write only mode
“a”
Append text to already data containing file
“wb”
It creates a binary file for write only mode.
"rb"
Opens a binary file for read only mode
"ab"
Open and create binary file for append mode
2) fclose( ):- The fclose() function is used for closing opened files. The only argument it accepts is the file pointer.
syntax
fclose(f);
3) fgetc( ):- It is used to  receive/read one single character  from data file.
syntax
getc(file_pointer);
4) fputc( )-  It  writes a character to the file.is used to write data to a data file.
syntax
fputc(character, file_pointer);
5) fprintf() - It is used to read data from data file
syntax:
fprintf(file_pointer, “data_format”, variable_name”);
6) fscanf( ):- It is used to write data to a data file
syntax:fscanf(file_pointer, “data_format’, &variable_name, &variable_name,…);
7) fread()-The buffer uis the pointer to a memory area that receives the data from the file.
syntax:-fread(buffer, number_of_bytes, count, file_pointer);
8) fwrite()- It is the pointer to information to be written to the file, the no of byte specifies the size of block to be write or written
syntax
fwrite(buffer, number_of_bytes, count, file_pointer);



Write a program to enter name, roll-number and marks of 10 students and store them in a file.

# include<stdio.h>

#include<conio.h>

int main(){

struct

 {

int roll;

char name[25];

float mark;
}std;
int i;
  FILE *fp;
fp=fopen("d:student.txt","wb");
clrscr();
printf("Enter student roll number name and marks for 10 students\n");
for(i=0;i<10;i++)
  {
scanf("%d%s%f",&std.roll,std.name,&std.mark);
fwrite(&std,sizeof(std),1,fp);
  }
fclose(fp);

getch();
  }


Write a program to reading a data file.
#include<stdio.h>
#include<conio.h>
 struct student
{
char name[30];
int roll,age;
}s;
void main()
{
FILE *ptr;
ptr=fopen("record,txt","r");
printf("\n Name \t Roll \t Age");
while(fread(&s,sizeof(s),1,ptr==1))
  printf("\n%s \t %d \t %d",s.name,s.roll,s.age);
fclose(ptr);
getch();
}

Write a program which writes welcome to Nepal in a file. [2070
#include <stdio.h>
#include <conio.h>
void main()
{
FILE *ptr;
clrscr();
Ptr=fopen("message.txt","w");
fclose(ptr);
getch();
}
 


Write a program using C language that reads successive records from the new data file and display each record on the screen in an appropriate format.

#include<stdio.h>

#include<conio.h>

int main()

{

struct

 {

int roll;

char name[25];
float mark;
}std;

int n,i;
  FILE *fp;
fp=fopen("d:student.txt","wb");
printf("\n Enter how many records: ");
scanf("%d",&n);
printf("Enter Student number, Name and Marks for %d students \n",n);
for(i=0;i<n;i++)
  {
scanf("%d%s%f",&std.roll,std.name,&std.mark);
fwrite(&std,sizeof(std),1,fp);
  }
fclose(fp);
fp=fopen("d:student.txt","r");
printf("\nRoll\tName\tMarks Obtained\n");
while(fread(&std,sizeof(std),1,fp))
printf("%d\t%s\t%f\n",std.roll,std.name,std.mark);
fclose(fp);
getch();
  }


Write a program to rename and delete a data file using rename and remove command.

#include<stdio.h>

#include<conio.h>

int main()

{

FILE *fp1,*fp2;

struct student

{
            intsn;
            char name[50];
            char address[100];
};
struct student s;
int x;
fp1=fopen("student.dat","r");
fp2=fopen("newstudent.dat","w");
            printf("\n The record of the student ");
            printf("\n S.N Name of Studnt address");
            scanf("\n%d%s %s ",s.sn,s.name,s.address);
           
            while(fread(&s,sizeof(s),1,fp1)==1)
            {
                        printf("\n%d%s %s   ",s.sn,s.name,s.address);
            }
            fclose(fp1);
            printf("\n Enter the serial no. of the record which you want to delete");
            scanf("%d",&x);
            while(fread(&s,sizeof(s),1,fp1)==1)
            {
                       
                        if(x==s.sn)
                                    {
                                                continue;
                                    }
            else
            {
                        fwrite(&s,sizeof(s),1,fp2);
            }
            }
            fclose(fp1);
            fclose(fp2);
            remove("student.dat");
            rename("newstudent.dat","student.dat");
            getch();
}



Write a C program designing a menu base system which has the following features:-
a.       Appending record
b.      Reading record
c.     Delete record
d.     Quit
#include<stdio.h>
#include<conio.h>
int main()
            {
FILE *fp1,*fp2;
struct student
 {
intsn;
char name[25];
char address[100];
  };
struct student s;
inti,x,choice;
char next='Y';
printf("\n MENU");
printf("\n 1. Append Record");
printf("\n 2. Read Record");
printf("\n 3. Delete Record");
printf("\n 4. Quit");
printf("\n Enter your choice between 1 to 4\t");
scanf("%d", &choice);
switch(choice)
{
            case 1:
            fp1=fopen("d:student.dat","a");
            while(next=='Y'||next=='y')
            {
                        printf("\n Enter the serial no.");
                        scanf("%d",&s.sn);
                        fflush(stdin);
                        printf("\n Enter the name of the student");
                        gets(s.name);
                        printf("\n Enter the address of student");
                        gets(s.address);
                        fwrite(&s,sizeof(s),1,fp1);
printf("\n Do you want to write next record to data file (Y/N)?");
next=getche();
                        }
fclose(fp1);
break; 
case 2:
fp1=fopen("d:student.dat","r");
printf("\n The record of the student");
printf("\n S.N Name of Student Address ");
while(fread(&s,sizeof(s),1,fp1)==1)
{
            printf("\n%d%s%s",s.sn,s.name,s.address);
}
fclose(fp1);
break;
case 3:
fp1=fopen("d:student.dat","r");
fp2=fopen("d:newstudent.dat","w");
printf("\n Enter the serail no. of the record which you want to delete");
scanf("%d",&x);
while(fread(&s,sizeof(s),1,fp1)==1)
{
            if(x==s.sn)
            {
                        continue;
            }
else
{
            fwrite(&s,sizeof(s),1,fp2);
}
}
fclose(fp1);
fclose(fp2);
remove("student.dat");
rename("newsstudent.dat","student.dat");
break;
case 4:
exit(0);
default:
printf("\n you entered invalid choice");
printf("\n please, enter your choice between 1 to 4");
}
getch();
}


Write a program which reads name, department and age from a file named “employee.dat" displays them on the monitor. [2069]
#include <stdio.h>
#include <conio.h>
struct student
{
char name[30],department[50];
int age;
}
s[l0];
void main()
{
FILE *ptr;
ptr=fopen("employee.dat","r");
printf("\n Name \t Department \t Marks");
while(fscanf(ptr,"\n%s\t%s\%d",s[i].name,s[i].department,&s[i]_age)!=EOF)
{
printf("\n%s\t%s\t%d",s[i].name,s[i].department,s[i].age);
}
fclose(ptr);
qetch();
}


Write a program which writes welcome to Nepal in a file. [2070]
#include <stdio.h>
#include <conio.h>
void main()
{
FILE *ptr;
clrscr();
ptr=fopen("message.txt","w");
fclose(ptr);
getch();
}



 



 
 

No comments:

Post a Comment