SlideShare a Scribd company logo
assadchadhar@gmail.com
1
1
By
Sir. Najam
BS Hon
M.A Edu
M.Ed Special Education
PGD (CS)
Cisco Certified Network Associate (CCNA)
Microsoft Certified system Engineer (MCSE)
MikroTik Certified Network Associate (MTCNA)
Email-ASSADCHADHAR@GMAIL.COM
#-03127522112 @ WHATSAPP
 Afile is an external collection of related data treated as a unit.
 Afile is a place on a disk where a group of related data is stored and
retrieved whenever necessary without destroying data
 The primary purpose of a file is to keep record of data. Record is a
group of related fields. Field is a group of characters which convey meaning.
 Files are stored in auxiliary or secondary storage devices. The two
common forms of secondary storage are disks (hard disk, CD and DVD) and
tapes.
Each file ends with an end of file (EOF) at a specified byte number,
recorded in file structure.
A file must first be opened properly before it can be accessed for
reading or writing. When a file is opened an object (buffer) is created and a
stream is associated with the object.
FILE’s
2
2
assadchadhar@gmail.com
It is advantageous to use files in the following circumstances.
i. When large volume of data are handled by the program and
ii. When the data need to be stored permanently without getting
destroyed when program is terminated.
FILE’s
3
2
assadchadhar@gmail.com
There are two kinds of files depending upon the format in which data is stored:
1) Text files 2) Binary files
Text files:
A text file stores textual information like alphabets, numbers, special symbols, etc.
actually the ASCII code of textual characters its stored in text files. Examples of
some text files include c, java, c++ source code files and files with .txt extensions .
The text file contains the characters in sequence. The computer process the text files
sequentially and in forward direction. One can perform file reading, writing and
appending operations. These operations are performed with the help of inbuilt
functions of c.
Binary files:
Text mode is inefficient for storing large amount of numerical data because it
occupies large space. Only solution to this is to open a file in binary mode, which
takes lesser space than the text mode. These files contain the set of bytes which
stores the information in binary form. One main drawback of binary files is data is
stored in human unreadable form. Examples of binary files are .exe files, video
stream files, image files etc. C language supports binary file operations with the help
FILE’s
of various inbuilt functions .. 4
4
assadchadhar@gmail.com
In order to perform the basic file operations C supports a number of functions
.Some of the important file handling functions available in the C library are as
follows :
@2020 Presented By Y. N. D.
Aravind 5
5
Operations on FILE’s
FUNCTION NAME OPERATION
fopen() Creates a new file for use Opens an existing file for use
fclose() Closes a file which has been opened for use
fcloseall() Closes all files which are opened
fgetc() Reads a character from a file
fputc() Writes a character to a file
fprintf() Writes a set of data values to files
fscanf() Reads a set of data values from files
getw() Reads an integer from file
putw() Writes an integer to a file
gets() Reads a string from a file
puts() Writes a string to a file
fseek() Sets the position to a desired point in afile
ftell() Gives the current position in the file
rewind() Sets the position to the beginning of the file Name
Basic operations on file:
1. Naming a file
2. Opening a file
3. Reading data from file
4. Writing data into file
5. Closing a File
Operations on FILE’s
6
7
assadchadhar@gmail.com
To store data in a file three things have to be specified for
operating system. They include
1. FILENAME :
It is a string of characters that make up a valid file name which
may contain two parts,a primary name and an optional period with
the extension. Prog1.c Myfirst.java Data.txt store
2. DATA STRUCTURE:
It is defined as FILE in the library of standard I/O function
definitions. Therefore all files are declared as type FILE.
FILE is a define data type. FILE *fptr;
3. PURPOSE:
It defines the reason for which a file is opened and the mode does
this job. fptr=fopen(“filename”,”mode”);
Modes of Opening FILE
7
7
assadchadhar@gmail.com
The different modes of opening files are :
//“r” (read) mode:
The read mode (r) opens an existing file for reading. When a file is opened in this
mode, the file marker or pointer is positioned at the beginning of the file (first
character).The file must already exist: if it does not exist a NULL is returned as an
error. If we try to write a file opened in read mode, an error occurs.
Syntax: fptr=fopen (“filename”,”r”);
// “w” (write) mode
The write mode (w) opens a file for writing. If the file doesn‟t exist, it is created. If it
already exists, it is opened and all its data is erased; the file pointer is positioned at
the beginning of the file It gives an error if we try to read from a file opened in write
mode.
Syntax: fptr=fopen (“filename”,”w”);
// “a” (append) mode
The append mode (a) opens an existing file for writing instead of creating a new file.
However, the writing starts after the last character in the existing file ,that is new data
is added, or appended, at the end of the file. If the file doesn‟t exist, new file is
created and opened. In this case, the writing will start at the beginning of the file.
Syntax: fptr=fopen (“filename”,”a”); 8
Modes of Opening FILE
8
assadchadhar@gmail.com
The different modes of opening files are :
// “r+” (read and write) mode
In this mode file is opened for both reading and writing the data. If a file does not
exist then NULL, is returned.
Syntax: fp=fopen (“filename”,”r+”);
// “w+” (read and write) mode
In this mode file is opened for both writing and reading the data. If a file already
exists its contents are erased and if a file does not exist then a new file is created.
Syntax: fp=fopen (“filename”,”w+”);
// “a+” (append and read) mode
In this mode file is opened for reading the data as well as data can be added at
the end.
Syntax:fp=fopen (“filename”, “a+”);
NOTE:To perform operations on binary files the following modes are
applicable with an extension b like rb,wb,ab,r+b,w+b,a+b,which has the
same menaing but allows to perform operations on binary files. 9
Modes of Opening FILE
9
assadchadhar@gmail.com
Naming and opening a file:
A name is given to the file used to store data. The file name is a string of
characters that make up a valid file name for operating system. It contains two
parts.Aprimary name and an optional period with the extension.
Examples: Student.dat, file1.txt, marks.doc, palin.c.
The general format of declaring and opening a file is
FILE *fp; //declaration
fp=fopen (“filename”,”mode”); //statement to open file.
Here FILE is a data structure defined for files. fp is a pointer to data type FILE.
filename is the name of the file. mode tells the purpose of opening this file.
Naming and opening a FILE
10
assadchadhar@gmail.com
Input functions used are ( Input operations on files)
getc(); : It is used to read characters from file that has been opened for read operation.
Syntax: c=getc (file pointer)
This statement reads a character from file pointed to by file pointer and assign to c.
It returns an end-of-file marker EOF, when end of file has been reached
fscanf(); : This function is similar to that of scanf function except that it works on files.
Syntax: fscanf (fp, ”control string”, list);
Example fscanf(f1,”%s%d”,str,&num);
The above statement reads string type data and integer type data from file.
getw(); : This function reads an integer from file.
Syntax: getw (file pointer);
fgets(): This function reads a string from a file pointed by a file pointer. It also copies the
string to a memory location referred by an array.
Syntax: fgets(string,no of bytes,filepointer);
fread(): This function is used for reading an entire structure block from a given file.
Syntax: fread(&struct_name,sizeof(struct_name),1,filepointer);
Reading a FILE
11
assadchadhar@gmail.com
To write into a file, following C functions are used
putc(); :This function writes a character to a file that has been opened in write mode.
Syntax: putc(c,fp);
This statement writes the character contained in character variable c into a file whose
pointer is fp.
fprintf(); :This function performs function, similar to that of printf.
Syntax: fprintf(f1,”%s,%d”,str,num);
putw(); :It writes an integer to a file.
Syntax: putw (variable, fp);
fputs(); : This function writes a string into a file pointed by a file pointer.
Synatax: fputs(string, filepointer);
fwrite(); : This function is used for writing an entire block structure to a given file.
Syntax: fwrite(&struct_name, sizeof(struct_name),1,filepointer);
Writing into a FILE
12
assadchadhar@gmail.com
A file is closed as soon as all operations on it have been completed.Closing a file
ensures that all outstanding information associated with the file is flushed out
from the buffers and all links to the file are broken. Another instance where we
have to close a file is to reopen the same file in a different mode. Library
function for closing a file is
fclose(file pointer);
Example: fclose(fp);
Where fp is the file pointer returned by the call to fopen(). fclose() returns 0 on
success (or) - 1 on error. Once a file is closed, its file pointer can be reused for
another file.
Note: fcloseall() can be used to close all the opened files at once.
Closing a FILE
13
assadchadhar@gmail.com
getc() and putc() functions:
getc()/fgetc() : It is used to read a character from a file that has been opened in a
read mode. It reads a character from the file whose file pointer is fp. The file
pointer moves by one character for every operation of getc(). The getc() will
return an end-of –marker EOF, when an end of file has been reached.
Syntax: getc(fp);
Ex: char ch; ch=getc(fp);
putc()/fputc() -:It is used to write a character contained in the character variable
to the file associated with the FILE pointer fp. fputc() also returns an end-of –
marker EOF, when an end of file has been reached.
Syntax: putc(c,fp);
Example: char c; putc(c,fp);
Character Oriented Functions
14
assadchadhar@gmail.com
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fptr;
char ch;
clrscr();
fptr=fopen(“text.data","w");
printf("n input data n");
ch=getchar();
while((ch!=EOF)
{
fputc(ch,fptr);
ch=getchar();
}
fclose(fptr);
getch();
}
Character Oriented Functions
15
assadchadhar@gmail.com
OUTPUT
input data
My name is computer ^z
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fptr;
char ch;
clrscr();
printf("n the content of the file text.data are n");
fptr=fopen("input1.txt","r");
while((ch=fgetc(fp))!=EOF)
putchar(ch);
fclose(fp);
}
Character Oriented Functions
16
assadchadhar@gmail.com
OUTPUT
the content of the file text.data are
My name is computer
fputs() and fgets():
fgets(): It is used to read a string from a file pointed by file pointer.
It copies the string to a memory location referred by an array.
Syntax:
Example:
fgets(buffer,size,filepointer);
fgets(text,50,fp1);
fputs(): It is used to write a string to an opened file pointed by file
pointer.
Syntax:
Example:
fputs(buffer,filepointer);
fputs(text,fp);
String Oriented Functions
17
assadchadhar@gmail.com
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fptr;
char str[10];
int i,n;
clrscr();
fptr=fopen(“student.data",“w");
printf("n Enter the no of students is :n");
scanf(“%d”,&n);
printf(“Enter %d students n”,n);
fflush(stdin);
for(i=0; i<n; i++)
{
gets(str);
fputs(str,fptr);
}
fclose(fptr);
getch();
}
String Oriented Functions
18
assadchadhar@gmail.com
OUTPUT
Enter the no of students is :
2
Enter 2 students
Revanth
Chinmay
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fptr;
char str[10];
int i,n;
clrscr();
fptr=fopen(“student.data",“r");
printf("n Strings are:n");
While(!feof(fptr))
{
fgets(str,10,fptr);
puts(str);
Printf(“n”);
}
fclose(fptr);
getch();
}
String Oriented Functions
19
assadchadhar@gmail.com
OUTPUT
Strings are :
Revanth
Chinmay
fprintf() and fscanf():
In order to handle a group of mixed data simultaneously there are two functions
that are fprintf() and fscanf().These two functions are identical to printf and scanf
functions,except that they work on files. The first argument of these functions is
a file pointer which specifies the file to be used.
fprintf(): The general form of fprintf() is
Syntax: fprintf(fptr,”control string”,aruguments-list);
where fp is a file pointer associated with a file that has been opened for writing .
The control string contains output specifications for the items in the list. .
Example: fprintf(fp,”%s%d”,name,age);
fscanf() : It is used to read a number of values from a file.
Syntax: fscanf(fptr,”control string”,arguments-list);
Example: fscanf(fp2,”%s %d”,item,&quantity);
like scanf , fscanf also returns the number of items that are successfully read.
when the end of file is reached it returns the value EOF.
Mixed data Oriented Functions
20
assadchadhar@gmail.com
#include<stdio.h>
#include<conio.h>
Struct student
{
int rollno;
char name[10];
float per;
}
void main()
{
FILE *fptr;
struct student x;
int i,n;
clrscr();
fptr=fopen(“student.data",“w");
printf("n Enter the number of students :n");
Scanf(“%d”,&n);
printf("n Enter %d student details :n");
for(1=1; i<=n:i++)
{
scanf(“%d %s %f”, &x.rolln0,x.name,&x.per);
fprintf(fptr,”%d %s %f”,x.rollno,x.name,x.per);
}
fclose(fptr);
getch();
}
Mixed Data Oriented Functions
21
assadchadhar@gmail.com
OUTPUT
Enter the no of students is :
2
Enter 2 students details
21
Revanth
98
22
Chinmay
95
#include<stdio.h>
#include<conio.h>
Struct student
{
int rollno;
char name[10];
float per;
}
void main()
{
FILE *fptr;
struct student x;
clrscr();
fptr=fopen(“student.data",“r");
}
printf("n Content of the file student.data :n");
While(!feof(fptr))
{
fscanf(fptr,“%d %s %f ”, &x.rolln0,x.name,&x.per);
printf(fptr,”%d3 %20s %9.2f”,x.rollno,x.name,x.per);
}
fclose(fptr);
getch();
assadchadhar@gmail.com 22
Mixed Data Oriented Functions
OUTPUT
Content of the file student.data :
21 Revanth 98.00
22 Chinmay 95.00
fread() and fwrite() :
fwrite(): It is used for writing an entire structure block to a given file in binary
mode.
Syntax: fwrite(&structure variable,sizeof(structure variable),1,filepointer);
Example: fwrite(&stud,sizeof(stud),1,fp);
fread(): It is used for reading an entire structure block from a given file in binary
mode.
Syntax: fread(&structure variable,sizeof(structure variable),1,filepointer);
Example: fread(&emp,sizeof(emp),1,fp1);
Reading and Writing Structures
23
assadchadhar@gmail.com
#include<stdio.h>
#include<conio.h>
Struct student
{
int rollno;
char name[10];
float per;
}
void main()
{
FILE *fptr;
struct student x;
int i,n;
clrscr();
fptr=fopen(“student.data",“wb");
printf("n Enter the number of students :n");
Scanf(“%d”,&n);
printf("n Enter %d student details :n");
for(1=1; i<=n:i++)
{
scanf(“%d %s %f”, &x.rolln0,x.name,&x.per);
fwrite(&x,sizeof(x),1,fptr);
}
fclose(fptr);
getch();
}
Writing Structures
24
assadchadhar@gmail.com
OUTPUT
Enter the no of students is :
2
Enter 2 students details :
21
Revanth
98
22
Chinmay
95
#include<stdio.h>
#include<conio.h>
Struct student
{
int rollno;
char name[10];
float per;
}
void main()
{
FILE *fptr;
struct student x;
clrscr();
fptr=fopen(“student.data",“rb");
printf("n Content of the file student.data :n");
While(fread(&x,sizeof(x),1,fptr))
{
printf(fptr,”%d3 %20s %9.2f”,x.rollno,x.name,x.per);
}
fclose(fptr);
getch();
}
Reading Structures
25
assadchadhar@gmail.com
OUTPUT
Content of the file student.data :
21 Revanth 98.00
22 Chinmay 95.00
At times we needed to access only a particular part of a file rather than accessing all the
data sequentially, which can be achieved with the help of functions fseek, ftell and rewind
available in IO library.
ftell():-
ftell takes a file pointer and returns a number of type long, that corresponds to the current
position. This function is useful in saving the current position of the file, which can later be
used in the program.
Syntax: n=ftell(fpTR);
n would give the Relative offset (In bytes) of the current position. This means that already n
bytes have a been read or written
rewind():-
It takes a file pointer and resets the position to the start of the file.
Syntax: rewind(fp);
n=ftell(fp);
would assign 0 to n because the file position has been set to start of the file by rewind().
The first byte in the file is numbered 0, second as 1, so on. This function helps in reading
the file more than once, without having to close and open the file.
Whenever a file is opened for reading or writing a rewind is done implicitly.
RANDOM ACCESS FILES
26
assadchadhar@gmail.com
fseek:-
fseek function is used to move the file pointer to a desired location within the file.
Syntax: fseek(file ptr,offset,position);
file pointer is a pointer to the file concerned, offset is a number or variable of type long and
position is an integer number which takes one of the following values. The offset specifies
the number of positions(Bytes) to be moved from the location specified by the position
which can be positive implies moving forward and negative implies moving backwards.
Example: fseek(fp,10,0) ;
fseek(fp,10,SEEK_SET);// file pointer is repositioned in the forward direction 10 bytes.
fseek(fp,-10,SEEK_END); // reads from backward direction from the end of file.
When the operation is successful fseek returns 0 and when we attempt to move a file
beyond boundaries fseek returns -1.
RANDOM ACCESS FILES
27
assadchadhar@gmail.com
POSITION VALUE VALUE CONSTANT MEANING
0 SEEK_SET BEGINNING OF FILE
1 SEEK_CUR CURRENT POSITION
2 SEEK_END END OF FILE
RANDOM ACCESS FILES
28
assadchadhar@gmail.com
Some of the Operations of fseek function are as follows :
STATEMENT MEANING
fseek(fp,0L,0); Go to beginning similar to rewind()
fseek(fp,0L,1); Stay at current position
fseek(fp,0L,2); Go to the end of file, past the last character of the file.
fseek(fp,m,0); Move to (m+1)th byte in the file
fseek(fp,m,1); Go forward by m bytes
fseek(fp,-m,1); Go backwards by m bytes from the current position
fseek(fp,-m,2); Go backwards by m bytes from the end.(positions the file
to the mth character from the end)
ERROR HANDLING IN FILES
29
assadchadhar@gmail.com
It is possible that an error may occur during I/O operations on a file. Typical error situations include:
1. Trying to read beyond the end of file mark.
2. Device overflow
3. Trying to use a file that has not been opened
4. Trying to perform an operation on a file, when the file is opened for another type of operations
5. Opening a file with an invalid filename
6. Attempting to write a write protected file
If we fail to check such read and write errors, a program may behave abnormally when an error occurs.An unchecked error may result
in a premature termination of the program or incorrect output. In C we have two status - inquiry library functions feof and ferror that
can help us detect I/O errors in the files.
A)feof(): The feof() function can be used to test for an end of file condition. It takes a FILE pointer as its only argument and returns a
non zero integer value if all of the data from the specified file has been read, and returns zero otherwise. If fp is a pointer to file that
has just opened for reading, then the statement
if(feof(fp))
printf(“End of data”);
would display the message “End of data” on reaching the end of file condition.
b)ferror(): The ferror() function reports the status of the file indicated. It also takes a file pointer as its argument and returns a nonzero
integer if an error has been detected up to that point, during processing. It returns zero otherwise. The statement
if(ferror(fp)!=0)
printf(“an error has occurredn”);
would print an error message if the reading is not successful
c)fp==null: We know that whenever a file is opened using fopen function, a file pointer is returned. If the file cannot be opened for
some reason, then the function returns a null pointer.
This facility can be used to test whether a file has been opened or not.
Example if(fp==NULL)
printf(“File could not be opened.n”);
d) perror(): It is a standard library function which prints the error messages specified by the compiler. For example:
if(ferror(fp)) perror(filename);
Thank You
30
assadchadhar@gmail.com
By
Sir. Najam
BS Hon
M.A Edu
M.Ed Special Education
PGD (CS)
Cisco Certified Network Associate (CCNA)
Microsoft Certified system Engineer (MCSE)
MikroTik Certified Network Associate (MTCNA)
Email-ASSADCHADHAR@GMAIL.COM
#-03127522112 @ WHATSAPP
Visiting Professor of ASPIRE Group of Colleges
Visiting Professor of Muslim Institute Hafizabad

