file input output operations
file input output operations
Programming Fundamentals
Programming Fundamentals
BS(IT) 1st semester
Stream
A logical interface to a file is known as stream. It is a flow of data. A sequence a characters
from an input device to computer is called input stream. A sequence of character from computer
to output device is called output stream. A stream is associated with a fil using an open operation.
The stream is disassociated from a file using a close operation.
Types of Streams
There are two types of streams in C language:
1. Text Stream
A text stream is a sequence of characters, A certain character translation may occur in text
stream. For example, a new line may be converted to a carriage return/line feed pair, means that
there may not be a one-to-one relationship between the written characters and the characters in
external device.
2. Binary Stream
A binary stream is a sequence of bytes. The translation is not performed in binar stream. It
exists with a one-to-one correspondence to the external devices. It means that th number of bytes
written or read is the same as the number of bytes on the external device However, an
implementation-defined number of bytes may be appended to a binary stream
BS(INFORMATION TECHNOLOGY). Programming Fundamentals
The above loop will continues to execute until it finds the EOF character
New Line
The ENTER key is used to move the cursor to the next line in a text editors such as Notepad. A
newline character is placed at the end of each line when the user presses Enter key. The newline
is denoted by \n in C.
Data file
A data file is a collection of related records. A record contains data about an entity. Data file can
be used to save any type of data. Files are stored on secondary storage. The data is stored in files
permanently.
Normally, a program inputs data from user and stores it in variables. The data stored in the
variables is temporary. When the program ends, all data stored in variables is also destroyed. The
user has to input data again from keyboard when the program is executed next time. The data has
to be entered each time program is executed that wastes time. The user may also type wrong data
at different times.
A data file can be used to provide input to a program. output of a program permanently. If a
program gets input from a file instead of keyboard, it will get the same data each time it is executed.
There will be far less chance of errors. can also be used to store the
Text Files
A type of file that stores data as readable and printable characters is called text file. A source
program of C language is an example of text file. The user can easily view and read the contents
of a text file. It can also be printed to get a hard copy.
Pointer
A type of variable that is used to store the memory address of a memory cell is known as pointer.
It normally stores the memory address of a variable or object. The data type of a pointer must be
same as data type of the variable whose memory address is stored in pointer.
File Pointer
File pointer is pointer that refers to file on the secondary storage. It is variable of FILE that is
defined in stdio.h. It used to access and manipulate data file. program to declare file pointer to use
file. The file pointer is associated with file after declaration. One file pointer can be associated
with only one data file.
syntax
The syntax for declaring file pointer as follows:
FILE "MyFile;
The identifier MyFile is file pointer. FILE the type of MyFile pointer. The symbol indicates that it
is pointer to file structure. This structure defined in stdio.h header file. contains all information
that required to manage files. must be included in the program to manipulate files.
File Operations
▸ Writing to a file
Opening a file
A file should be opened before can processed. All standard file handling functions C declared in
stdio.h. must be included almost every program. file pointer declared and associated with the file
to opened. function fopen used open file.
Syntax
The syntax of fopen function is follows:
File Pointer= fopen(File Name, Mode):
Mode “w”: It is a write only mode. The fopen() function creates a new file when the specified file
doesn’t exist and if it fails to open file then it returns NULL.
Mode “a”: Using this mode Content can be appended at the end of an existing file. Like Mode
“w”, fopen() creates a new file if it file doesn’t exist. On unsuccessful open it returns NULL.File
Pointer points to: last character of the file.
Mode “r+”: This mode is same as mode “r”; however you can perform various operations on the
file opened in this mode. You are allowed to read, write and modify the content of file opened in
“r+” mode.
File Pointer points to: First character of the file.
Mode “w+”: Same as mode “w” apart from operations, which can be performed; the file can be
read, write and modified in this mode.
Mode “a+”: Same as mode “a”; you can read and append the data in the file, however content
modification is not allowed in this mode.
BS(INFORMATION TECHNOLOGY). Programming Fundamentals
Reading a File
To read the file, we must open it first using any of the mode, for example if you only want to read
the file then open it in “r” mode. Based on the mode selected during file opening, we are allowed
to perform certain operations on the file.
#include <stdio.h>
int main()
{
/* Pointer to the file */
FILE *fp1;
/* Character variable to read the content of file */
char c;
Writing to a file
To write the file, we must open the file in a mode that supports writing. For example, if you open
a file in “r” mode, you won’t be able to write the file as “r” is read only mode that only allows
reading.
BS(INFORMATION TECHNOLOGY). Programming Fundamentals
#include <stdio.h>
int main()
{
char ch;
FILE *fpw;
fpw = fopen("C:\\newfile.txt","w");
if(fpw == NULL)
{
printf("Error");
exit(1);
}
return 0;
}
Closing a file
fclose(fp);
The fclose( ) function is used for closing an opened file. As an argument you must provide a
pointer to the file that you want to close.
return 0;
}
Why I used if(fgets(str, 10, fpr)==NULL as a logic to determine end of the file?
In the above examples, we have used ch==EOF to get to know the end of the file. Here we have
used this logic because fgets returns NULL when there is no more records are available to be read.
#include <stdio.h>
int main()
{
FILE *fpw;
fputs(str, fpw)
str – str represents the array, in which string is stored.
fpw – FILE pointer to the output file, in which record needs to be written.
fputs("\n", fpw);