<< Chapter < Page Chapter >> Page >
  • No special processing of the data occurs and each byte of data is transferred to or from the disk unprocessed.
  • C Programming Language places no constructs on the file, and it may be read from, or written to, in any manner chosen by the programmer.

Binary files can be either processed sequentially or, depending on the needs of the application, they can be processed using random access techniques. In C Programming Language, processing a file using random access techniques involves moving the current file position to an appropriate place in the file before reading or writing data. This indicates a second characteristic of binary files – they a generally processed using read and write operations simultaneously.

For example, a database file will be created and processed as a binary file. A record update operation will involve locating the appropriate record, reading the record into memory, modifying it in some way, and finally writing the record back to disk at its appropriate location in the file. These kinds of operations are common to many binary files, but are rarely found in applications that process text files.

For all file operations you should always follow the 5-step plan as outlined below.

  1. Declare file pointer.
  2. Attach the file pointer to the file (open file).
  3. Check file opened correctly.
  4. Read or Write the data from or to the file.
  5. Close the file.

Operations on files


In C, we usually create variables of type FILE * to point to a file located on the computer.

FILE *file_pointer_name;


FILE * f1, * f2;

Open files

First things first: we have to open a file to be able to do anything else with it. For this, we use fopen function, like all the I/O functions, is made available by the stdio.h library. The fopen() function prototype is as follows.

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

In the above prototype, there are two arguments:

  • filename is a string containing the name of the file to be opened. So if your file sits in the same directory as your C source file, you can simply enter the filename in here - this is probably the one you'll use most.
  • mode determines how the file may be accessed.
Mode Meaning
“r” Open a file for read only, starts at beginning of file (default mode).
“w” Write-only, truncates existing file to zero length or create a new file for writing.
“a” Write-only, starts at end of file if file exists,otherwise creates a new file for writing.
“r+” Open a file for read-write, starts at beginning of file. If the file is not exist, it will cause an error.
“w+” Read-write, truncates existing file to zero length or creates a new file for reading and writing.
“a+” Read-write, starts at end of file if file exists, otherwise creates a new file for reading and writing.

So there are 12 different values that could be used: "rt", "wt", "at", "r+t", "w+t", "a+t" and "rb", "wb", "ab", "r+b", "w+b", "a+b".

Character Type
“t” Text File
“b” Binary File
When work with the text file, you also can use only "r", "w", "a", "r", "w”, "a", instead of "rt", "wt", "at", "r+t", "w+t", "a+t" respectively.

Questions & Answers

what is variations in raman spectra for nanomaterials
Jyoti Reply
I only see partial conversation and what's the question here!
Crow Reply
what about nanotechnology for water purification
RAW Reply
please someone correct me if I'm wrong but I think one can use nanoparticles, specially silver nanoparticles for water treatment.
yes that's correct
I think
what is the stm
Brian Reply
is there industrial application of fullrenes. What is the method to prepare fullrene on large scale.?
industrial application...? mmm I think on the medical side as drug carrier, but you should go deeper on your research, I may be wrong
How we are making nano material?
what is a peer
What is meant by 'nano scale'?
What is STMs full form?
scanning tunneling microscope
how nano science is used for hydrophobicity
Do u think that Graphene and Fullrene fiber can be used to make Air Plane body structure the lightest and strongest. Rafiq
what is differents between GO and RGO?
what is simplest way to understand the applications of nano robots used to detect the cancer affected cell of human body.? How this robot is carried to required site of body cell.? what will be the carrier material and how can be detected that correct delivery of drug is done Rafiq
what is Nano technology ?
Bob Reply
write examples of Nano molecule?
The nanotechnology is as new science, to scale nanometric
nanotechnology is the study, desing, synthesis, manipulation and application of materials and functional systems through control of matter at nanoscale
Is there any normative that regulates the use of silver nanoparticles?
Damian Reply
what king of growth are you checking .?
What fields keep nano created devices from performing or assimulating ? Magnetic fields ? Are do they assimilate ?
Stoney Reply
why we need to study biomolecules, molecular biology in nanotechnology?
Adin Reply
yes I'm doing my masters in nanotechnology, we are being studying all these domains as well..
what school?
biomolecules are e building blocks of every organics and inorganic materials.
anyone know any internet site where one can find nanotechnology papers?
Damian Reply
sciencedirect big data base
Introduction about quantum dots in nanotechnology
Praveena Reply
what does nano mean?
Anassong Reply
nano basically means 10^(-9). nanometer is a unit to measure length.
do you think it's worthwhile in the long term to study the effects and possibilities of nanotechnology on viral treatment?
Damian Reply
absolutely yes
how did you get the value of 2000N.What calculations are needed to arrive at it
Smarajit Reply
Privacy Information Security Software Version 1.1a
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get the best Algebra and trigonometry course in your pocket!

Source:  OpenStax, Introduction to computer science. OpenStax CNX. Jul 29, 2009 Download for free at http://cnx.org/content/col10776/1.1
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Introduction to computer science' conversation and receive update notifications?