More Related Content

PDF
FILES IN C
PPTX
Unit-VI.pptx
PDF
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
PPTX
File handling in C hhsjsjshsjjsjsjs.pptx
PDF
File handling C program
PPTX
File Handling
PPTX
File Handling
DOCX
FILES IN C
Unit-VI.pptx
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
File handling in C hhsjsjshsjjsjsjs.pptx
File handling C program
File Handling
File Handling

Similar to INput output stream in ccP Full Detail.pptx (20)

PDF
VIT351 Software Development VI Unit5
PPT
Lecture 20 - File Handling
PPTX
File management
PPTX
File management
PPTX
PPS PPT 2.pptx
PPT
7 Data File Handling
PPTX
PPS-II UNIT-5 PPT.pptx
PDF
Module 5 file cp
PDF
file_c.pdf
PPT
file_handling_in_c.ppt
PPT
file_handling_in_c.ppt
PDF
23CS101T PSPP python program - UNIT 5.pdf
PPTX
C Programming Unit-5
PPT
Unit5
PPTX
File Organization
PPTX
Programming C- File Handling , File Operation
PDF
Data file handling
PDF
File Management
PPTX
File Handling
PPTX
File management in C++
VIT351 Software Development VI Unit5
Lecture 20 - File Handling
File management
File management
PPS PPT 2.pptx
7 Data File Handling
PPS-II UNIT-5 PPT.pptx
Module 5 file cp
file_c.pdf
file_handling_in_c.ppt
file_handling_in_c.ppt
23CS101T PSPP python program - UNIT 5.pdf
C Programming Unit-5
Unit5
File Organization
Programming C- File Handling , File Operation
Data file handling
File Management
File Handling
File management in C++

