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

Lecture 12

This document provides an introduction to file input/output (I/O) in C programming, explaining the necessity of files for data persistence beyond program termination. It covers high-level file I/O functions, file operations such as creating, opening, reading, writing, and closing files, and introduces key functions like fopen(), fclose(), fprintf(), and fscanf(). Additionally, it illustrates how to write to and read from files with example code snippets.

Uploaded by

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

Lecture 12

This document provides an introduction to file input/output (I/O) in C programming, explaining the necessity of files for data persistence beyond program termination. It covers high-level file I/O functions, file operations such as creating, opening, reading, writing, and closing files, and introduces key functions like fopen(), fclose(), fprintf(), and fscanf(). Additionally, it illustrates how to write to and read from files with example code snippets.

Uploaded by

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

Introduction to File I/O

Why files are needed?


When the program is terminated, the
entire data is lost in C programming. If
you want to keep large volume of data, it
is time consuming to enter the entire data.
But, if file is created, these information
can be accessed using few commands.
•There are large numbers of functions to
handle file I/O in C language.
• Now, we will learn to handle standard
I/O(High level file I/O functions) in C.

High level file I/O functions can be

categorized as:

•Text file

•Binary file
File Operations

•Creating a new file

•Opening an existing file

•Reading from a file

•Writing information to a file

•Closing a file
FILE *ptr;

Working with file

While working with file, we


need to declare a pointer of type
file. This declaration is needed
for communication between file
and program.

FILE *ptr;
Opening a file
Opening a file is performed using library
function fopen(). The syntax for opening a
file in standard I/O is:
ptr=fopen("fileopen","mode")
For Example:
fopen("E:\\cprogram\
program.txt","w");
/*----------------------------------------------- */
E:\\cprogram\program.txt is the location to
create file. "w" represents the mode for
writing.
/*----------------------------------------------- */
Here, the program.txt file is opened for writing
mode.

Opening Modes in Standard I/O


File Mode Meaning of Mode During Inexistence of file
If the file does not exist,
r Open for reading.
fopen() returns NULL.

If the file exists, its contents


w Open for writing. are overwritten. If the file does
not exist, it will be created.

Open for append. i.e, Data is If the file does not exists, it
a
added to end of file. will be created.
Open for both reading and If the file does not exist,
r+
writing. fopen() returns NULL.

If the file exists, its contents


Open for both reading and
w+ are overwritten. If the file does
writing.
not exist, it will be created.

Open for both reading and If the file does not exists, it
a+
appending. will be created.
Closing a File

The file should be closed after reading/writing


of a file. Closing a file is performed using
library function fclose().
fclose(ptr);
/ptr is the file pointer associated with file to be
closed.
The Functions fprintf() and fscanf()
functions.

The functions fprintf() and fscanf()


are the file version of printf() and
scanf().

The only difference while using


fprintf() and fscanf() is that, the
first argument is a pointer to the
structure FILE
Writing to a file

#include <stdio.h>
int main()
{ int n; FILE *fptr; fptr=fopen("C:\\
program.txt","w"); if(fptr==NULL)
{ printf("Error!");
exit(1); }
printf("Enter n: ");
scanf("%d",&n);
fprintf(fptr,"%d",n);
fclose(fptr);
return 0;}
This program takes the number from user and
stores in file. After compile and run this
program, we can see a text file program.txt
created in C drive of your computer.
When we open that file, we can see the integer
we entered.

Similarly, fscanf() can be used to read data from file.


Reading from file

#include <stdio.h>
int main()
{ int n; FILE *fptr;
if(fptr=fopen("C:\\program.txt","r"))==NULL)
{ printf("Error! opening file");
exit(1);
/* Program exits if file pointer returns NULL. */ }

fscanf(fptr,"%d",&n);
printf("Value of n=%d",n);
fclose(fptr);
return 0;}
12
 If we have run program above to write in file
successfully, we can get the integer back entered in that
program using this program.

 Other functions like fgetchar(), fputc() etc. can be used


in similar way.

13

You might also like