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....

Wednesday, 23 March 2022

Demo post


Tuesday, 22 March 2022

Need of language (Computer oriented):

What is computer?
Computer is a programmable machine made with number of electrical, electronic and mechanical parts.
Computer is used to perform procedural arithmetic, relational and logical operations.
Computer is also used to control other machines and to store the data permanently for future usage.

The language of computers:
The field of computer sciences, which deals with the physical parts of computer, is called hardware. 
The natural language of computer hardware is the machine language (Binary code), which is difficult to learn and practice. 









Language:
The language is an interface among the programmer and computer hardware. 
Because machine language is difficult to learn and practice, there are number of other languages available to program the computer.
Programmers develop the software using any computer oriented language.
Where as users operate the computer using software.
 
Software:
Set of instructions developed in any computer oriented language is called a program.
Set of programs developed to solve any problem is called software.













Classification of languages:

Language is an interface among the programmer and computer hardware.
According the program development and execution efficiency languages are classified into low-level, high-level and middle level languages.

Low-level languages:
These are the languages developed and used before 60s.
These are the machine oriented languages which are difficult to learn and practice.
Prior knowledge in computer organization (technical knowledge) is needed to learn these languages.
These languages have low program development efficiency and high program execution efficiency (difficult to develop but effective in execution).
Examples:
Machine language
Assembly language (ALP).

High-level languages:
These are the programmer friendly languages. 
Simple English words, operators and expressions made the language easy to learn and practice.  
Any body from any field can learn these languages to develop software according to their requirement.
These languages have high program development efficiency and low program execution efficiency (Easy to develop but slow in execution).
Examples:
BASIC (Beginners All Purpose Symbolic Instruction Code). 
-Used to develop small applications.
COBOL (Common Business Oriented Language)
-Used to develop business applications
Pascal
-Used to develop scientific applications.
FORTRAN (Formula Translation)
Middle level languages:
According to the program development and execution efficiency, the performance of these languages is existed in between both the low-level and high-level languages.
Examples:
C, C++, Java etc.

 




Need of translator:

Natural language of computer hardware is the machine language. 
Computer can’t directly understand the source code written any high-level and middle-level languages. 
We need a translator to translate the source code into executable code. 
According to the functional nature these are of two types.
Interpreter
Compiler

Interpreter:
It is a translator, translates the source code into executable code, statement after statement in a procedural order.
The executable code generated by the interpreter is executed by the computer hardware.
Interpreter accepts the data from either the user or from other source while executing the program.
Need both the source code and interpreter every time we execute the program. 








Compiler:
It is a translator; translates the total source code into executable code at once.
The executable code generated by the compiler is used as an application (software).
We no more require either the source code or compiler once the, compiler produces the executable code.
Most of the languages like C, C++, and Pascal etc. use compiler.

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

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


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:

  1. Creating the file "data1" in binary write mode.
  2. 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:

  1. Opening the file "data1" in binary read mode
  2. 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

Goa

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