Problems with fseek, not notifying past end of file
I'm using 'fseek' and when I try to set the pointer beyond the end of file, I don't get and error message. Does anybody know how to generate an error message from from 'fseek' to monitor when you have gone beyond the end of file.
Here is my code:
struct Person
{
int RecNum;
char Type[11];
char CoName[41];
char LastName[41];
char FirstName[41];
char Address[61];
char TeleNum[16];
};
struct Person DB;
void rfile_db(void) // READ A RECORD TO DB
{
FILE *fptr;
int bytes_out;
CString msg;
if ((fptr = fopen("temp3.rec", "r+b")) ==NULL)
{
AfxMessageBox("Can't Open/Create File ");
}
// recno is the max # of records in the file
offset = recno * sizeof(DB); // DB is a struct of the file
You can use fseek to reposition the pointer anywhere in a file. The pointer can also be positioned beyond the end of the file.
So it's a normal behavior. I think, all what you can to do - is to query file size (and current file pointer position) before setting file pointer to new position.
I'm not sure, but may be it is for creating files like 'something-"empty"_space-something'. I mean that you can write 24 bytes from the beginning of file, then set pointer to 1024 bytes and write 40 more bytes without writing 1000 intermediate bytes.
May be these bytes will be zeroes, may be not. You can make a test program, I am too lazy.
Bookmarks