CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jun 2004
    Posts
    1,352

    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);



    }

  2. #2
    Join Date
    Apr 2002
    Location
    St.Petersburg, Russia
    Posts
    714
    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

  3. #3
    Join Date
    Jun 2004
    Posts
    1,352
    Thanks, were in MSDN did you get that from? I couldn't find that info in my docuementation?


    TIA

  4. #4
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    For example...here...

  5. #5
    Join Date
    Jun 2004
    Posts
    1,352
    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??

  6. #6
    Join Date
    Apr 2002
    Location
    St.Petersburg, Russia
    Posts
    714
    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

  7. #7
    Join Date
    Jun 2004
    Posts
    1,352

    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
  •  





Click Here to Expand Forum to Full Width

Featured