<< 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

Declarations

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

FILE *file_pointer_name;

Example

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 phylogeny
Odigie Reply
evolutionary history and relationship of an organism or group of organisms
AI-Robot
ok
Deng
what is biology
Hajah Reply
the study of living organisms and their interactions with one another and their environments
AI-Robot
what is biology
Victoria Reply
HOW CAN MAN ORGAN FUNCTION
Alfred Reply
the diagram of the digestive system
Assiatu Reply
allimentary cannel
Ogenrwot
How does twins formed
William Reply
They formed in two ways first when one sperm and one egg are splited by mitosis or two sperm and two eggs join together
Oluwatobi
what is genetics
Josephine Reply
Genetics is the study of heredity
Misack
how does twins formed?
Misack
What is manual
Hassan Reply
discuss biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles
Joseph Reply
what is biology
Yousuf Reply
the study of living organisms and their interactions with one another and their environment.
Wine
discuss the biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles in an essay form
Joseph Reply
what is the blood cells
Shaker Reply
list any five characteristics of the blood cells
Shaker
lack electricity and its more savely than electronic microscope because its naturally by using of light
Abdullahi Reply
advantage of electronic microscope is easily and clearly while disadvantage is dangerous because its electronic. advantage of light microscope is savely and naturally by sun while disadvantage is not easily,means its not sharp and not clear
Abdullahi
cell theory state that every organisms composed of one or more cell,cell is the basic unit of life
Abdullahi
is like gone fail us
DENG
cells is the basic structure and functions of all living things
Ramadan
What is classification
ISCONT Reply
is organisms that are similar into groups called tara
Yamosa
in what situation (s) would be the use of a scanning electron microscope be ideal and why?
Kenna Reply
A scanning electron microscope (SEM) is ideal for situations requiring high-resolution imaging of surfaces. It is commonly used in materials science, biology, and geology to examine the topography and composition of samples at a nanoscale level. SEM is particularly useful for studying fine details,
Hilary
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




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?

Ask