Files Session 1
Files Session 1
Files
Concept of a file, Opening and Closing files, file input / output functions (standard library input / output
functions for text files)
Definition:
FILE is a predefined structure data type which is defined in library file called stdio.h.
(OR)
FILE is a set of records that can be accessed through a set of library functions.
Syntax:
FILE *filepointerobject;
Example:
FILE *fp; // where FILE is a keyword and “fp” is a file pointer object
✓ C supports a number of functions that have the ability to perform basic file operations, which
include:
1. Naming a file
2. Opening a file
3. Reading from a file
4. Writing data into a file
5. Closing a file
✓ The fprintf( ) function is identical to printf( ) function except that they work on files.
✓ The first argument of this function is a file pointer which specifies the file to be used.
Example:
fprintf(fp, “%s%d%f”,name,age,weight);
Here, name is an array variable of type char and age is an int variable
Reading from Files : fscanf( )
✓ It is a predefined file handling function which is used to read the content from a file.
✓ The fscanf( ) function is identical to scanf( ) function except that they work on files.
✓ The first argument of this function is a file pointer which specifies the file to be used.
3 Department of Computer Science and Engineering
Problem Solving and Programming with C GITAM
Syntax: fscanf(fp, “controlstring”,var_list);
This statement would cause the reading of items in the control string.
Example:
fscanf(fp, “%s%d”,name,&quantity”);
Where fp id is a file pointer associated with a file that has been opened for reading. The
control string is file output specifications list may include variable, constant and string.
Closing the File : fclose( fp)
✓ When we have finished reading from the file, we need to close it. This is done using the function
fclose through the statement, fclose( fp );
Note:
✓ During a write to a file, the data written is not put on the disk immediately. It is stored in a buffer.
When the buffer is full, all its contents are actually written to the disk. The process of emptying
the buffer by writing its contents to disk is called flushing the buffer.
✓ Closing the file flushes the buffer and releases the space taken by the FILE structure which is
returned by fopen. Operating System normally imposes a limit on the number of files that can be
opened by a process at a time. Hence, closing a file means that another can be opened in its place.
Hence, if a particular FILE pointer is not required after a certain point in a program, pass it to
fclose and close the file.
Syntax: fclose(filepointer);
Example:
fclose(fp); // Where fp is a file pointer
Example Program 01:Using write mode to create new file and to write the data in file.
#include<stdio.h>
void main()
{
int i;
char name[15];
FILE *fp; fp=fopen("data1.txt","w");
printf("Input an integer");
scanf("%d",&i);
printf("Enter your name:");
scanf("%s",name);
fprintf(fp,"%d\n%s",i,name);
fclose(fp);
}
Example Program 02: Using read mode to read the data from file.
#include<stdio.h>
void main()
{
int i;
char name[15];
FILE *fp;
fp=fopen("data1.txt","r");
fscanf(fp,"%d%s",&i,name);
printf("The integer in data1.txt is %d\n",i);
printf("The String in data1.txt is %s",name);
fclose(fp);
}
Output:
The integer in data1.txt is 5
The String in data1.txt is gitam
Example Program 03: Using append mode to add the data in the existing file.
#include<stdio.h>
void main()
{
char name[15];
FILE *fp;
fp=fopen("data2.txt","a");
printf("Enter ur name:");
scanf("%s",name);
fprintf(fp,"%s",name);
fclose(fp);
fp=fopen("data2.txt","r");
if(fscanf(fp,"%s",name))
printf("%s",name);
fclose(fp);
}