You are on page 1of 7

C File IO handling, Difference b/w Text and Binary file

(../index.html)

Page 1 of 7

File IO - Input Output


PROGRAMS (../SHOWPROGRAM.ASPX)

Advertisements

File IO in C
As we know, at the time of execution, every program comes in main memory to execute. Main
memory is volatile and the data would be lost once the program is terminated. If we need the
same data again, we have to store the data in a file on the disk. A file is sequential stream of
bytes ending with an end-of-file marker.
Types of file supported by C:

Text Files
Binary Files
Difference between text file and binary file
Text file is human readable because everything is stored in terms of text. In binary file
everything is written in terms of 0 and 1, therefore binary file is not human readable.

A newline(\n) character is converted into the carriage return-linefeed combination before


being written to the disk. In binary file, these conversions will not take place.

http://www.tutorialdost.com/C-Programming-Tutorial/27-C-File-IO-Handling.aspx

7/23/2016

C File IO handling, Difference b/w Text and Binary file

Page 2 of 7

In text file, a special character, whose ASCII value is 26, is inserted after the last
character in the file to mark the end of file. There is no such special character present in
the binary mode files to mark the end of file.

In text file, the text and characters are stored one character per byte. For example, the
integer value 1245 will occupy 2 bytes in memory but it will occupy 5 bytes in text file. In
binary file, the integer value 1245 will occupy 2 bytes in memory as well as in file.

Declaration of File Pointer


To access any file, we need to declare apointer to FILE structure and then associate it with
the particular file. This pointer is referred to as file pointer.

Syntax for declaring file pointer


FILE * fp;

A pointer to FILE structure contains information, such as size, current file pointer position,
type of file etc., to perform operation on the file.

Opening a file using fopen() function


The fopen() function takes two arguments, the name of the file amd the mode in which the file
is to be opened. Mode specify the purpose of opening the file i.e, whether for reading or
writing.

Syntax for opening the file in C


fp = fopen(char *filename,char *mode);

When fopen() function opens a file in memory, it returns a pointer to this particular file. If fopen
() function can't open the file then it will return NULL.

http://www.tutorialdost.com/C-Programming-Tutorial/27-C-File-IO-Handling.aspx

7/23/2016

C File IO handling, Difference b/w Text and Binary file

Page 3 of 7

Example for opening the file in C


void main()
{
FILE *fp;
fp = fopen("file1.txt","r");

//Statement 1

if(fp == NULL)
{
printf("\nCan't open file or file doesn't exist.");
exit(0);
}

In the above example, statement 1 will open an existing in text mode and return a pointer to
file. If file will not open, an appropriate message will be displayed.

File Opening Modes


Mode

"r" or "rt"

"w" or "wt"

"a" or "at"

rb

wb

ab

"rt+" or "r+t"

Purpose

Open a text file for reading, The file must already exist.
Open a text file for writing. If the file already exists, all the data will lost. If it doesnt
exist, it will be created.
Open a text file for appending. Data will be added at the end of the existing file. If file
doesnt exist, it will be created.
Open a binary file for reading, The file must already exist.
Open a binary file for writing. If the file already exists, its contents will be overwritten.
If it doesnt exist, it will be created.
Open a binary file for appending. Data will be added at the end of the existing file. If
file doesnt exist, it will be created.
Open text file for reading and writing. The file must already exist.

http://www.tutorialdost.com/C-Programming-Tutorial/27-C-File-IO-Handling.aspx

7/23/2016

C File IO handling, Difference b/w Text and Binary file

"wt+" or
"w+t"
"at+" or
"a+t"
"rb+" or
"r+b"
"wb+" or
"w+b"
"ab+" or
"a+b"

Page 4 of 7

Open text file for reading and writing. If file doesn't exist, it will be created.

Open text file for reading and appending. If file doesn't exist, it will be created.

Open binary file for reading and writing. The file must already exist.

Open binary file for reading and writing. If file doesn't exist, it will be created.

Open binary file for reading and appending. If file doesn't exist, it will be created.

Closing a File using fclose() function


When the reading or writing of a file is finished, the file should be closed properly using fclose
() function. The fclose() function does the followling tasks:

Flushes any unwritten data from memory.


Discards any unread buffered input.
Frees any automatically allocated buffer
Finally, close the file.

Syntax for closing the file in C


int fclose( FILE* );

http://www.tutorialdost.com/C-Programming-Tutorial/27-C-File-IO-Handling.aspx

7/23/2016

C File IO handling, Difference b/w Text and Binary file

Page 5 of 7

Example for closing the file in C


void main()
{
FILE *fp;
fp = fopen("file1.txt","r");
if(fp == NULL)
{
printf("\nCan't open file or file doesn't exist.");
exit(0);
}
------------------fclose(fp);
}

Reading and Writing File


We can read data from file and write data to file in many ways.

Reading or writing characters using fgetc() and fputc() functions. More info (C-file-io-fgetcfputc-function.aspx)

Reading or writing string using fgets() and fputs() functions. More info (C-file-io-fgetsfputs-function.aspx)

Reading or writing integers using getw() and putw() functions. More info (C-file-io-getwputw-function.aspx)

Reading or writing formatted IO using fscanf() and fprintf() functions. More info (C-file-iofprintf-fscanf-function.aspx)

Reading or writing records using fread() and fwrite() functions. More info (C-file-io-freadfwrite-function.aspx)

File Positioning Functions


File positioning functions are used to move the pointer in a file to the desired position, without

http://www.tutorialdost.com/C-Programming-Tutorial/27-C-File-IO-Handling.aspx

7/23/2016

C File IO handling, Difference b/w Text and Binary file

Page 6 of 7

closing and re-opning the file.


C provides followling three functions to move the pointer in the file:

The rewind() function is used to move the pointer to the beining of the file. More info
(C-file-io-rewind-function.aspx)

The ftell() function is used to retrieve the current position of pointer in the file. More info
(C-file-io-ftell-function.aspx)

The fseek() function is used to move the pointer to the desired position in the file. More
info (C-file-io-fseek-function.aspx)

Related topics
- Write a program to count total number of blank spaces in a file. (Download-CPrograms.aspx)
- Write a program to count total number of consonant in a file. (Download-C-Programs.aspx)
- Write a program to copy one file to another. (Download-C-Programs.aspx)
- Write a program to copy all even numbers from one file to another. (Download-CPrograms.aspx)
- Write a menu driven program to add, display, search, update and delete the student record.
(Download-C-Programs.aspx)
Advertisement

Follow us

http://www.tutorialdost.com/C-Programming-Tutorial/27-C-File-IO-Handling.aspx

7/23/2016

C File IO handling, Difference b/w Text and Binary file

Page 7 of 7

(C-Programming-Introduction.aspx) (C-Programming-Introduction.aspx)
(C-Programming-Introduction.aspx) (C-Programming-Introduction.aspx)
Advertisements

Site Map
Learn C Programming
Learn C++ Programming
Learn Core Java
Learn VB.NET
Learn C Sharp.net

Examples
C Examples
C++ Examples
Core Java Examples
Visual Basic.NET Examples
C Sharp.net Examples

About Us
This website is designed for readers who have less or no programming experience. Even
the experienced programmers will find this website equally useful. We have covered all
the basic of C, C++, C#, JAVA, VB.NET, ASP.NET, etc..., programming language with easy
examples and their descriptions.

COPYRIGHT 2084 COMPANY NAME

http://www.tutorialdost.com/C-Programming-Tutorial/27-C-File-IO-Handling.aspx

7/23/2016

You might also like