Saturday, 31 August 2013

Random access files | C Programming Language Tutorial



Random access files:

The file I/O routines all work in the same way; unless the user takes explicit steps to change the file position indicator, files will be read and written sequentially. A read followed by a write followed by a read (if the file was opened in a mode to permit that) will cause the second read to start immediately following the end of the data just written. (Remember that stdio insists on the user inserting a buffer-flushing operation between each element of a read-write-read cycle.) To control this, the Random Access functions allow control over the implied read/write position in the file. The file position indicator is moved without the need for a read or a write, and indicates the byte to be the subject of the next operation on the file.

Three types of function exist which allow the file position indicator to be. examined or changed. Their declarations and description follow.
#include <stdio.h>
/* return file position indicator * / .
long ftell(FILE *stream);
int fgetpos(FILE *stream, fpos_t *pos);
/* set file position indicator to zero * / void rewind (FILE *stream);
/* set file position indicator */
int fseek(FILE *stream, long offset,
int ptmame); int fsetpos(FILE *stream, const fpos_t *pos);

ftell returns the current value (measured in characters) of the file position indicator if stream refers to a binary file. For a text file, a 'magic' number is returned, which may only be used on a subsequent call to fseek to reposition to the current file position indicator. On failure, -1 L is returned and ermo is set.
rewind sets the current file position indicator to the start of the file indicated by stream. The file's error indicator is reset by a call of rewind. No value is returned.
Fseek allows the file position indicator for stream to be set to an arbitrary value (for binary files), or for text files, only to a position obtained from ftell, as follows:
=> In the general case the file position indicator is set to offset bytes (characters) from a point in the file determined by the value of ptrname. Offset may be negative. The values of ptmame may be SEEK_SET, which sets the file position indicator relative to the beginning of the file, SEEK_CUR, which sets the file position indicator relative to its current value, and SEEK_END, which sets the file position indicator relative to the end of the file. The latter is not necessarily guaranteed to work properly on binary streams.
=> For text files, offset must either be zero or a value returned from a previous call to ftell for the same stream, and the value ofptrname must be. SEEK_SET.
=> Fseek clears the end of file indicator for the given stream and erases the memory of any ungetc. It works for both input and output.
=> Zero is returned for success, non-zero for a forbidden request.

Note that.for ftell and fseek it must be possible to encode the value of the file position indicator into a long. This may not work for very long files, so the Standard introduces fgetpos and fsetpos which have been specified in a way that removes the problem.
fgetpos stores the current file position indicator for stream in the object pointed to by pos. The value stored is 'magic' and only used to return to the specified position for the same stream using fsetpos. fsetpos works as described above, also clearing the stream's end-of-file indicator and forgetting the effects of any ungetc operations. For both functions, on suc,cess, zero is returned; on failure, non-zero is returned and errno is set.
#include <stdio.h>
;
int main()
{
FILE * f;
f= fopen("myfile.txt", "w"); fputs("Hello World", f); fseek(f, 6,
SEEK_SET); fputs(" India", f);
fclose(f);
return 0;
}
Now the file consist of following data:
Hello India

Sample program to print the current position of the file pointer in the file using ftell()
function:
#include<stdio.h> .
void main()
{
FILE *fp;
char ch; fp=fopenG'lines.txt" ,"r"); fseek( fp,21 ,SEEK_SET); ch=fgetc(fp );
clrscr();
while(!feof(fp ))
{
printf("%c" ,ch); printf("%d",ftell(fp )); ch=fgetc(fp );
}
rewind(fp ); while(! feof( fp))
{
printf("%c" ,ch); printf("%d" ,ftell(fp)); ch=fgetc(fp );
}
fclose(fp );
}

0 comments:

Post a Comment

 

C Programming Language Interview Questions and Answers Tutorial for beginners. Copyright 2013 All Rights Reserved