0% found this document useful (0 votes)
204 views15 pages

File Handling Techniques in C

File handling in C allows programs to perform operations like creating, opening, reading, writing and deleting files. Some key functions for file handling include fopen() to open files, fprintf() and fputs() to write to files, fscanf() and fgets() to read from files, and fclose() to close files. Files can be opened in different modes like read, write, append etc and these functions make it possible to manage files from within C programs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
204 views15 pages

File Handling Techniques in C

File handling in C allows programs to perform operations like creating, opening, reading, writing and deleting files. Some key functions for file handling include fopen() to open files, fprintf() and fputs() to write to files, fscanf() and fgets() to read from files, and fclose() to close files. Files can be opened in different modes like read, write, append etc and these functions make it possible to manage files from within C programs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

File Handling in C

File handling in C enables us to create, update, read, and delete


the files stored on the local file system through our C program.

The following operations can be performed on a file.

■ Creation of the new file


■ Opening an existing file
■ Reading from the file
■ Writing to the file
■ Deleting the file
Functions for file handling

fopen() opens new or existing file


fprintf() write data into the file
fscanf() reads data from the file
fputc() writes a character into the file
fgetc() reads a character from file
fclose() closes the file
fseek() sets the file pointer to given position
fputw() writes an integer to file
fgetw() reads an integer from file
ftell() returns current position
rewind() sets the file pointer to the beginning of the file
Opening Files

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 −

FILE *fopen( const char * filename, const char * mode );


filename is a string literal, which you will use to name your file, and
access mode can have one of the following values −

r Opens an existing text file for reading purpose.


w Opens a text file for writing. If it does not exist, then a new
file is created. Here your program will start writing content
from the beginning of the file.
a Opens a text file for writing in appending mode. If it does not
exist, then a new file is created. Here your program will start
appending content in the existing file content.
r+ Opens a text file for both reading and writing.
w+ Opens a text file for both reading and writing. It first
truncates the file to zero length if it exists, otherwise creates
a file if it does not exist.
a+ Opens a text file for both reading and writing. It creates the
file if it does not exist. The reading will start from the beginning
but writing can only be appended.
Mode Description

r opens a text file in read mode


w opens a text file in write mode
a opens a text file in append mode
r+ opens a text file in read and write mode
w+ opens a text file in read and write mode
a+ opens a text file in read and write mode
rb opens a binary file in read mode
wb opens a binary file in write mode
ab opens a binary file in append mode
rb+ opens a binary file in read and write mode
wb+ opens a binary file in read and write mode
ab+ opens a binary file in read and write mode
Closing a File

To close a file, use the fclose( ) function. The prototype of this


function is −

int fclose( FILE *fp );

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.

There are various functions provided by C standard library to read


and write a file, character by character, or in the form of a fixed
length string.
Writing a File

Following is the simplest function to write individual characters to a


stream −

int fputc( int c, FILE *fp );

The function fputc() writes the character value of the argument c


to the output stream referenced by fp. It returns the written
character written on success otherwise EOF if there is an error.

To write a null-terminated string to a stream −

int fputs( const char *s, FILE *fp );


The function fputs() writes the string s to the output stream
referenced by fp. 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>

void main() {
FILE *fp;

fp = fopen("test.txt", "w+");
fprintf(fp, "This is testing for fprintf...\n");
fputs("This is testing for fputs...\n", fp);
fclose(fp);
}
Reading a File

int fgetc( FILE * fp );

The fgetc() function reads a character from the input file


referenced by fp. 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 *fp );

The functions fgets() reads up to n-1 characters from the input


stream referenced by fp. 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, ...)
function to read strings from a file, but it stops reading after
encountering the first space character.
#include <stdio.h>

void main() {
First, fscanf() read just This because after that,
FILE *fp; it encountered a space, second call is for
char buff[255]; fgets() which reads the remaining line till it
encountered end of line. Finally, the last call
fgets() reads the second line completely.
fp = fopen("test.txt", "r");
fscanf(fp, "%s", buff);
printf("1 : %s\n", buff );

fgets(buff, 255, (FILE*)fp);


printf("2: %s\n", buff );

fgets(buff, 255, (FILE*)fp);


printf("3: %s\n", buff );
fclose(fp);

}
Binary I/O Functions

There are two functions, that can be used for binary input and
output −

size_t fread(void *ptr, size_t size_of_elements, size_t


number_of_elements, FILE *a_file);

size_t fwrite(const void *ptr, size_t size_of_elements, size_t


number_of_elements, FILE *a_file);

Both of these functions should be used to read or write blocks of


memories - usually arrays or structures.

You might also like