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

Module 5 Files

The document provides an overview of file handling in C, explaining the concept of files as collections of data stored in secondary memory and the importance of file input and output operations. It details essential actions for using files, such as declaring a file pointer, opening a file with various modes, processing the file, and closing it, along with functions for reading and writing data. Additionally, it includes example programs demonstrating file operations and detecting the end of a file.

Uploaded by

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

Module 5 Files

The document provides an overview of file handling in C, explaining the concept of files as collections of data stored in secondary memory and the importance of file input and output operations. It details essential actions for using files, such as declaring a file pointer, opening a file with various modes, processing the file, and closing it, along with functions for reading and writing data. Additionally, it includes example programs demonstrating file operations and detecting the end of a file.

Uploaded by

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

FILES

Introduction to Files

A File is a collection of data stored in the secondary memory. So far data was entered
into the programs through the keyboard. So Files are used for storing information that can be
processed by the programs. Files are not only used for storing the data, programs are also stored
in files. In order to use files, we have to learn file input and output operations. That is, how data
is read and how to write into a file.
A Stream is the important concept in C. The Stream is a common, logical interface to the
various devices that comprise the computer. So a Stream is a logical interface to a file. There are
two types of Streams, Text Stream and Binary Stream. A Text File can be thought of as a stream
of characters that can be processed sequentially. It can only be processed in the forward
direction.

Using Files in C

To use a file four essential actions should be carried out. These are,
a. Declare a file pointer variable.
b. Open a file using the fopen() function.
c. Process the file using suitable functions.
d. Close the file using the fclose()
Declaration of file pointer:
A pointer variable is used to points a structure FILE. The members of the FILE structure are
used by the program in various file access operation, but programmers do not need to concerned
about them.
FILE *file_pointer_name;
Eg : FILE *fp;
Opening a file
To open a file using the fopen() function. Its syntax is,
FILE *fopen(const char *fname,const char* mode);
const char *fname represents the file name. The file name is like “D:\\501\example.txt”.
Here D: is a drive, 501 is the directory, example.txt is a file name.
const char *mode represents the mode of the file. It has the following values.
Mode Description
R Opens a text file in reading mode
W Opens or create a text file in writing mode
A Opens a text file in append mode
r+ Opens a text file in both reading and writing mode
w+ Opens a text file in both reading and writing mode
a+ Opens a binary file in reading mode
Rb Opens a binary file in reading mode
Wb Opens or create a binary file in writing mode
Ab Opens a binary file in append mode
rb+ Opens a binary file in both reading and writing mode
wb+ Opens a binary file in both reading and writing mode
ab+ Opens a binary file in both reading and writing mode
Difference between write mode and append mode is,
Write (w) mode and Append (a) mode, while opening a file are almost the same. Both are
used to write in a file. In both the modes, new file is created if it doesn't exists already.
The only difference they have is, when you open a file in the write mode, the file is reset,
resulting in deletion of any data already present in the file. While in append mode this will not
happen. Append mode is used to append or add data to the existing data of file(if any). Hence, when
you open a file in Append(a) mode, the cursor is positioned at the end of the present data in the file.

Closing the file


The file must be closed using the fclose() function. Its prototype is ,
int fclose(FILE *fp);
The argument fp is the FILE pinter associated with the stream. Fclose() returns 0 on success and -1
on error.
fflush() used when a file’s buffer is to be written to disk while still using the file. Use of
fflushall() to flush the buffers of all open streams. The prototype of these two functions are,
int flushall(void);
int fflush(FILE *fp);

Reading and writing data files


C provides various functions to working with text files. Four functions can be used to read
text files and four functions that can be used to write text files into a disk.
These are,

Reading data files


fscanf()
fgets()

fgetc()

fread()

Writing data files


fprintf()

fputs()

fputc()

fwrite()

The prototypes of the above functions are,


1. int fscanf(FILE *stream, const char *format,list);
2. int getc(FILE *stream);
3. char *fgets(char *str,int n,FILE *fp);
4. int fread(void *str,size_t size,size_t num, FILE *stream);
5. int fprintf(FILE *stream, const char *format,list);
6. int fputc(int c, FILE *stream);
7. int fputs(const char *str,FILE *stream);
8. int fwrite(const void *str,size_t size, size_t count,FILE *stream);

Example programs on text files:


/* Read a string into a text file using fputs() function */
void main()
{
FILE *fp;
char text[80];
int i;
clrscr();
fp=fopen("Sample.txt","w");
printf("\n Enter the text : ");
gets(text);
fputs(text,fp); //write a string into a file
if(fp!=NULL)
printf("\n The string copied into a text file ");
fclose(fp);
getch();
}

/* Program to copy from one file to another file */


void main()
{
FILE *fp,*fp1;
int ch;
clrscr();
fp=fopen("ex_file4.c","r");
fp1=fopen("ex_file2.c","w");
if(fp==NULL)
printf(“File is not available”);
ch=getc(fp);
while(ch!=EOF)
{
putc(ch,fp1);
ch=getc(fp);
}
fclose(fp);
fclose(fp1);
getch();
}
/* Program to display the content of the file */
void main()
{
FILE *fp;
int ch;
clrscr();
fp=fopen("ex_file4.c","r");
if(fp==NULL)
printf(“File is not available”);
ch=getc(fp);
while(ch!=EOF)
{
printf("%c",ch);
ch=getc(fp);
}
fclose(fp);
getch();
}

/* read and write the content of the file using fprintf() and fscanf() functions */
void main()
{
FILE *fptr;
char name[20];
int age;
float salary;
clrscr();
/* open for writing */
fptr = fopen("abc.txt", "w");
if (fptr == NULL)
{
printf("File does not exists \n");
return;
}
printf("Enter the name \n");
scanf("%s", name);
fprintf(fptr, "Name = %s\n", name);
printf("Enter the age\n");
scanf("%d", &age);
fprintf(fptr, "Age = %d\n", age);
printf("Enter the salary\n");
scanf("%f", &salary);
fprintf(fptr, "Salary = %f\n", salary);
getch();
fclose(fptr);
}

Detecting the end of a file:


When reading from a text-mode file character by character, one can look for the end-of-file
character. The symbolic constant EOF is defined in stdio.h as -1, a value never used by a real
character.
Eg: while((c=getc(fp)!=EOF). This is the general representation of the End Of the File.

You might also like