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

File - Handling C Programming

The document discusses file handling in C programming. It defines key concepts like file pointers, opening and closing files, reading and writing to files, and provides code examples to create, read and write to data files.

Uploaded by

shresthaaastha93
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

File - Handling C Programming

The document discusses file handling in C programming. It defines key concepts like file pointers, opening and closing files, reading and writing to files, and provides code examples to create, read and write to data files.

Uploaded by

shresthaaastha93
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Difference between Structure and union It is used to read formatted character, string or numeric data from a data file.

Structure Union Syntax:


1. Structure reserves separate 1. Union reserves only one memory fscanf(filepointer, “data format specifier”, &variable);
memory space for each member space for all the member data type. Example:
data type. fscanf(fp,”%s %d”,&name,&age);
2. In union, amount of memory space
2. The amount of memory space required is equal to that required by [6]. fclose( ) : It is used for closing a data file after reading & writing operations.
required to store structure variable is largest member data type. Syntax:
the sum of size of all the member fclose(file pointer);
data type. 3. Only one member data type can be 1) Write a c program to create data file and store name and age.
3. All the members of structure can accessed at a time because only one
be accessed at a time. union variable is active at a time. #include<stdio.h>
4. struct keyword is used to declare 4. Union keyword is used to declare #include<conio.h>
structure. union. void main()
{
File: file is the collection of related information stored on disk with unique name. clrscr();
FILE *fp;
Elements of file handling: char name[10];
int age;
[1]. Declaration of file pointer:
fp=fopen("student.txt","w");
printf("Enter name and age :");
Syntax: FILE *pointer; e.g. FILE *fp; scanf("%s %d",&name,&age);
fprintf(fp,"%s\t %d",name,age);
[2]. Opening a File:
fclose(fp);
Syntax: getch();
Pointer name = fopen(“filename”, “mode”); }
2) Write a c program to read data (content) from a data file.
[3]. Modes of opening a file:
#include<stdio.h>
r : Reading contents (data items) from a data file.
#include<conio.h>
w : Writing contents (data items) to a data file.
void main()
a : Adding contents(data items) to a exiting data file.
{
r+ : Reading and writing from and to a data file.
clrscr();
w+ : writing and reading to and from a data file.
FILE *fp;
A+ : reading and adding new contents from and
char name[10];
to a data file. int age;
[4].fprintf( ) : fp=fopen("student.txt","r");
It is used to write formatted character, string or numeric data into a data file. fscanf(fp,"%s %d",&name,&age);
Syntax: printf("%s \t %d",name,age);
fprinf(filepointer, “format specifier”, &variables); fclose(fp);
Example: getch();
fprintf(fp,”%s %d”,name,age); }

[5].fscanf( ) :
3) Write a c program to create a data file “employee.txt” to name , post and salary 5) Write a c program to read all the data (content) from a data file “Employee.txt” .
of any 5 employees #include<stdio.h>
#include<conio.h>
#include<stdio.h>
void main()
#include<conio.h>
{
void main()
clrscr();
{
FILE *fp;
clrscr();
char name[20],post[20];
FILE *fp;
int salary;
char name[20],post[20];
fp=fopen("employee.txt","r");
int salary,j;
while(fscanf(fp,"%s\t %s\t %d",&name,&post,&salary)!=EOF)
fp=fopen("employee.txt","w");
{
for( j=0;j<5;j=j+1)
{
printf("\n %s\t %s \t%d",name,post,salary);
printf("Enter name,post and salary :");
scanf("%s %s %d",&name,&post,&salary);
}
fprintf(fp,"%s \t %s \t %d \n",name,post,salary);
fclose(fp);
}
getch();
fclose(fp);
}
getch(); 4) Write a c program to create a data file “people.txt” to store name,sex and age of people.
} The program allows to enter data as long as the user wants.
4) Write a c program to read data (content) from a data file “Employee.txt” which #include<stdio.h>
contains name,post,salary of 5 employees. #include<conio.h>
void main()
#include<stdio.h> {
#include<conio.h> clrscr();
void main() FILE *fp;
{ char name[20],sex[20],re;
re = ‘y’;
clrscr();
int age;
FILE *fp; fp=fopen("people.txt","w");
char name[20],post[20]; while( re=’y’)
int salary,j; {
fp=fopen("employee.txt","r"); printf("Enter name,sex and age :");
for(j=1;j<=5;j=j+1) scanf("%s %s %d",&name,&sex,&age);
{ fprintf(fp,"\n %s \t %s \t %d",name,sex,age);
fscanf(fp,"%s\t%s\t %d",&name,&post,&salary) printf(“do you want store more data”);
printf("\n %s\t %s \t%d",name,post,salary); re = getch();
}
}
fclose(fp);
getch(); getch(); }
}

You might also like