-
August 22nd, 2004, 03:25 PM
#1
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
msg.Format( "Recno = %d", recno);
AfxMessageBox(msg);
if (fseek(fptr,offset,SEEK_SET) !=0){
AfxMessageBox(" can't move pointer there");
}else{
bytes_out = fread(&DB, sizeof(DB),1, fptr);
fclose(fptr);
if (bytes_out) AfxMessageBox("File was Read, Bytes = ");
}
fclose(fptr);
}
-
August 23rd, 2004, 02:40 AM
#2
From MSDN:
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.
With best wishes, Alexander Hritonenkov
-
August 23rd, 2004, 05:01 PM
#3
Thanks, were in MSDN did you get that from? I couldn't find that info in my docuementation?
TIA
-
August 24th, 2004, 02:35 AM
#4
-
August 24th, 2004, 03:58 AM
#5
Thanks,
But doesn't make any sense as to why you would want to be able to move the pointer beyond the end of the file??
-
August 24th, 2004, 05:38 AM
#6
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.
With best wishes, Alexander Hritonenkov
-
August 24th, 2004, 02:06 PM
#7
Re: Problems with fseek, not notifying past end of file
Possible, I try it sometime in the next couple of days and get back to you ..... j/k. Thanks, I will try that, that's an interesting though.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|