More from AssadLeo1 (20)

PPT
Chagal chagal with khatch khatch model with detail
PPT
E commerce busin and some important issues
PPTX
What is SEO in pakistan with main components
PPT
business model and some other things that
PPTX
Software Evolution all in Mehmoona.pptx
PPTX
Behavioral Model with Maniha Butt and many More
PPTX
Software Quality Assurance Qurat ul ain.pptx
PPTX
UML Samra Bs it 4th all about aspire college
PPTX
Process Structure and some other important
PPT
Process importance with full detail about
PPTX
IPM Chapter 1 Complete detail and chapeter
PPTX
Hardware Firewall with all the detail of
PPTX
Law and Order in PK in a country is most important
PPTX
Types of Multipule things and other things
PPTX
Model_of_Heterogeneous_System and other things
PPTX
what a knowledge and other things in this slide
PPTX
full with knowledge and other things with
PPT
that is the most important part of this topic
PPT
Discrete and other examples with great intrest
PPTX
Decoding Insights and some extra examples
Chagal chagal with khatch khatch model with detail
E commerce busin and some important issues
What is SEO in pakistan with main components
business model and some other things that
Software Evolution all in Mehmoona.pptx
Behavioral Model with Maniha Butt and many More
Software Quality Assurance Qurat ul ain.pptx
UML Samra Bs it 4th all about aspire college
Process Structure and some other important
Process importance with full detail about
IPM Chapter 1 Complete detail and chapeter
Hardware Firewall with all the detail of
Law and Order in PK in a country is most important
Types of Multipule things and other things
Model_of_Heterogeneous_System and other things
what a knowledge and other things in this slide
full with knowledge and other things with
that is the most important part of this topic
Discrete and other examples with great intrest
Decoding Insights and some extra examples

