-->

Tuesday, September 30, 2014

File Structure in C

The file meant for a C programmer is nothing but a data file. Using C programs the data in the form of character or characters is stored permanently on the secondary storage device. In file handling such programs are designed that store data in file and read back data from the file. It means the file replaces the input/output devices for I/O operations. Rather than displaying the data on the output device monitor, it can be written to the file or the data can be read from the file instead of the input device like keyboard. Contents of a file describe the structure of a file.

The way how the contents of a file are being managed or decided is called as file Structure. It may be character, fields, records or block of records.

Each and every data stored in a file can be treated simply as character. So, the character is simplest unit of transfer. In its simplest structure a file can contain a character. More number of characters can be stored in file to make it meaningful. If the transfer rate is simply a character then there is lots of overhead. In order to overcome this overhead the characters can be grouped into many numbers of characters. A file with ‘character’ structure looks as follows:

POEM.TXT
Twinkle twinkle little star!
How I wonder what you are?
Up above the world so high!
Like a diamond in the sky.


In this case of the file ‘POEM.TXT’ each time a single character is written in the file. The file is simply a collection of cnaracters. So, the structure of the file is 'character'. [A text file]
The grouped characters can be transferred to or from the file. In such case a ‘field' can be treated as unit of transfer. The field here is nothing but a variable of any standard data type. So, the file can now contain these fields in its structure. The length of the field varies according to the contents in it. It is not fixed. So, the random access is not possible. The field values must be separated by a marker indicating end of the value. The file creator must take care in such cases.

A file with ‘field’ structure looks as follows:

STUDENT.DAT
101 jacob 102 jacob2
103 bill  104  bill2
105 jacob3 106  bill3


In this case of the file ‘STUDENT.DAT’ each time the data from variables is transferred. Two variables like 'rollno' and 'name’ are used in this case. The file is a collection of data in the form of characters only. But the structure of the file is ‘field’.

The fields that logically explain the characteristics of a person or a thing can be grouped in the form of 'records’ and  these records can be stored in the file. The file is now a collection of records.So, the unit of transfer in this case will be a record. A whole record can be transferred at a time to or from the file. So, in this case the structure of a file can have records in it. Generally here the file contains fixed length records. Number of bytes transferred is equal to the length of record. So, a record containing many fields is transferred, individual fields.

A file with ‘record’ structure looks as follows:

EMP.DAT

101   A    25400
10    B    3240
13    C    12500
104   D    5400


In this case of the file ‘EMP.DAT’ each time the data from a complete record is transferred. For the sake of understanding each record is written in new line. The record structure consists of three fields (like ‘empno’, ‘empname’ and ‘salary’) are used in this case. The file is a collection of data in the form of records only. But the length of the record is fixed. So, there is chance of wastage of storage space. But random access or direct access is the great utilization of fixed length ‘record structure’.

 Further the records can be grouped into ‘blocks’. A block may contain ‘n’ number of records. In such case the data transfer rate is in terms of ‘block’. A block can be transferred at a time. So, the structure of file is ‘block’ of records.

 In order to support such files the definition of the control structure for streams defined in C which as “FILE structure” is defined as follows:

typedef struct {

int level;     /* Full or empty level of buffer*/
unsigned flags;    /* File status flags */
char fd;           /* File descriptor (handler) */
unsigned char hold; /* Used to indicate no buffer */
int bsize;           /* Buffer size */
unsigned char *buffer;  /* Data transfer buffer */
unsigned char *curp;  /* Current active pointer */
unsigned istemp;  /* Temporary file indicator*/
short token;      /* Used for validity checking*/

} FILE;

FILE is the structured data type name using which the logical file pointers are declared in C programs. Whenever a file is to be operated for input or output operations the pointer variable of the type FILE is used. We discuss such FILE pointer in the next article in detail.

File System Basics

The hard disk drive used as secondary storage device in the computing system provide  place to store data. The only way to store and access data on a hard disk drive is by either specifying the data’s physical location in terms of cylinder, head, and sector or by its logical location the block number on the disk. For a programmer specifying and referring the data like this is too difficult. So the operating system takes care of this by means of providing a good file system.

The operating system easily keeps track of the things stored on disks. It is nothing but a way of filing data in an easily accessible way. This is the major role played by the file system. In order to access such data on the disk the programmer is required to write ‘file-handling’ programs. The data on the secondary storage is stored only by means of file.

With reference to Operating System like "Linux", a File System is the whole structure in which the FILES are organized, stored and named especially for the users of the system.

File system treats different sets of data as files. Each file is separate from the other. Along with the A data stored within it, the file system includes additional information for each file like
  • The name of the file.
  • The access permissions on the file
  •  The time and date of creation, access, and modification of the file.


