Files in C
Files in C
Files in C
FILES in C
C File Handling
• A file is a container in computer storage devices used for storing data.
• 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, provide the
least security, and take 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 a higher amount of data, are not readable easily, and
provides better security than text files.
File Operations
• C file operations refer to the different possible operations that we can
perform on a file in C such as:
1.Creating a new file – fopen() with attributes as “a” or “a+” or “w”
or “w+”
2.Opening an existing file – fopen()
3.Reading from file – fscanf() or fgets()
4.Writing to a file – fprintf() or fputs()
5.Moving to a specific location in a file – fseek(), rewind()
6.Closing a file – fclose()
• The highlighted text mentions the C function used to perform the file
operations.
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 the
program.
FILE *fptr;
Opening a file - for creation and edit
Opening a file is performed using the fopen() function defined in
the stdio.h header file.
ptr = fopen("fileopen","mode");
The writing mode allows you to create and edit (overwrite) the contents of the file.
•Now let's suppose the second binary file oldprogram.bin exists in the location E:\cprogram.
The second function opens the existing file for reading in binary mode 'rb’.
The reading mode only allows you to read the file, you cannot write into the file.
Opening Modes in Standard I/O
Mode Meaning of Mode During Inexistence of file
r Open for reading. If the file does not exist, fopen() returns NULL.
rb Open for reading in binary mode. If the file does not exist, fopen() returns NULL.
r+ Open for both reading and writing. If the file does not exist, fopen() returns NULL.
Open for both reading and writing in If the file exists, its contents are overwritten.
wb+
binary mode. If the file does not exist, it will be created.
a+ Open for both reading and appending. If the file does not exist, it will be created.
fclose(fptr);
They are just the file versions of printf() and scanf(). The
only difference is that fprintf() and fscanf() expects a
pointer to the structure FILE.
Example 1: Write to a text file
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num;
FILE *fptr;
if(fptr == NULL)
{
Example 2: Read from a text file
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num;
FILE *fptr;
fputs() Prints the whole line in the file and a newline at the end.
Functions fread() and fwrite() are used for reading from and
writing to a file on the disk respectively in case of binary
files.
Writing to a binary file
To write into a binary file, you need to use the fwrite() function. The
functions take four arguments:
• Syntax of fread()
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *file_pointer);
Parameters:
• ptr: pointer to the block of memory to read.
• size: the size of each element to read(in bytes).
• nmemb: number of elements.
• file_pointer: FILE pointer to the input file stream.
Return Value:
• Number of objects written.
Program to Read from a binary file using
fread()
// C Program to Read from a binary file using fread()
#include <stdio.h>
#include <stdlib.h>
struct threeNum {
int n1, n2, n3;
};
int main()
{
int n;
struct threeNum num;
FILE* fptr;
if ((fptr = fopen("D:\\program.bin", "rb")) == NULL) {
printf("Error! opening file");
fseek() in C
• If we have multiple records inside a file and need to access a particular
record that is at a specific position, so we need to loop through all the
records before it to get the record.
• fseek() function in C seeks the cursor to the given record in the file.
Syntax for fseek()
int fseek(FILE *ptr, long int offset, int pos);
// C Program to demonstrate the use of fseek() in C
#include <stdio.h>
int main()
{
FILE* fp;
fp = fopen("D:\\program.txt", "r");
return 0;
}
rewind() in C
• The rewind() function is used to bring the file pointer to the beginning
of the file. It can be used in place of fseek() when you want the file
pointer at the start.
• Syntax of rewind()
rewind (file_pointer);
Example
// C program to illustrate the use of rewind
#include <stdio.h>
int main()
{
FILE* fptr;
fptr = fopen("D:\\program.txt", "w+");
fprintf(fptr, "good afternoon students\n");
// using rewind()
rewind(fptr);
printf("%s", buf);
Difference between fseek() and rewind() in C
fseek rewind
fseek is used to move the file pointer to a specific rewind is used to move the file pointer to the
position. beginning of the file stream.
Syntax: Syntax:
int fseek(FILE *stream, long offset, int origin); void rewind(FILE *stream);
The stream error indicator is not cleared. The stream error indicator is cleared.