Recently uploaded (20)

PDF
Business Ethics Teaching Materials for college
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Pre independence Education in Inndia.pdf
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
01-Introduction-to-Information-Management.pdf
PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Basic Mud Logging Guide for educational purpose
PDF
Supply Chain Operations Speaking Notes -ICLT Program
Business Ethics Teaching Materials for college
O7-L3 Supply Chain Operations - ICLT Program
TR - Agricultural Crops Production NC III.pdf
Abdominal Access Techniques with Prof. Dr. R K Mishra
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Week 4 Term 3 Study Techniques revisited.pptx
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Pre independence Education in Inndia.pdf
Pharmacology of Heart Failure /Pharmacotherapy of CHF
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
2.FourierTransform-ShortQuestionswithAnswers.pdf
01-Introduction-to-Information-Management.pdf
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Basic Mud Logging Guide for educational purpose
Supply Chain Operations Speaking Notes -ICLT Program

INput output stream in ccP Full Detail.pptx

  • 1. assadchadhar@gmail.com 1 1 By Sir. Najam BS Hon M.A Edu M.Ed Special Education PGD (CS) Cisco Certified Network Associate (CCNA) Microsoft Certified system Engineer (MCSE) MikroTik Certified Network Associate (MTCNA) Email-ASSADCHADHAR@GMAIL.COM #-03127522112 @ WHATSAPP
  • 2.  Afile is an external collection of related data treated as a unit.  Afile is a place on a disk where a group of related data is stored and retrieved whenever necessary without destroying data  The primary purpose of a file is to keep record of data. Record is a group of related fields. Field is a group of characters which convey meaning.  Files are stored in auxiliary or secondary storage devices. The two common forms of secondary storage are disks (hard disk, CD and DVD) and tapes. Each file ends with an end of file (EOF) at a specified byte number, recorded in file structure. A file must first be opened properly before it can be accessed for reading or writing. When a file is opened an object (buffer) is created and a stream is associated with the object. FILE’s 2 2 assadchadhar@gmail.com
  • 3. It is advantageous to use files in the following circumstances. i. When large volume of data are handled by the program and ii. When the data need to be stored permanently without getting destroyed when program is terminated. FILE’s 3 2 assadchadhar@gmail.com
  • 4. There are two kinds of files depending upon the format in which data is stored: 1) Text files 2) Binary files Text files: A text file stores textual information like alphabets, numbers, special symbols, etc. actually the ASCII code of textual characters its stored in text files. Examples of some text files include c, java, c++ source code files and files with .txt extensions . The text file contains the characters in sequence. The computer process the text files sequentially and in forward direction. One can perform file reading, writing and appending operations. These operations are performed with the help of inbuilt functions of c. Binary files: Text mode is inefficient for storing large amount of numerical data because it occupies large space. Only solution to this is to open a file in binary mode, which takes lesser space than the text mode. These files contain the set of bytes which stores the information in binary form. One main drawback of binary files is data is stored in human unreadable form. Examples of binary files are .exe files, video stream files, image files etc. C language supports binary file operations with the help FILE’s of various inbuilt functions .. 4 4 assadchadhar@gmail.com
  • 5. In order to perform the basic file operations C supports a number of functions .Some of the important file handling functions available in the C library are as follows : @2020 Presented By Y. N. D. Aravind 5 5 Operations on FILE’s FUNCTION NAME OPERATION fopen() Creates a new file for use Opens an existing file for use fclose() Closes a file which has been opened for use fcloseall() Closes all files which are opened fgetc() Reads a character from a file fputc() Writes a character to a file fprintf() Writes a set of data values to files fscanf() Reads a set of data values from files getw() Reads an integer from file putw() Writes an integer to a file gets() Reads a string from a file puts() Writes a string to a file fseek() Sets the position to a desired point in afile ftell() Gives the current position in the file rewind() Sets the position to the beginning of the file Name
  • 6. Basic operations on file: 1. Naming a file 2. Opening a file 3. Reading data from file 4. Writing data into file 5. Closing a File Operations on FILE’s 6 7 assadchadhar@gmail.com
  • 7. To store data in a file three things have to be specified for operating system. They include 1. FILENAME : It is a string of characters that make up a valid file name which may contain two parts,a primary name and an optional period with the extension. Prog1.c Myfirst.java Data.txt store 2. DATA STRUCTURE: It is defined as FILE in the library of standard I/O function definitions. Therefore all files are declared as type FILE. FILE is a define data type. FILE *fptr; 3. PURPOSE: It defines the reason for which a file is opened and the mode does this job. fptr=fopen(“filename”,”mode”); Modes of Opening FILE 7 7 assadchadhar@gmail.com
  • 8. The different modes of opening files are : //“r” (read) mode: The read mode (r) opens an existing file for reading. When a file is opened in this mode, the file marker or pointer is positioned at the beginning of the file (first character).The file must already exist: if it does not exist a NULL is returned as an error. If we try to write a file opened in read mode, an error occurs. Syntax: fptr=fopen (“filename”,”r”); // “w” (write) mode The write mode (w) opens a file for writing. If the file doesn‟t exist, it is created. If it already exists, it is opened and all its data is erased; the file pointer is positioned at the beginning of the file It gives an error if we try to read from a file opened in write mode. Syntax: fptr=fopen (“filename”,”w”); // “a” (append) mode The append mode (a) opens an existing file for writing instead of creating a new file. However, the writing starts after the last character in the existing file ,that is new data is added, or appended, at the end of the file. If the file doesn‟t exist, new file is created and opened. In this case, the writing will start at the beginning of the file. Syntax: fptr=fopen (“filename”,”a”); 8 Modes of Opening FILE 8 assadchadhar@gmail.com
  • 9. The different modes of opening files are : // “r+” (read and write) mode In this mode file is opened for both reading and writing the data. If a file does not exist then NULL, is returned. Syntax: fp=fopen (“filename”,”r+”); // “w+” (read and write) mode In this mode file is opened for both writing and reading the data. If a file already exists its contents are erased and if a file does not exist then a new file is created. Syntax: fp=fopen (“filename”,”w+”); // “a+” (append and read) mode In this mode file is opened for reading the data as well as data can be added at the end. Syntax:fp=fopen (“filename”, “a+”); NOTE:To perform operations on binary files the following modes are applicable with an extension b like rb,wb,ab,r+b,w+b,a+b,which has the same menaing but allows to perform operations on binary files. 9 Modes of Opening FILE 9 assadchadhar@gmail.com
  • 10. Naming and opening a file: A name is given to the file used to store data. The file name is a string of characters that make up a valid file name for operating system. It contains two parts.Aprimary name and an optional period with the extension. Examples: Student.dat, file1.txt, marks.doc, palin.c. The general format of declaring and opening a file is FILE *fp; //declaration fp=fopen (“filename”,”mode”); //statement to open file. Here FILE is a data structure defined for files. fp is a pointer to data type FILE. filename is the name of the file. mode tells the purpose of opening this file. Naming and opening a FILE 10 assadchadhar@gmail.com
  • 11. Input functions used are ( Input operations on files) getc(); : It is used to read characters from file that has been opened for read operation. Syntax: c=getc (file pointer) This statement reads a character from file pointed to by file pointer and assign to c. It returns an end-of-file marker EOF, when end of file has been reached fscanf(); : This function is similar to that of scanf function except that it works on files. Syntax: fscanf (fp, ”control string”, list); Example fscanf(f1,”%s%d”,str,&num); The above statement reads string type data and integer type data from file. getw(); : This function reads an integer from file. Syntax: getw (file pointer); fgets(): This function reads a string from a file pointed by a file pointer. It also copies the string to a memory location referred by an array. Syntax: fgets(string,no of bytes,filepointer); fread(): This function is used for reading an entire structure block from a given file. Syntax: fread(&struct_name,sizeof(struct_name),1,filepointer); Reading a FILE 11 assadchadhar@gmail.com
  • 12. To write into a file, following C functions are used putc(); :This function writes a character to a file that has been opened in write mode. Syntax: putc(c,fp); This statement writes the character contained in character variable c into a file whose pointer is fp. fprintf(); :This function performs function, similar to that of printf. Syntax: fprintf(f1,”%s,%d”,str,num); putw(); :It writes an integer to a file. Syntax: putw (variable, fp); fputs(); : This function writes a string into a file pointed by a file pointer. Synatax: fputs(string, filepointer); fwrite(); : This function is used for writing an entire block structure to a given file. Syntax: fwrite(&struct_name, sizeof(struct_name),1,filepointer); Writing into a FILE 12 assadchadhar@gmail.com
  • 13. A file is closed as soon as all operations on it have been completed.Closing a file ensures that all outstanding information associated with the file is flushed out from the buffers and all links to the file are broken. Another instance where we have to close a file is to reopen the same file in a different mode. Library function for closing a file is fclose(file pointer); Example: fclose(fp); Where fp is the file pointer returned by the call to fopen(). fclose() returns 0 on success (or) - 1 on error. Once a file is closed, its file pointer can be reused for another file. Note: fcloseall() can be used to close all the opened files at once. Closing a FILE 13 assadchadhar@gmail.com
  • 14. getc() and putc() functions: getc()/fgetc() : It is used to read a character from a file that has been opened in a read mode. It reads a character from the file whose file pointer is fp. The file pointer moves by one character for every operation of getc(). The getc() will return an end-of –marker EOF, when an end of file has been reached. Syntax: getc(fp); Ex: char ch; ch=getc(fp); putc()/fputc() -:It is used to write a character contained in the character variable to the file associated with the FILE pointer fp. fputc() also returns an end-of – marker EOF, when an end of file has been reached. Syntax: putc(c,fp); Example: char c; putc(c,fp); Character Oriented Functions 14 assadchadhar@gmail.com
  • 15. #include<stdio.h> #include<conio.h> void main() { FILE *fptr; char ch; clrscr(); fptr=fopen(“text.data","w"); printf("n input data n"); ch=getchar(); while((ch!=EOF) { fputc(ch,fptr); ch=getchar(); } fclose(fptr); getch(); } Character Oriented Functions 15 assadchadhar@gmail.com OUTPUT input data My name is computer ^z
  • 16. #include<stdio.h> #include<conio.h> void main() { FILE *fptr; char ch; clrscr(); printf("n the content of the file text.data are n"); fptr=fopen("input1.txt","r"); while((ch=fgetc(fp))!=EOF) putchar(ch); fclose(fp); } Character Oriented Functions 16 assadchadhar@gmail.com OUTPUT the content of the file text.data are My name is computer
  • 17. fputs() and fgets(): fgets(): It is used to read a string from a file pointed by file pointer. It copies the string to a memory location referred by an array. Syntax: Example: fgets(buffer,size,filepointer); fgets(text,50,fp1); fputs(): It is used to write a string to an opened file pointed by file pointer. Syntax: Example: fputs(buffer,filepointer); fputs(text,fp); String Oriented Functions 17 assadchadhar@gmail.com
  • 18. #include<stdio.h> #include<conio.h> void main() { FILE *fptr; char str[10]; int i,n; clrscr(); fptr=fopen(“student.data",“w"); printf("n Enter the no of students is :n"); scanf(“%d”,&n); printf(“Enter %d students n”,n); fflush(stdin); for(i=0; i<n; i++) { gets(str); fputs(str,fptr); } fclose(fptr); getch(); } String Oriented Functions 18 assadchadhar@gmail.com OUTPUT Enter the no of students is : 2 Enter 2 students Revanth Chinmay
  • 19. #include<stdio.h> #include<conio.h> void main() { FILE *fptr; char str[10]; int i,n; clrscr(); fptr=fopen(“student.data",“r"); printf("n Strings are:n"); While(!feof(fptr)) { fgets(str,10,fptr); puts(str); Printf(“n”); } fclose(fptr); getch(); } String Oriented Functions 19 assadchadhar@gmail.com OUTPUT Strings are : Revanth Chinmay
  • 20. fprintf() and fscanf(): In order to handle a group of mixed data simultaneously there are two functions that are fprintf() and fscanf().These two functions are identical to printf and scanf functions,except that they work on files. The first argument of these functions is a file pointer which specifies the file to be used. fprintf(): The general form of fprintf() is Syntax: fprintf(fptr,”control string”,aruguments-list); where fp is a file pointer associated with a file that has been opened for writing . The control string contains output specifications for the items in the list. . Example: fprintf(fp,”%s%d”,name,age); fscanf() : It is used to read a number of values from a file. Syntax: fscanf(fptr,”control string”,arguments-list); Example: fscanf(fp2,”%s %d”,item,&quantity); like scanf , fscanf also returns the number of items that are successfully read. when the end of file is reached it returns the value EOF. Mixed data Oriented Functions 20 assadchadhar@gmail.com
  • 21. #include<stdio.h> #include<conio.h> Struct student { int rollno; char name[10]; float per; } void main() { FILE *fptr; struct student x; int i,n; clrscr(); fptr=fopen(“student.data",“w"); printf("n Enter the number of students :n"); Scanf(“%d”,&n); printf("n Enter %d student details :n"); for(1=1; i<=n:i++) { scanf(“%d %s %f”, &x.rolln0,x.name,&x.per); fprintf(fptr,”%d %s %f”,x.rollno,x.name,x.per); } fclose(fptr); getch(); } Mixed Data Oriented Functions 21 assadchadhar@gmail.com OUTPUT Enter the no of students is : 2 Enter 2 students details 21 Revanth 98 22 Chinmay 95
  • 22. #include<stdio.h> #include<conio.h> Struct student { int rollno; char name[10]; float per; } void main() { FILE *fptr; struct student x; clrscr(); fptr=fopen(“student.data",“r"); } printf("n Content of the file student.data :n"); While(!feof(fptr)) { fscanf(fptr,“%d %s %f ”, &x.rolln0,x.name,&x.per); printf(fptr,”%d3 %20s %9.2f”,x.rollno,x.name,x.per); } fclose(fptr); getch(); assadchadhar@gmail.com 22 Mixed Data Oriented Functions OUTPUT Content of the file student.data : 21 Revanth 98.00 22 Chinmay 95.00
  • 23. fread() and fwrite() : fwrite(): It is used for writing an entire structure block to a given file in binary mode. Syntax: fwrite(&structure variable,sizeof(structure variable),1,filepointer); Example: fwrite(&stud,sizeof(stud),1,fp); fread(): It is used for reading an entire structure block from a given file in binary mode. Syntax: fread(&structure variable,sizeof(structure variable),1,filepointer); Example: fread(&emp,sizeof(emp),1,fp1); Reading and Writing Structures 23 assadchadhar@gmail.com
  • 24. #include<stdio.h> #include<conio.h> Struct student { int rollno; char name[10]; float per; } void main() { FILE *fptr; struct student x; int i,n; clrscr(); fptr=fopen(“student.data",“wb"); printf("n Enter the number of students :n"); Scanf(“%d”,&n); printf("n Enter %d student details :n"); for(1=1; i<=n:i++) { scanf(“%d %s %f”, &x.rolln0,x.name,&x.per); fwrite(&x,sizeof(x),1,fptr); } fclose(fptr); getch(); } Writing Structures 24 assadchadhar@gmail.com OUTPUT Enter the no of students is : 2 Enter 2 students details : 21 Revanth 98 22 Chinmay 95
  • 25. #include<stdio.h> #include<conio.h> Struct student { int rollno; char name[10]; float per; } void main() { FILE *fptr; struct student x; clrscr(); fptr=fopen(“student.data",“rb"); printf("n Content of the file student.data :n"); While(fread(&x,sizeof(x),1,fptr)) { printf(fptr,”%d3 %20s %9.2f”,x.rollno,x.name,x.per); } fclose(fptr); getch(); } Reading Structures 25 assadchadhar@gmail.com OUTPUT Content of the file student.data : 21 Revanth 98.00 22 Chinmay 95.00
  • 26. At times we needed to access only a particular part of a file rather than accessing all the data sequentially, which can be achieved with the help of functions fseek, ftell and rewind available in IO library. ftell():- ftell takes a file pointer and returns a number of type long, that corresponds to the current position. This function is useful in saving the current position of the file, which can later be used in the program. Syntax: n=ftell(fpTR); n would give the Relative offset (In bytes) of the current position. This means that already n bytes have a been read or written rewind():- It takes a file pointer and resets the position to the start of the file. Syntax: rewind(fp); n=ftell(fp); would assign 0 to n because the file position has been set to start of the file by rewind(). The first byte in the file is numbered 0, second as 1, so on. This function helps in reading the file more than once, without having to close and open the file. Whenever a file is opened for reading or writing a rewind is done implicitly. RANDOM ACCESS FILES 26 assadchadhar@gmail.com
  • 27. fseek:- fseek function is used to move the file pointer to a desired location within the file. Syntax: fseek(file ptr,offset,position); file pointer is a pointer to the file concerned, offset is a number or variable of type long and position is an integer number which takes one of the following values. The offset specifies the number of positions(Bytes) to be moved from the location specified by the position which can be positive implies moving forward and negative implies moving backwards. Example: fseek(fp,10,0) ; fseek(fp,10,SEEK_SET);// file pointer is repositioned in the forward direction 10 bytes. fseek(fp,-10,SEEK_END); // reads from backward direction from the end of file. When the operation is successful fseek returns 0 and when we attempt to move a file beyond boundaries fseek returns -1. RANDOM ACCESS FILES 27 assadchadhar@gmail.com POSITION VALUE VALUE CONSTANT MEANING 0 SEEK_SET BEGINNING OF FILE 1 SEEK_CUR CURRENT POSITION 2 SEEK_END END OF FILE
  • 28. RANDOM ACCESS FILES 28 assadchadhar@gmail.com Some of the Operations of fseek function are as follows : STATEMENT MEANING fseek(fp,0L,0); Go to beginning similar to rewind() fseek(fp,0L,1); Stay at current position fseek(fp,0L,2); Go to the end of file, past the last character of the file. fseek(fp,m,0); Move to (m+1)th byte in the file fseek(fp,m,1); Go forward by m bytes fseek(fp,-m,1); Go backwards by m bytes from the current position fseek(fp,-m,2); Go backwards by m bytes from the end.(positions the file to the mth character from the end)
  • 29. ERROR HANDLING IN FILES 29 assadchadhar@gmail.com It is possible that an error may occur during I/O operations on a file. Typical error situations include: 1. Trying to read beyond the end of file mark. 2. Device overflow 3. Trying to use a file that has not been opened 4. Trying to perform an operation on a file, when the file is opened for another type of operations 5. Opening a file with an invalid filename 6. Attempting to write a write protected file If we fail to check such read and write errors, a program may behave abnormally when an error occurs.An unchecked error may result in a premature termination of the program or incorrect output. In C we have two status - inquiry library functions feof and ferror that can help us detect I/O errors in the files. A)feof(): The feof() function can be used to test for an end of file condition. It takes a FILE pointer as its only argument and returns a non zero integer value if all of the data from the specified file has been read, and returns zero otherwise. If fp is a pointer to file that has just opened for reading, then the statement if(feof(fp)) printf(“End of data”); would display the message “End of data” on reaching the end of file condition. b)ferror(): The ferror() function reports the status of the file indicated. It also takes a file pointer as its argument and returns a nonzero integer if an error has been detected up to that point, during processing. It returns zero otherwise. The statement if(ferror(fp)!=0) printf(“an error has occurredn”); would print an error message if the reading is not successful c)fp==null: We know that whenever a file is opened using fopen function, a file pointer is returned. If the file cannot be opened for some reason, then the function returns a null pointer. This facility can be used to test whether a file has been opened or not. Example if(fp==NULL) printf(“File could not be opened.n”); d) perror(): It is a standard library function which prints the error messages specified by the compiler. For example: if(ferror(fp)) perror(filename);
  • 30. Thank You 30 assadchadhar@gmail.com By Sir. Najam BS Hon M.A Edu M.Ed Special Education PGD (CS) Cisco Certified Network Associate (CCNA) Microsoft Certified system Engineer (MCSE) MikroTik Certified Network Associate (MTCNA) Email-ASSADCHADHAR@GMAIL.COM #-03127522112 @ WHATSAPP Visiting Professor of ASPIRE Group of Colleges Visiting Professor of Muslim Institute Hafizabad