Using the available file system the user or many users can create many files. When all these files are at one place the identification is difficult for the users. The file system provides certain mechanism to make it easier to group related files together. The most commonly used mechanism is the directory structure’. The directory structure or simply directory is regularly implemented as a special type of file. The directories make it possible to create hierarchical structures of files and sub directories.

The file systems vary in implementation details. It means that all the file systems cannot be accessed by all the operating systems. The operating system ‘Linux’ includes support for many popular file systems. So, it is possible to access the file systems of other operating systems with ease. This is particularly useful in dual-boot scenarios, and when migrating files from one operating system to another.

Sunday, September 28, 2014

How to Add or Remove CSS class from element: jQuery

Earlier article was about to using basic jQuery syntaxes and changing default behaviour of html elements in MVC. As html easily provide to include CSS classes to change the design of an element or we can say anything on the web-page. The same operation can easily be performed by using jQuery according to some conditions or on some events triggering.

Add CSS class: To add a class using jQuery, programmer have to use the function addClass() which have some parameters including property name and the value to be assign.
Add below style on the page:
<style>    a.test {        font-weight: bold;    }</style>
This above style will make font-weight: bold for the anchor tag using this css class. To add this CSS class on the anchor tag, write the following line of code:

$( "a" ).addClass( "test" );

Remove Class: On click of this anchor tag or some other element, we can simple remove this class by using the below line of code:

$( "a" ).removeClass( "test" );

