Files in C
Files in C
Introduction
Types of Files
When dealing with files, there are two types of files you should know
about:
Text files
Binary files
1. Text files
Text files are the normal .txt files that you can easily create using
Notepad or any simple text editors.
When you open those files, you'll see all the contents within the file
as plain text. You can easily edit or delete the contents.
They take minimum effort to maintain, are easily readable, and
provide least security and takes bigger storage space.
2. Binary files
Binary files are mostly the .bin files in your computer.
Instead of storing data in plain text, they store it in the binary form
(0's and 1's).
They can hold higher amount of data, are not readable easily and
provides a better security than text files.
File Operations
In C, you can perform four major operations on the file,
either text or binary:
Creating a new file
Opening an existing file
Closing a file
Reading from and writing information to a file
Working with files
When working with files, you need to declare a pointer of
type file.
This declaration is needed for communication between the
file and program.
FILE *fptr;
Opening a file
You can use the fopen( ) function to create a new file or to
open an existing file.
This call will initialize an object of the type FILE, which
contains all the information necessary to control the
stream.
The prototype of this function call is as follows
fptr = fopen( const char * filename, const char *
mode );
Here, filename is a string literal, which you will use to
name your file, and access mode can be to read, write,
append, or both read & write.
If you are going to handle binary files, then you will use
following access modes
"rb", "wb", "ab", "rb+", "wb+", "ab+"
Mode
Description
r+
w+
a+
Closing a File
To close a file, use the fclose( ) function.
The prototype of this function is
fclose( FILE *fptr );
The fclose(-) function returns zero on success, or EOF if
there is an error in closing the file.
This function actually flushes any data still pending in the
buffer to the file, closes the file, and releases any memory
used for the file.
The EOF is a constant defined in the header file stdio.h.
Writing a File
Following is the simplest function to write individual characters to a
stream
int fputc( int c, FILE *fptr );
The function fputc() writes the character value of the argument c
to the output stream referenced by fptr.
It returns the written character written on success otherwise EOF if
there is an error.
You can use the following functions to write a null-terminated string
to a stream
int fputs( const char *s, FILE *fptr );
The function fputs() writes the string s to the output stream
referenced by fptr.
It returns a non-negative value on success, otherwise EOF is
returned in case of any error.
You can use
int fprintf(FILE *fp,const char *format, ...)
function as well to write a string into a file
#include <stdio.h>
main()
{
FILE *fptr;
fptr = fopen(c:\\tmp\\test.txt", "w+");
fprintf(fptr, "This is testing for fprintf...\n");
fputs("This is testing for fputs...\n", fptr);
fclose(fptr);
}
Before executing this program we need to create a
directory by name tmp under C drive in the machine
When the above code is compiled and executed, it creates
a new file test.txt in tmp directory and writes two lines
using two different functions
Reading a File
Following is the simplest function to read a single character from a file
int fgetc( FILE * fptr );
The fgetc() function reads a character from the input file referenced by
fptr.
The return value is the character read, or in case of any error, it returns
EOF.
The following function allows to read a string from a stream
char *fgets( char *buf, int n, FILE *fptr );
The functions fgets() reads up to n-1 characters from the input stream
referenced by fptr.
It copies the read string into the buffer buf, appending a null character
to terminate the string.
If this function encounters a newline character '\n' or the end of the file
EOF before they have read the maximum number of characters, then it
returns only the characters read up to that point including the new line
character.
You can also use
int fscanf(FILE *fp, const char *format, ...)
#include <stdio.h>
main()
{
FILE *fptr;
char buff[255];
if ((fptr = fopen("C:\\tmp\\test.txt","r")) == NULL){
printf("Error! opening file");
exit(0);
}
fscanf(fptr, "%s", buff);
printf("1 : %s\n", buff );
fgets(buff, 255, (FILE*)fptr);
printf("2: %s\n", buff );
fgets(buff, 255, (FILE*)fptr);
printf("3: %s\n", buff );
fclose(fp);
}
Programs on files