Welcome !!

NICETECHVIDYA

Tutorials

Web Designs

You are welcome to Aptech Designers, designed by Pratik from Nepal.

Creativity is my passion. Logo describes your details about company.

Graphics Design needs vision to create yours unique digital concept.

Responsive and All Browser Compatible Dynamic Web Layouts our target.

Home Python C Language J A V A Our Courses
This WebSite is under Construction....

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

Goa

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

  1. File pointer
  2. "offset" specifies the number of bytes to be skipped to locate a record
  3. "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:

  1. Sending command line arguments to the program
  2. The Os automatically creates an array of strings with command line arguments.
  3. Number of argument and the reference of array of strings are sending as arguments to the main
  4. 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

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


0 comments:

Post a Comment