This code can be written on the click event of any element on the page or on some other event. Programmer must place all this jQuery code in the below block so that your code executes when the document is ready to be worked on.
$(document).ready(function(){ //write your code here});

Friday, September 26, 2014

Handling Errors and Exceptions using RAISERROR: SQL

A REISEROR statement is used to return messages to the business applications that are executing the SQL statements. This statement uses the same format as a system error or warning message generated by the database engine. Consider an example of an application that is executing a batch. If an error occurs while executing this batch, an error message will be raised and sent to the application. The application in turn will include the code to handle the error.

You can also return user-defined error messages by using the RAISERROR statement. A REISERROR severity of 1; to 19 executed in the TRY block causes the control to be transferred to the associated CATCH block.

Consider the following example of the AdventureWorks database that stores the details of the shift in which the employees work. You need to update the Shift table to update the time of a shift. While updating the shift details, you need to ensure that the difference between the start time and the end time is eight hours. If it is less than eight hours, an error is raised, and the update process is stopped.

BEGIN TRY
DECLARE @Start datetime
DECLARE @End datetime
DECLARE @Date_diff int
SELECT @Start = ‘1900-01-01 23:00:00.000’, @End = ‘1900-01-02 06:00:00.000’
SELECT @Date_diff = datediff (hh, @Start, @End)
IF (@Date_diff != 8)
 RAISERROR (‘Error Raised’, 16, 1)
ELSE
BEGIN
UPDATE HumanResources.Shift
SET StartTime = @Start, EndTime = @End
WHERE ShiftID = 3
END
END TRY
BEGIN CATCH
PRINT, ‘The difference between the Start and End time should be 8 hours’
END CATCH
GO

Thursday, September 25, 2014

Introduction to file handling

Introduction to file handling 
File system basics
Standard streams in c
File structure
FILE pointer
Opening and closing a file
File handling functions
File types, Text and Binary
Input / Output operations on file
Reading a character using getc()
Writing a character using putc()
Using feof()
Working with string using fputs() and fgets()
Using fprintf() and fscanf()
Using fread() and fwrite()
Direct Access file
fseek()

Introduction to file handling 

Main memory of the computing system is used to store temporary data that is processed to produce information. The data stored in memory is not permanent. When the system is switched off we lose the data stored in main memory. It is because of the volatile nature of main memory. But most of the time storing of data permanently for the sake of future reference is necessary. Of course in almost all organization storing of data is compulsory to produce very important resource "information". This data can be stored permanently only on the secondary storage like magnetic disk. The data on the secondary storage can be stored only through FILE. Without file nothing can be stored on the secondary storage.

Files are not only used to store data but also for other things. Our programs are also stored in the form of files are used widely in computers and play a pivotal role in data accessing. Contents are read from files and we write any contents in these files only. Various types of files are used in computers, but in C language we see two types of files namely, Text files and Binary files.
 File is nothing but a format to store data permanently on secondary storage device. A C programmer is interesting in handling the file to store data in two formats TEXT and BINARY

 The standard library 'stdio.h' in C has many files input/output functions. The file-handling functions are easy to use, powerful, and complete. We frequently use the files for storing data which can be processed by the programs. In order to store data permanently and retrieve it whenever required we need to use files.
 The editor which is used to enter the program and save it simply manipulates files. These files containing language statements are program files. The program can be translated by the compiler to make an object program. The object program is also a file. The object program is linked with other object programs to prepare an executable program. The executable program is also a file. But these all files are created and processed with operating system's file system. But using C language we organize the files to store the data in purely textual format or in binary format. The file contains data in the form of characters only is called as 'text file'. If the data is stored in the form of binary format then it is called as 'binary file'.

EnableClientScript in ASP.NET Vaildation, How to disable it

Script is a short of code, run on any browser, must to enable Java Script. If you want to enable validation along with the page then must to enable java script of the browser for running validation. That's type of validation is known as client side validation.
Let's start to learn about EnableClientScript property of control, if you will set true then validation is enable on single click or you can say validation enable on onfocus.
How to disable it
Controls_Id_name . EnableClientScript = false;


Wednesday, September 24, 2014

Breadth First Traversal in Graph

Graph Traversals

Traversing a graph is nothing but to visit or process all the nodes of the graph once and only once. two techniques are popular to traverse the graph. They are Breadth first traversal and depth first traversal. In both the traversal techniques the traversing starts from one of the nodes of the graph and from that starting node all other node are explored. In case of Breadth first traversal from the starting node all the adjacent nodes of that node are explored and the process of exploring is continued. In case of Depth first traversal only one adjacent node (from the adjacent nodes) is explored and the process of exploring continues. Understanding adjacent node is important in both the techniques of graph traversal.

Breadth First Traversal:

As the name of the traversal techniques suggests the traversal explores the nodes of graph by breadth. It means all the adjacent nodes of one selected node are explored first. From the start node all the adjacent nodes of that node are explored. From the list of explored nodes one of the node is selected as the next start node and the unexplored adjacent nodes of that node are explored. Then from the first list of explored nodes the second node is selected as the next start node and the unexplored adjacent nodes of that node are explored. The process continues till the exploration of all the nodes of the graph. The data structure QUEUE finds its application in this traversal. Consider the following graph:
Breadth First Traversal in Graph

The adjacent nodes of the node

A     :     B     C     E
B     :     A     D
C     :     A     D
D     :     B     C     E
E     :     A     D 
The breadth first traversal of the above graph, assuming node 'A' as start node is:
A     B     C     E     D
You can observe from the traversal result that the first node visited is the starting node. Then the next nodes visited are the adjacent nodes of the start node 'A'. In the last 'D' is visited because it is the only unexplored adjacent node of 'B' the exploring of nodes may be in any order. So, the traversal may also be like:

A     C     B     E     D     OR
A     E     B     C     D.
If the start node is 'C' then the BFT (Breadth First Traversal) is:
C     A     D     B     E

The formal algorithm for the BFT is :

GRAPHBFT
[TA is the one dimensional array of size n where n is the number of nodes in the given graph]
Mark the start node and insert it in the QUEUE
Repeat while QUEUE is not empty
Delete QUEUE.
Add delete node to TA at the next position.
Mark the unmarked adjacent nodes of the deleted node.
Insert the marked nodes (if any) of the deleted node in the QUEUE.
[End of while]
Print TA from the first position as traversal.
Exit.

The above algorithm works in the following manner:

Consider the following graph

Breadth First Traversal in Graph

Let us assume the start node as node 'A'. Let us insert node 'A' in the QUEUE. So, the QUEUE is:
Breadth First Traversal in Graph

When QUEUE is deleted, the node obtained is 'A'. It is added to the traversed array. So, the traversed array is:
Breadth First Graph example

The marked adjacent nodes of deleted node 'A' are, B, C and E. Insert the nodes B, C and E in the QUEUE(in any order) So, the QUEUE is:

Breadth First Traversal in Graph

QUEUE is not Empty the process is repeated.
when QUEUE is deleted, the node obtained is 'B'. It is added to the traversed array. So, the traversed array is:
Breadth First Traversal in Graph

The marked adjacent nodes of deleted node 'B' is D. Insert the node D in the QUEUE so, the QUEUE is:
Breadth First Traversal in Graph


QUEUE is not empty the process is repeated.
When QUEUE is deleted, the node obtained is 'C'. It is added to the traversed array. So, the traversed array is:
Breadth First Traversal in Graph

The marked adjacent node of deleted node 'C' are none. So the QUEUE is:
Breadth First Traversal in Graph

QUEUE is not empty the process is repeated.
When QUEUE is deleted, the node obtained is 'E'. It is added to the traversed array. So, the traversed array is:
Breadth First Traversal in Graph

The marked adjacent node of deleted node 'E' are none. So, the QUEUE is:
Breadth First Traversal in Graph

QUEUE is not empty the process is repeated.
When QUEUE is deleted, the node obtained is 'D'. It is added to the traversed array. So, the traversed array is:
Breadth First Traversal in Graph

The marked adjacent nodes of deleted node 'E' are none. So, the QUEUE is:
Breadth First Traversal in Graph

The QUEUE is empty, stop the process.
When the array is printed we get the Breadth First Traversal as:
A     B     C     E     D
© Copyright 2013 Computer Programming | All Right Reserved