Normally reading and accessing all had done sequentially i.e. start starting from the beginning with the first character and end till last. However, sometimes we need to access a word at the middle i.e. Called “Random Access”. Random access allows us to access a particular data item located at anywhere in file.
For random access file processing two functions are used:
1. fseek(): – this function moves file position at specified position in file. Records can access from the middle of file also.
Syntax: fseek(file pointer, number of characters, mode);
This function takes three arguments: First, the file pointer. Second, number of characters move to new position. Variable must be long integer for it. Third, mode that takes 0 and 1. 0 for positioning a pointer at beginning of the file 1 for relative current position where pointer is. 2 end of file. This function returns non zero value if successfully otherwise returns 0 in case of failure.
2. ftell(): – this function is used to return current position of pointer. It takes one argument. The return value is always a long integer.
Syntax: variable=ftell(file pointer);
Two types of files are there in C Programming:
- Text files
- Binary files
Text files only consists text information i.e. Alphabet, digits, symbols etc… Every these have ASCII code and in reality all ASCII codes of characters stored in text files.
For Eg: ASCII code of alphabet ‘A’ is 65.
Wherever, binary files only consists the bytes. For Eg: we store a images/pictures etc. They always store in a binary file.
In text binary, a special character whose ASCII value is 20 is inserted at the end of last character. This special character is used to indicate that the file is ended.
If that character is searched at anywhere in file, the read() would return EOF. No such an special character exists in binary file.