Vasavi College of Engineering
(Autonomous)
Department of Electronics and Communication
Engineering
Course: Programming for Problem Solving (U22ES120CS)
Text Input / Output - Files
M. Ramanjaneyulu
Assistant Professor
ECE Department
Files:
• A file is an external collection of related data treated as a unit. The
primary purpose of a file is to keep a record of data.
• Files are stored in auxiliary or secondary storage devices i.e. hard disk, CD,
DVD etc.
• When the computer reads the data move from the external device to
memory; when it writes, the data move from memory to the external
device. This data movement often uses a special work area known as a
buffer.
• A buffer is a temporary storage area that holds data while they are being
transferred to or from memory.
• As a file is being read, there eventually comes a point when all the data
have been input. At this point, the file is said to be at End Of File (EOF).
• The End Of File in an auxiliary device is detected automatically by the
device and passed to the program. It is the programmer’s job to test for
End Of File.
M. Ramanjaneyulu, Assistant Professor, ECE
Dept, VCE
Streams:
• Although the source and destination of data is a file or a physical device in
C, data is input to and output from a stream.
• A stream can be associated with a physical device, such as a terminal, or
with a file stored in auxiliary memory.
M. Ramanjaneyulu, Assistant Professor, ECE
Dept, VCE
• C uses two types of streams: text and binary. A text stream consists of a
sequence of characters divided into lines with each line terminated by a
new line (\n).
• A binary stream consists of a sequence of data values such as integer, real
using their memory representation.
• A file exists as an independent entity with a name known to the operating
system. A stream is an entity created by the program.
• In general there are four steps to processing a file.
First create the stream.
Open the file, which associates the stream name with the file name.
Read or Write data, that is process the file
Close the file after processing.
• Creating a stream is done as follows:
FILE *fp;
The FILE type is a structure that contains the information needed for
reading and writing a file.
In the above example fp is a pointer to the stream.
M. Ramanjaneyulu, Assistant Professor, ECE
Dept, VCE
File open and close:
• The function that prepares a file for processing is fopen() . It does two
things: First, it makes the connection between the physical file and the file
stream in the program. Second, it creates a program file structure to store
the information needed to process the file.
• To open a file, we need to specify the physical filename and its mode as
shown in the statement
fopen(“filename”, “mode”);
• A filename is a string that supplies the name of the physical file as it is
known to the external world.
• The address of the file structure that contains the file information is
returned by fopen, and store it in a suitable pointer variable to read or
write the file.
File modes:
• The mode shows how we will use the file: for reading, for writing, or for
appending i.e. adding new data at the end of the current file.
• There are 6 modes in C, 3 for text streams and 3 for binary streams.
M. Ramanjaneyulu, Assistant Professor, ECE
Dept, VCE
M. Ramanjaneyulu, Assistant Professor, ECE
Dept, VCE
Read mode:
• The read mode ( r) opens an existing file for reading.
• When a file is opened in read mode, the file marker is positioned at the
beginning of the file (the first character).
• The file marker is a logical element in the file structure that keeps track of our
current position in the file.
• To open a file in read mode, the file must already exist. If it does not, NULL is
returned as an error.
Write mode:
• The write mode (w) opens a file for writing.
• If the file does not exist, it is created. If it already exists, it is opened and all its
data are deleted.
• It is an error to try to read from a file opened in write mode.
Append mode:
• The append mode (a) also opens an existing file for writing. Instead of creating
a new file, the writing starts after the last character i.e. new data are added at
the end of the file.
• If the file does not exist, it is created and opened. In this case, the writing will
start at the beginning of the file; the result will be logically the same as
opening a new file for writing.
M. Ramanjaneyulu, Assistant Professor, ECE
Dept, VCE
File close fclose() :
• When we no longer need a file, we should close it to free system
resources. A file is closed using the close function fclose () .
• Single character data can be written to a file from standard input stream
using the predefined function fputc or putc .
• Single character data can read from a file using a predefined function
fgetc or getc .
• Prototypes are
int fputc(int onechar, FILE *fp);
int fgetc(FILE *fp);
• In windows operating system the EOF character is represented by Ctrl + Z,
in Unix and MAC Operating systems Ctrl + D.
M. Ramanjaneyulu, Assistant Professor, ECE
Dept, VCE
C Program to create a new text file named as [Link], write some data to
the file from standard input (keyboard), and display the same on the screen.
#include<stdio.h> while(ch!=EOF)
#include<stdlib.h> {
int main(void) fputc(ch,fp);
{ ch = getchar();
FILE *fp; }
char ch; fclose(fp);
fp = fopen("[Link]","w"); fp = fopen("[Link]","r");
if(fp==NULL) printf("\nThe data in file is:");
{ ch = fgetc(fp);
printf("\nUnable to open file"); while(ch!=EOF)
exit(0); {
} putchar(ch);
printf("\nEnter the text:"); ch = fgetc(fp);
ch= getchar(); }
fclose(fp);
return 0;
}
M. Ramanjaneyulu, Assistant Professor, ECE
Dept, VCE
C program to copy the content of file [Link] to a new file [Link]
#include<stdio.h> fclose(fp2);
int main(void) fp2 = fopen("[Link]","r");
{ printf("\nInformation copied :");
FILE *fp1, *fp2; ch = fgetc(fp2);
char ch; while(ch!=EOF)
fp1 = fopen("[Link]","r"); {
fp2 = fopen("[Link]", "w"); putchar(ch);
ch = fgetc(fp1); ch = fgetc(fp2);
while(ch!=EOF) }
{ fclose(fp2);
fputc(ch, fp2); return 0;
ch = fgetc(fp1); }
}
fputc(EOF, fp2);
fclose(fp1);
M. Ramanjaneyulu, Assistant Professor, ECE
Dept, VCE