0% found this document useful (0 votes)
27 views

Module5 Files

Files allow storing data outside of a program. There are two types: text files store plain text while binary files store data in 0s and 1s. In C, files are accessed via streams - stdin, stdout, and stderr which represent standard input, output, and errors. When a file stream is opened, a buffer in memory manages data transfer between the program and disk. Common file operations include opening, closing, reading and writing. Files are opened via fopen() specifying a mode like read, write, or append.

Uploaded by

Prabhat Advait
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Module5 Files

Files allow storing data outside of a program. There are two types: text files store plain text while binary files store data in 0s and 1s. In C, files are accessed via streams - stdin, stdout, and stderr which represent standard input, output, and errors. When a file stream is opened, a buffer in memory manages data transfer between the program and disk. Common file operations include opening, closing, reading and writing. Files are opened via fopen() specifying a mode like read, write, or append.

Uploaded by

Prabhat Advait
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

MODULE 5 –STRUCTURES, UNION AND FILES 22POP13

1. FILES: A File is a collection of data stored on a secondary storage device like hard
disk.
 Till now, we had been processing data that was entered through the computer’s
keyboard. But this task can become very tedious especially when there is a huge
amount of data to be processed.
 A better solution, therefor, is to combine all the input data into a file and then
design a C program to read this data from the file whenever required.

2. Why files are needed?


 When a program is terminated, the entire data is lost. Storing in a file will preserve your data
even if the program terminates.

 If you have to enter a large number of data, it will take a lot of time to enter them all.
However, if you have a file containing all the data, you can easily access the contents of the file
using a few commands in C.
 You can easily move your data from one computer to another without any changes.

3. STREAMS IN C
 In c, the standard streams are termed as pre-connected input and output channels
between a text terminal and the program.
 A stream is a logical interface to the devices that are connected to the computer.
 Stream is widely used as a logical interface to a file where a file can refer to a disk
file, the computer screen, keyboard, etc.
 The 3 standard Streams in C are
1. Standardinput (stdin)
2. Standardoutput (stdout)
3. Standard error (stderr)

Standardinput (stdin): Standard input or the stream from which the program receives
its data. The program requests transfer of data using the real operations. However, not all
programs require input. Generally, unless redirected, input for a program is expected from the
keyboard.

Standardoutput (stdout):Standard output is the stream where a program writes its


output data. The program requests data transfer using the write operation. However, not all
programs generate output.

Prof.Kavitha B, Dept of ISE, RNSIT Page 1


MODULE 5 –STRUCTURES, UNION AND FILES 22POP13

Standarderror(stderr):
Standard error is basically an output stream used by programs to report error messages or
diagnostics. It is a stream independent of standard output and can be redirected separately.

A stream is linked to a file using an open operation and dissociated from a file using a close
operation.

BUFFER ASSOCIATED WITH FILE STREAMS:


 When a stream linked to a disk file is created, a buffer is automatically created and
associated with the stream.
 A buffer is nothing but a block of memory that is used for temporary storage of data that
has to be read from or written to a file.
 Buffers are needed because disk drives are block oriented devices as they can operate
efficiently when data has to be read/written in blocks of certain size. An ideal buffer size
is hardware-dependent.
 The buffer act as a interface between the stream and the disk hardware.
 When the program has to write data to the stream, it is saved in the buffer till it is full.
Then the entire contents of the buffer are written to the disk as a block.

program writes data to buffer Data from the buffer is written to the disk file

PROGRAM BUFFER
DISK

 Similarly, when reading data from a dis file, the data is read as a bloc from the file
and written into the buffer.
 The program reads data from the buffer. The creation and operation of the buffer
is automatically handled by the operating system.

Types of Files

When dealing with files, there are two types of files you should know about:

1. Text files

2. Binary files

Prof.Kavitha B, Dept of ISE, RNSIT Page 2


MODULE 5 –STRUCTURES, UNION AND FILES 22POP13

1. Text files

Text files are the normal .txt files. You can easily create text files using any
simple text editors such as Notepad.
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, and provide the
least security and takes 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
In C, you can perform four major operations on files, either text or binary:

1. Creating a new file

2. Opening an existing file

3. Closing a file

4. Reading from and writing information to a file

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.

Prof.Kavitha B, Dept of ISE, RNSIT Page 3


MODULE 5 –STRUCTURES, UNION AND FILES 22POP13

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.
The syntax for opening a file in standard I/O is:

ptr = fopen("fileopen","mode");

For example,

fopen("E:\\cprogram\\newprogram.txt","w");

fopen("E:\\cprogram\\oldprogram.bin","rb");

 Let's suppose the file newprogram.txt doesn't exist in the location E:\cprogram .

The first function creates a new file named newprogram.txt and opens it for
writing as per the mode 'w'.
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

If the file does not exist, fopen() returns


r Open text file for reading.
NULL.

Prof.Kavitha B, Dept of ISE, RNSIT Page 4


MODULE 5 –STRUCTURES, UNION AND FILES 22POP13

Opening Modes in Standard I/O

Mode Meaning of Mode During Inexistence of file

If the file does not exist, fopen() returns


rb Open for reading in binary mode.
NULL.

If the file exists, its contents are


w Open text file for writing. overwritten.
If the file does not exist, it will be created

If the file exists, its contents are


wb Open for writing in binary mode. overwritten.
If the file does not exist, it will be created

Open for append.


a If the file does not exist, it will be created
Data is added to the end of the text file.

Open for append in binary mode.


ab If the file does not exist, it will be created
Data is added to the end of the file.

If the file does not exist, fopen() returns


r+ Open text file for both reading and writing.
NULL.

Open for both reading and writing in binary If the file does not exist, fopen() returns
rb+
mode. NULL.

If the file exists, its contents are


w+ Open text file for both reading and writing. overwritten.
If the file does not exist, it will be created

If the file exists, its contents are


Open for both reading and writing in binary
wb+ overwritten.
mode.
If the file does not exist, it will be created

a+ Open text file for both reading and appending. If the file does not exist, it will be created

Open for both reading and appending in binary


ab+ If the file does not exist, it will be created
mode.

Prof.Kavitha B, Dept of ISE, RNSIT Page 5


MODULE 5 –STRUCTURES, UNION AND FILES 22POP13

Closing a File
The file (both text and binary) should be closed after reading/writing.

Closing a file is performed using the fclose() function.

fclose(fptr);

Here, fptr is a file pointer associated with the file to be closed.

Reading and writing to a text file


For reading and writing to a text file, we use the
functions fprintf() and fscanf().

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.

Prof.Kavitha B, Dept of ISE, RNSIT Page 6

You might also like