-->

Tuesday, September 30, 2014

File Pointer in C

In order to use files it is mandatory to learn the file Input / Output operations. Learning of these is nothing but to understand the functions to write data into a file, to read data from a file, etc. For at any of such operations the file must be opened. The file opening operation is done through a pointer variable called as file pointer. So, file pointer is nothing but a variable which is declared and then I to any required physical file. All the I/O operations are done to physical file through the logical file.

A File Pointer is a special type of pointer variable of the type FILE that is used to store the address of a physical file.

FILE I/O is almost identical to the console and terminal I/O operations. The pnntf() function is used to display the data on the screen. And in file handling we use fprintf() to write the data into the file. We use scanf() function to read the data from the keyboard. And in file handling we use fscanf() to read the data from the file. This is called file manipulation.

The major difference between manipulation of file and terminal I/O is that the terminal I/O is standard no need to specify anything but it is necessary to specify files to be used in the programs where file I/O are performed. It is possible have many files on the disk. If you wish to use a file or files in the programs, then you must specify which file or files you wish to use. Specification of the file that is to be used is referred as opening a file.
At the time of opening the file it is also compulsory to specify the type of operation to be performed i.e. Read from the file, Write to the file, or both. This specification is done through the file opening mode. When different files are used in the program, it is required to specify a particular file for reading or writing. It can be done using a variable called a FILE pointer.

Every file you open has its own FILE pointer variable. When you wish to write to a fiie you specify the file by using its file pointer variable. The file pointer declared using FILE data type is called as logical file. The data is not stored permanently on the secondary storage with this name. It is stored with other file name which is called as physical file name. The logical file is linked with this physical file name. The read/write operations are done on logical file in the program which is indirectly transferred from or to the physical file. The linking of logical file with physical file is done through fopen() function. The FILE pointer variables can be declared as follows:

FILE *fP1, *fp2, *fp3;

The variables fpl, fp2, fp3 are the pointer variables of the type FILE or simply file pointers. They can store the address of any physical file.

Any name can be used as file pointer but must be a valid variable or identifier name of C language. The rules of standard data type identifiers apply to these variables also. The declared FILE Pointer variable may hold address of any physical file but only one at a time. So, at a given point of time logically a logical file points to one and only one physical file. The read / write operactions performed on the file pointer indirectly directed to physical file. At the time of opening file the file pointed by file pointer variable is mentioned along with the type of operation. The opened file is closed when the operation is finished or whenever a new file is to be attached. We will discuss file opening and closing in more details in the next article.

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;


© Copyright 2013 Computer Programming | All Right Reserved