Google Find us on Google+

Thursday, 25 April 2013

Input Output Operations getc() And putc() Of Files.

The standard input/output library provides similar routines for file input/output as well as for standard input/output.

getc() and putc()

The two functions getc() and putc() are the most basic functions of C. They are used to read the character and to display the character.

getc():

The function getc() reads a character from a file referenced by file pointer opened in read mode using fopen(). The syntax of getc() is: int getc(File's pointer); 
Example: int getc(fp); 
getc() returns the next character on the given input stream and increments the stream's file pointer to the next character. It reaches EOF marker after reaching the end of  file.

putc():

The function putc() writes a character to the file which is opened by fopen() in write mode. 
The syntax of putc() is: int putc(int c,fp);
putc() outputs the character given by 'c' to the file through the pointer 'fp'. It returns the character 'c' on success, that is, when a character is written to that particular which we have been opened. On error, the pointer will points towards end of the file(EOF).

The character input/output operations like getchar(), and putchar() will also work as similar to getc(fp), and putc(c,fp) 

Example Program:
A program code using getc(), and putc() operations. 
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
void main()
{
FILE *fp;
int c;
fp=fopen("cse.txt","r");  // opens file 'cse' in read mode.
if(fp==NULL)
{
printf("cannot open the file");
exit(0);  //terminates the program.
}
 c=getc(fp);  // reads a character and fp points to the next character.
while(c!=EOF) // checks whether end of file reaches are not; if not the below code will executes.
{
putchar(c); // prints a character 'c' on the screen.
c=getc(fp); // reads next character.
}
fclose(fp); //closes the file which is referenced by the pointer fp.
getch();

Explanation:
In this program, the file named cse.txt is opened in a read mode. Its contents are read character by character using getc(), and then displayed on the screen. When the pointer reaches to EOF, then the while loop terminates and file will be closed using the function called fclose().


From site : http://ctechrockz.blogspot.com/

Sunday, 21 April 2013

Opening And Closing A Data File.

Files are not only used for data, but our programs are also stored in files. To work with a file, a program has to go through the following steps:
  1. Open a file for reading or writing.
  2. Do operation read or write to file using library functions.
  3. Close the file may be used by some other program.
The difference between working with file or with standard input/output terminals is that we must specify the location of file to work within our program.

Opening a File:

Specifying the file you wish to use is referred to as opening the file. When working with a stream oriented data file, the first step is to establish a buffer area,where information is temporarily stored while being transferred between the computer's memory and the data file. The buffer area allows information to be read from or written to the data file more rapidly than would other wise be possible.

Buffered input/output:

A buffer is a region of memory used to temporarily hold data while it is being moved from one place to another. Typically the data is stored in buffer(i.e.memory location) as it is retrieved from an input device (such as a keyboard) or just before it is sent to an output device (such as monitor). 
The buffer area is used using the pointer to FILE declared as:
FILE *ptvar;
where,
FILE (only uppercase letters should be required) is a special structure type that establishes the buffer area, and ptvar is a pointer variable that indicates the beginning of the buffer area(courser). It is often referred to as stream pointer, or simply a stream. The structure type FILE is defined within a system include file, typically stdio.h.

A data file must be opened before it can be created or processed. A file is opened by call to function fopen() which is available in library stdio.h.
The syntax of function fopen() is
ptvar=fopen(file name, file type);
where file name and file type are the stings that represent the name of the data file and the manner in which the data file will be utilised. It means that the file type is just a mode which specifies the purpose of opening a file; reading a file, or writing a file, or appending text at the end of the file(i.e.continuation of writing at the end of the file), etc.
The name chosen for the file must be consistent with the rules for naming files, as determined by the computer's operating system.
The function returns a pointer to the object which controls the stream associated with the file, that is, the pointer to the beginning of buffer area associated with the file(if the file is opened successfully), otherwise it returns a NULL value which is defined in stdio.h.
For example:
#include<stdio.h>
void main()
{
FILE *fp;
fp=fopen("cse.txt","r");
}
The above program will open a file "cse.txt" for reading. If no file exists with this name in the current directory, then the function will return NULL value, indicating no valid buffer is associated within the file.
Considering another example:
#include<stdio.h>
void main()
{
FILE *fp;
fp=fopen("cse.txt","w");
}
The above code will open a file named "cse.txt" for writing. If the file does not exists within the given path, then the function will create a new file and opens it to write. If the file already exists, then it will be opened in write mode but the previous contents will be deleted.
Every file has its own pointer variable. When we wish to read from a file or write to a file, we specify the file pointer variables as FILE *fp1, *fp2, *fp3;
The variables fp1,fp2, and fp3 are file pointers.
Example for a NULL pointer:
#include<stdio.h>
#include<conio.h> 
#include<stdlib.h>   //prototype for exit function
void main()
{
FILE *fp;
fp=fopen("cse.txt","w");
if(fp==NULL)
{
printf("cannot open the file");
exit(0);  //terminates the program.
}
 //here we can work on file(i.e. write to a file)..
}
If fopen() returns NULL, we can check whether the file is opened or not and accordingly print the message in to the file.
We can also combine the fopen() and if statements as follows:
if((fp=fopen("cse.txt","w"))==NULL)
printf("cannot open the file");
exit(0); //terminates the program.

Closing a File:

When all read, write, and other operations are done with an opened file, it must be closed immediately for the following reasons:
  • To reopen it in some other mode.
  • To prevent any misuse with the file.
  • To make the operations you have performed on the files successful.
To close a file, we must use the function named fclose().
The syntax of fclose() is as follows:
fclose(ptvar)
where ptvar is a pointer variable(fp).
The function fclose() closes the named stream. Pointer variable points to a file to be closed. All buffer associated with the stream are flushed before closing the file. On success, fclose() returns 0 otherwise, it returns end-of-function.
Example program:
A program code which demonstrates the functions fopen() and fclose().


#include<stdio.h>
#include<conio.h> 
#include<stdlib.h>   //prototype for exit function.
void main()
{
FILE *fp;
clrscr();
fp=fopen("cse.txt","w");
if(fp==NULL)
{
printf("cannot open the file");
exit(0); //terminates the program.
else
{
printf("File is opened successfully");
printf("\n Now you can work on your file in write mode");
}
if(fclose(fp)==0)
{
printf("\n File is closed now. cannot work on file");
}
getch();
}

Output for the above given program:
File is opened successfully
Now you can work on your file in write mode
File is closed now. cannot work on file

How the above program works?
In the above code, a file named "cse.txt" is opened. The if condition checks whether file is opened or not. If the file is not opened, it prints the message "cannot open the file" and terminates the program. If the file is opened, it prints the corresponding message. The condition fclose(fp)==0 checks whether the file pointed to by fp is closed successfully or not.
  •  Note: A file pointer is simply a variable like an integer or a character. It does not point to a file or the data in a file; it is simply used to indicate which file refers to the input/output operations.
From site : http://ctechrockz.blogspot.com/

Saturday, 20 April 2013

Introduction To File Handling.

  •  We normally often need to store data through our programs on some permanent storage devices so that we are able to access the data some other time. Such a data is stored on the memory device in the form of a data file. Thus,data files allow us to store information permanently, and to access and alter that information whenever necessary. The C library provides set of library functions to perform read and write operations on files. A computer file is a collection of data stored on a non volatile device in a computer system. Files exists on permanent storage devices, such as hard disks, DVDs, USB drives, and reels of magnetic tape. 
  • There are two different  types of data files, called stream oriented (or) standard data files and the system oriented (or) low level data files. 
  •  Stream oriented data files are generally easier to work with and are therefore more commonly used. These files are sub divided into two categories: The first category is of text files, that which consists of consecutive characters. These characters can be interpreted as strings or numbers or-else individual data items. The manner in which these characters  are interpreted will use library functions such as scanf and printf statements etc. The second is often referred to as unformulated data files. They organizes data into blocks containing contiguous bytes of information. These blocks represent more complex data structures, such as arrays and structures. A separate set of library functions are available for processing stream oriented data files of this type.
  •  System oriented data files are more closely related to the computer's operating system than stream oriented. They are somewhat more complicated to work with, though their use may be more efficient for certain kind of applications.
  •  Data files are most frequently used to store user's data. However data related to numbers can be efficiently stored in the form of binary files, which are also useful to handle file containing machine language contents. The files which are stored in binary format differ significantly from data(i.e.text files).
  • Text Files;which contain data that can be read in a text editor because the data has been encoded using a scheme such as ASCII or uni-code. Text files might include facts and figures used by business programs, such as a payroll file that contains employee numbers, names, and salaries.
  • Binary file;which contain data that has not been encoded as text. It mean that the binary file always contains binary digits. Examples include images and music.
         Although their contents vary, files have many common characteristics, as follows:
  • A byte is a small unit of storage; for example, in a simple text file, a byte holds only one character. Because a byte is so small. File sizes usually expressed in kilo bytes(thousands of bytes), mega bytes(millions of bytes), giga bytes(billions of bytes).
      The C language treats all the information that enters a program or leaves a program as a stream of bytes or series of bytes. We can work on files by using input or output streams.

For a quick summery, visit  the below given link.

http://ctechrockz.blogspot.in/2013/04/an-overview-of-files.html

From site : http://ctechrockz.blogspot.com/

An Overview Of Files.

What is a File?

  • A File is a collection of related records such as the records of employee in a company. Each record will be a collection of related data items called fields namely, employer name, number, department etc..Each field consists of group of characters. Many computer applications require that information must be written or read from an auxiliary device such as hard disk or floppy disk. Such a information  will be stored in the storage devices in the form of data files.
  • Data files allow us to store information permanently, and access or alter that information whenever required.

 How a File will be useful?

          There can be many reasons to use files.

    • The first and the foremost reason is need of permanency of storage of data. If we want to preserve the output of any program for future use, it is not possible with out files. All the inputting files have been printed with the help of any output statements like printf, putchar etc..are never available for future use and the only solution in that case is to write the output in files.Similarly, if there is a large amount of data generated as output by a program, storing that output in file will help in easy analysis of the output,as user can see the whole output at any time even after complete execution of the program. These situations will explain the need of output files. 
    • The input files are also equally important and useful. If a program needs a lot of data to be inputted, user cannot keep on typing that again again for repeated execution of the program. In that case, all input data can be once written in a file and then that file can be easily used as the input file. Moreover the transfer of input data and/or the output data from one computer to another can be easily done by using files.
           From site : http://ctechrockz.blogspot.com/

    Wednesday, 17 April 2013

    A Basic Introduction To Programming Languages.

    Programming Languages:

    A Programming Language  is a language that can be used to write computer programs which controls the functionality or behavior of a computer. A Programming Language is not a spoken language. It is a way of describing what the programmer wants the computer to do. In fact every programming language is defined by the syntactic and semantic rules which describes the whole language. Thousands of different languages are created so far and many new languages are created every year. Suppose you want to solve an equation but  you don't know the steps included in it, in this case you will find very difficult to solve such a problem. Similarly if you want your computer to solve that problem, your computer must know the steps involving to solve it. These steps are provided by you as instructions to the computer as programs written in a particular programming languages. A computer program is also called as a piece of code or source code and the actual writing of source code is called coding. 
    Every Programming Language has rules governing its word usage and punctuation. These rules are called the language's syntax. Unless the syntax is correct, the computer cannot interpret the programming language instructions at all. 
    When you write a program, you usually type its instructions using a keyboard. When you type the program instructions, they are stored in computer's memory, which is a temporarily internal storage device. It is known-ed to be volatile, since its contents are lost when the computer is turned off or when the power is loosed. Such that you need to store them on a permanent storage devices like hard disks. These are known-ed to be non-volatile, since their contents are persistent and are retained when the power is lost. After storing a program, it must be translated from your programming language statement to machine language statement.


    From site : http://ctechrockz.blogspot.com/ 

    Introduction To Computer Systems.

     A Basic Introduction To Computer Systems:

    Welcome to computer science. You are in a world which offers many challenges and exciting careers.
     Here, we introduce you to the concepts which are dealing with computer science and especially about programming, coding, and the languages which are understood by the computers. A computer system is a combination of all the components required  to process and store data using computer. Each and every system is composed with multiple pieces of Hardware and Software.

    Hardware:

    Hardware is a physical device which is associated with a computer. Devices like keyboards, mice, speakers, and printers are all hardware devices. The hardware components of the computer systems are divided in to three parts:
    1.Input devices
    2.Storage devices 
    3.Output devices
    Keyboard is considered to be as an input device.
    Storage device is of Central Processing Unit(CPU).
    Primary storage devices are like pen drives,disks,and taps etc.
    Monitors and Printers are of output devices.

    Keyboard:

    Keyboard is a device in which programs and data to be entered into the computer. Other examples of input devices include a touch screen, stylus or a pen, an audio input unit, and a writing note pad.

    CPU:

    Central processing unit is responsible for executing arithmetic calculations. Primary storage device is considered to be as main memory, since it is a place in which programmers can store the programs and data temporarily during processing. It can be erased if we turn off the system. Since it is stored in RAM(Random Accessing Memory). Auxiliary storage device is considered as a secondary storage device, which is used for both input and output. It is a place where the programs are stored permanently. When we turn off the computer, our programs and data will be stored in secondary storage devices like hard disks, and pen drives.

    Monitors and Printers:

    Output devices are usually monitors and printers that which shows the output. The shown output or the displayed output on the screen of the monitor will be considered to be as a soft copy. If we prints on a printer, then the output printed copies are of hard copies.

    Software:

    Software is computer's instructions that tells the hardware what to do. Simply we can say that the software is a programs instructions written by a programmer. There can be a plenty of p rewritten programs which are stored in a disk. Also it can be available to buy by the users and it can be downloaded from the WEB.
    Side by side we can also write our own programs, which is known-ed to be as coding the program or programming. This site mainly focuses on the coding the programs or the programming process and the logic which are used to write a program.
    Software can be classified in to two categories:
    The system software, and the Application software.

    System software:

    System software comprises the programs that you use to manage your system or a computer, including operating systems such as Windows, LINUX, or UNIX.

    Software applications :

    Software applications comprises all the programs you apply to a task. Which are of like word processing programs, spread sheets, payroll, and inventory programs, and also even games.

    From site : http://ctechrockz.blogspot.com/