The tell function is used to find the current position of a file pointer.
#include <stdio.h>
#include <io.h>
#include <fcntl.h>
#include <errno.h>
int main () {
int Account_Master;
char Buffer [100];
int Maximum;
int Actual;
/* Other declarations */
Maximum = 100;
Account_Master = open ("accounts.dat", O_RDWR);
if (Account_Master >0) {
printf ("Open succeeded.\n");
} else {
printf ("Open failed.\n")
switch (errno) {
case EACCES: printf ("Permission denied.\n"); break;
case EINVACC: printf ("Invalid access mode.\n"); break;
case EMFILE: printf ("No file handle available.\n"); break;
case ENOENT: printf ("File or path not found.\n"); break;
default: printf ("Unknown error.\n"); break;
}
/* File processing */
while (!eof (Account_Master)) {
Pos = tell (Account_Master);
Actual = read (Account_Master, Buffer, Maximum);
/* MODIFY THE RECORD */
/* rewrite the record */
lseek (Account_Master, Pos, SEEK_SET);
Actual = write (Account_Master, Buffer, Maximum);
}
if (close (Account_Master) == 0) {
printf ("Close succeeded.\n");
} else {
printf ("Close failed.\n")
switch (errno) {
case EBADF: printf ("File not open.\n"); break;
default: printf ("Unknown error.\n"); break;
}
}
return 0;
}
| lseek | Move a file pointer |
Copyright © 1999-2007, jhyoung,
revised 4/30/2007