PPS V Unit
PPS V Unit
STREAMS:
Standard error: It is connected to the screen & all error messages are
output to standard error.
Binary files contain fixed length records, They are in binary format
(machine readable) not a human readable format like text files, Binary
files can be accessed directly (i.e. random access). The record structure
for a binary file is created using structures. They are more efficient than
text files as conversion from ASCII to binary (reading) and vice versa for
writing does not have to occur. They cannot be read easily by other non-C
programs. Binary files are appropriate for online transaction processing
systems.
Ex: Airline reservation, Order processing, banking systems.
fwrite function:
SYNTAX:
size_t fwrite (void *ptr, size_t size, size_t n, FILE *fptr);
fwrite writes from array ptr, n objects of size, size to file pointed to
by fptr
fwrite returns the number of objects written
which is less than n if an error occurs
SYNTAX:
For formatted input and output to files we use fprintf and fscanf
functions. These functions are the same as printf and scanf except that
the output is directed to the stream accessed by the file pointer, fptr.
fprintf :
fprintf is used to write a record to the file.
fscanf :
fscanf is used toRead a record from the file.
UPDATING RECORDS:
Records cannot be updated, to update a text file the entire file is
rewritten.
fgetc( ): This function is used to read the next character from the stream.
This is similar to getchar().
Synta : int fgetc (FILE *stream)
It returns the character (as an integer) or EOF if end of file or an error
occurs.
fputc( ): This function is used to write the character c to the stream. This
is similar to putchar (int).
Syntax: int fputc (int c, FILE *stream)
It returns the character or EOF for error.
fgets( ): This function Reads at most the next n-1 characters from the
stream into the array s. Reading stops after an EOF or a newline. If a
newline is read, it is stored into the buffer. A '\0' is stored after the last
character in the buffer. This is similar to gets(char* ).
Syntax : char* fgets (char *s,int n,FILE *stream)
It returns s or NULL if EOF or error occurs
ftell() function returns the current file position of the specified stream.
ftell() function is used to get the total size of a file after moving file pointer
at the end of file. We can use SEEK_END constant to move the file pointer
at the end of file.
rewind( ) :The function rewind resets the file pointer back to the start of
file.
2. Opening a File:
A File must be opened before it can be created or processed. This
association the file name with the buffer area. It also specifies how the file
will be utilized i.e. read-only file, write-only file, or read/write file.
We use the library function fopen() for opening a file. ptrvar =
fopen(filename, mode)
where filename and mode are strings. Filename represent the name
of the file, mode represents how the file opened i.e., read only, write only
etc.
The fopen() function returns a pointer to the begins of the buffer
area associated with the file. A NULL value is returned if the file cannot be
created or when existing file cannot be found.
The different file opening modes area listed below.
Mode Meaning
“r” Open an existing file for reading only
“w” Open a new file for writing only. If the file exists, its
contents are over written.
“a” Open an existing file for appending (i.e., for adding
new information at the end of the file). A new file will
be created if the file does not exist.
“r+” Open an existing file for both reading and writing.
“w+” Open a new file for both reading and writing. If the
file exists, the contents are over written
“a+” Open an existing file for both reading and appending.
A new file will be created if the file does not exists
To open the file in Binary file mention the mode along with a extra letter
‘b’ as “rb”,
“wb”, “rb+” and so on.
After opening the file we can read the characters from a file or write the
characters into the file by using the two library functions fgetc() and
fputc().
fgetc() reach a character from the current position and advances the
pointer position to the next character, and returns the character that is
read.
Eg:- ch = fgetc(ptrvar)
fscanf( ) and fprintf( ) are two functions used for formatted reading and
writing of characters, Strings, integers and floats.
fprintf(FILE *fp,"format-string",var-list);
fscanf(FILE *fp,"format-string",var-list);
Finally, the file must be closed at the end of the program. This can
be done by using the function fclose().
Syntax: fclose(ptrvar);
It is good programming practice to close a file explicitly, even
though C compiler will automatically close the file.
#include<stdio.h>
main()
{
FILE fs,ft; char ch;
fs = fopen(“source”,’r’);
if(fs = =NULL)
{
puts(“Cannot open source file”);
exist();
}
fr = fopen(“target”,’w’);
if(ft = =NULL)
{
puts(“Cannot open target file”);
fclose(fs);
exist();
}
while((ch=fgetc(fs))!=EOF)
putc(ch,ft);
printf(“File Copied”);
fclose(fs);
fclose(ft);
}
/* Program to illustrate the use of fprintf() */
#include<stdio.h>
main()
{
FILE *fp;
char itemname[20]; int qty;
float rate;
char choice = ‘y’;
fp = fopen (“Item”,”w”);
while (choice = = ‘y’)
{
printf(“Enter Item Name, Quantity and Rate \n”); scanf(“%s%d
%f”,itemname,&qty,&rate);
fprintf(fp, “%s%d%f\n”,itemname, age,rate);
printf(“Do you want to Continue (y/n)”);
fflush(stdin);
choice = getch();
}
fclose(fp);
}
/* Program to illustrate the use of fscanf() */
#include<stdio.h>
main()
{
FILE *fp;
char itemname[20];
int qty;
float rate;
fp = fopen (“Item”,”r”);
while (fscanf(fp,“%s%d%f”,itemname,&qty,&rate)!=EOF)
{
printf(“Item Name : %s\n”,itemname);
printf(“Quantity =%d\n”,qty);
printf(“Rate = %f\n”,rate);
}
fclose(fp);
}
#include<stdio.h>
main()
{
FILE *fp;
char choice = ‘y’;
struct emp
{
char name[20];
int age;
float bs;
};
struct emp e;
fp = fopen(“Employee.dat”, “wb”);
if(fp = = NULL)
{
puts(“Cannot open file”);
exit();
}
while (choice = =’y’)
{
printf(“Enter Name, age and Basic Salary”);
scanf(“%s%d%f”, e.name, &e.age, &e.bs);
fwrite(&e,sizeof(e),1,fp);
printf(“Do you want to add one more record (y/n)”);
fflush(stdin);
choice =getch();
}
fclose(fp);
}
#include<stdio.h>
main()
{
FILE *fp;
struct emp
{
char name[20];
int age;
float bs;
};
struct emp e;
fp = fopen(“Employee.dat”, “rb”);
if(fp = = NULL)
{
puts(“Cannot open file”);
exit();
}
while (fread(&e,sizeof(e),1,fp)
{
printf(“\n Employee Name : %s\n”,e.name);
printf(“\n Employee Age:%d”,e.age);
printf(“\nBasic Salary = %f”,e.bs);
}
fclose(fp);
}
/* Program for copying one file to another file using command line
arguments */
#include<stdio.h>
main(int argc, char *argv[])
{
FILE *fs, *ft;
Char ch;
if (argc!=3)
{
puts(“In sufficient arguments”);
exit();
}
fs = fopen(argv[1], “r”);
if (fs= =NULL)
{
puts(“Cannot open source file”);
exit();
}
ft = fopen(argv[2],”w”);
if (ft = =NULL)
{
puts(“Cannot open target file”);
fclose(fs);
exit();
}
while ((ch=fgetc(fs))!=EOF)
fputc(ch,ft);
fclose(fs);
fclose(ft);
}