Wednesday, 23 March 2022
Tuesday, 22 March 2022
Friday, 11 March 2022
75.Input and output of c language : fgets(), fseek()
fgets():
It is the function used to read a line of text from a file.
It return the string fetched from the file.
It returns NULL on end of file.
Function definition:
char*
fgets(char *s, int n, FILE *p)
{
---------------------------
---------------------------
---------------------------
}
Specification10:
Read the names of fruits from the file "fruits"
and print onto the monitor.
Program:
#include<stdio.h>
#include<string.h>
void main()
{
FILE *p;
char x[40];
clrscr();
p=fopen("fruits","r");
printf("The
names of fruits:\n");
while(1)
{
if(fgets(x,40,p)==NULL)
break;
printf("%s",x);
}
fclose(p);
getch();
}
Execution:
The names of fruits:
apple
grapes
jack fruit
Example explained:
The file "fruits" is opened in read mode.
The strings are fetched from the file one after another
using fgets() and printing on to the monitor using printf().
fgets() returns NULL when end of file is reached.
Finally file is closed using fclose()
Random access/Direct access files:
In general we access the data from a file character after
character or record after record called sequential access.
Accessing a record from the required position directly is
called random accessing.
These files must be of binary type.
fseek():
It is the function used to send the file pointer to the
specified location from the specified position.
It accepts three arguments those are
- File
pointer
- "offset"
specifies the number of bytes to be skipped to locate a record
- "Whence"
specifies from where "offset" is measured.
"whence" must be 0 if we want to specify the
beginning of file (BOF)
It returns zero on successfully reaching of required
position otherwise returns a non-zero
Function definition:
int
fseek(FILE *p, long offset, int whence)
{
-----------------------------------
-----------------------------------
-----------------------------------
}
Example:

Specification11:
Accept "n" records from the keyboard, write onto
the file "employee" and display the required record using direct or
random access.
Program:
#include<stdio.h>
struct emp
{
int empno;
char ename[20];
int sal;
long int phno;
};
void main()
{
struct emp x;
FILE *p;
int n,i,t,rec;
char choice[1];
clrscr();
p=fopen("employee","wb+"); /* opening the file in binary
write/read mode*/
printf("How
many records?");
scanf("%d",&n);
printf("Enter
%d records:\n",n);
for(i=1;i<=n;i++)
{
printf("\nEmpno:");
scanf("%d",&x.empno);
printf("Name:");
scanf("%s",x.ename);
printf("Salary:"); /* Accepting
the data from keyboard*/
scanf("%d",&x.sal);
printf("Phone
No:");
scanf("%ld",&x.phno);
fwrite(&x,sizeof(x),1,p); /* Writing objects onto the
file*/
}
printf("Press
any key to continue....");
getch(); /* To wait for user confirmation*/
clrscr();
/* Clearing the console*/
while(1)
{
printf("Enter
the record number:");
scanf("%d",&rec); /* Accepting
record number */
if(rec<1||rec>n) /* Checking
for validity of record number*/
printf("Search failure...");
else
{
fseek(p,(rec-1)*sizeof(x),0);
/* placing record pointer to the required record*/
fread(&x,sizeof(x),1,p); /* Reading the record*/
printf("Empno:%d\nName:%s\nSalary:%d\nPhno:%ld"
,x.empno,x.ename,x.sal,x.phno); /* Printing the record*/
}
printf("\nWant
to continue...y/n:"); /*
accepting confirmation to continue*/
scanf("%s",choice);
if(strcmp(choice,"n")==0)
break;
}
fclose(p);
getch();
}
Execution:
How many records?3
Enter 3 records:
Empno:1000
Name:Black
Salary:2700
Phone No:56789
Empno:1001
Name:Smith
Salary:3650
Phone No:56788
Empno:1002
Name:Rajesh
Salary:5400
Phone No:98765
Press any key to continue....
(clears the screen)
Enter the record number:0
Search failure...
Want to continue...y/n:y
Enter the record number:2
Empno:1001
Name:Smith
Salary:3650
Phno:56788
Want to continue...y/n:y
Enter the record number:1
Empno:1000
Name:Black
Salary:2700
Phno:56789
Want to continue...y/n:y
Enter the record number:6
Search failure...
Want to continue...y/n:n
Executable file:
The program written in C (any) language is called the source
code.
Compiler translates the source code into executable code.
Executable file is saved with the name of program and with
.exe extension
Executable code is the machine code which can be directly
executed from the command prompt.
Example:
c:\turboc2\sum.c
--------------------

Compile the program by pressing ctrl+F9 which produces the
executable file with in the same directory

Executing the program from the command prompt


Command line arguments:
main() function is the mandatory and user defined function.
Execution of any application starts from the main()
The main() function accepts arguments from the command line.
When we send arguments through the command line, the
operating system converts the arguments as an array of strings and sends the
count of arguments, the reference of array of strings as arguments to the
main().
Here the first argument is the name of executable file.
By using the reference of array of strings we can access the
command line arguments from the main().
Example:

Example explained:
- Sending
command line arguments to the program
- The Os
automatically creates an array of strings with command line arguments.
- Number
of argument and the reference of array of strings are sending as arguments
to the main
- main
is accessing the command line arguments.
Program:
step1: type the program and save with the name
"demo.c"

step2: Compile the program by using F9

step3: Go to the command prompt (OS shell)
step4: Send command line arguments

74.Input and output of c language : Binary file, fread(), fputs()
Binary files:
Text file is a file where the data of any type is converted
into text and stored.
For example the salary 7560 will be converted into
individual characters and stored as individual characters "7", "5",
"'6" and "0".
When we read into the program then the text type is again
converted into number type.
It takes more memory (7560 takes 2 bytes where as
"7560" takes 4 bytes because every character takes 1 byte)
It shows less performance in writing and reading from the
file (Because data must be converted while writing and reading from the file)

We can save the memory and improve the performance by
writing the binary data directly on to the file.
The file with binary data is called a binary file.
Binary files can't open with any other applications.
fwrite():
It is a function used to write an object (structure
variable) on to the file in a binary format
Writes "n" items of "size" bytes each
through the file pointer
It accepts the address of object, size of object, number
object to be written and the file pointer as arguments.
Returns the number of actually written items (not bytes)
Function definition:
int
fwrite(void *ptr, int size, int n, FILE *fp);
{
-------------------------------------
-------------------------------------
-------------------------------------
}
Example:

Example explained:
- Creating
the file "data1" in binary write mode.
- fwrite()
is used to write the object on to the file in a binary format. Here the
address of object (6332), size of object (44), number of objects (1) and
the file pointer are sending as arguments.
fread():
It is the function used to read the binary data from the
file and store into the object
Reads "n" items of "size" bytes each
through the file pointer
It accepts the address of object, size of object, number
object to be read and the file pointer as arguments.
Returns the number of items fetched from the file (not
bytes)
Function definition:
int fread(void *ptr, int size, int n, FILE *fp);
{
-------------------------------------
-------------------------------------
-------------------------------------
}
Example:

Program explained:
- Opening
the file "data1" in binary read mode
- fread()
is used to read the binary data from the file and write onto the object.
Here the address of object (6332), size of object (44), number of objects
(1) and the file pointer are sending as arguments.
Specification7:
Accept "n" addresses from the keyboard and write
onto the file "data1"
Program:
#include<stdio.h>
struct address
{
char name[20];
char place[20];
long int pin;
};
void main()
{
FILE *p;
struct address x;
int n,i;
clrscr();
p=fopen("data1","wb");
printf("How
many records?");
scanf("%d",&n);
printf("Enter
%d records:\n",n);
for(i=1;i<=n;i++)
{
printf("Name:");
scanf("%s",x.name);
printf("Place:");
scanf("%s",x.place);
printf("Pin
code:");
scanf("%ld",&x.pin);
fwrite(&x,sizeof(x),1,p);
}
fclose(p);
printf("1 file
created");
getch();
}
Execution:
How many records?3
Enter 3 records:
Name:Smith
Place:Kavali
Pin code:524201
Name:John
Place:RudraKota
Pin code:524201
Name:Black
Place:BudamGunta
Pin code:425201
1 file created
Specification8:
Read the data from file "data1" and display onto
the monitor.
Program:
#include<stdio.h>
struct address
{
char name[20];
char place[20];
long int pin;
};
void main()
{
FILE *p;
struct address x;
clrscr();
p=fopen("data1","rb");
printf("Rcords
of file:\n");
while(1)
{
fread(&x,sizeof(x),1,p);
if(feof(p)!=0)
break;
printf("\n\nName:%s\nPlace:%s\nPin
code:%ld",x.name,x.place,x.pin);
}
fclose(p);
getch();
}
Execution:
Rcords of file:
Name:Smith
Place:Kavali
Pin code:524201
Name:John
Place:RudraKota
Pin code:524201
Name:Black
Place:BudamGunta
Pin code:425201
1 file created
fputs():
It is the function used to print a string onto the file.
It accepts both the string and the file pointer as
arguments.
It returns the last character written on to the file if
operation is successful otherwise returns -1
Function definition:
int
fputs(char *s, FILE *p)
{
-------------------------
-------------------------
-------------------------
}
Specification9:
Accept multiple lines of text and write onto the file
"fruits".
Program:
#include<stdio.h>
#include<string.h>
void main()
{
FILE *p;
char x[40];
clrscr();
p=fopen("fruits","w");
printf("Enter
the names of fruits:\n");
printf("exit
to close..\n");
while(1)
{
gets(x);
strcat(x,"\n");
if(strcmp(x,"exit")==0)
break;
fputs(x,p);
}
fclose(p);
printf("1 file
created");
getch();
}
Execution:
Enter the names of fruits:
exit to close..
apple
grapes
jackfruit
exit
Note: Here "exit" is used as terminating word but
not written on the file.
Program explained:
Opened the file "fruits" in write mode
gets() is used to read a line of text from the keyboard
Writing the strings on to the file using fputs() through the
file pointer
strsmp(x,"exit") is used to check for end of file
