Thread: What will happen if lseek() is wrong?

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    284

    What will happen if lseek() is wrong?

    Suppose the file size is less than 10000, what will happen when blindly write (fd, "a", 1) ?

    Code:
    fd=open(fname,O_RDWR|O_CREATE,0777);
    lseek(fd, 10000l, SEEK_SET);
    write (fd, "a", 1);

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    lseek doesn't appear in my standard. But this was the first Google hit for "lseek", and it seems pretty clear on the subject, if your lseek is the same as theirs.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    284
    Thanks, but my question is when the "offset" is too large, will lseek fail? will write fail? Or will they have ways to handle it? I don't have a linux machine at hand and can't check it.

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by meili100 View Post
    Suppose the file size is less than 10000, what will happen when blindly write (fd, "a", 1) ?

    Code:
    fd=open(fname,O_RDWR|O_CREATE,0777);
    lseek(fd, 10000l, SEEK_SET);
    write (fd, "a", 1);
    The file will be extended to 10001 bytes. The byte 'a' will be written at byte position 10000 (the 10001st byte). There is no "failure" of any kind -- this is normal operation.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I shouldn't do this, but since you couldn't be bothered to click on the bloody link I just provided, I will quote:
    Quote Originally Posted by IEEE and the Open Group
    The lseek() function shall allow the file offset to be set beyond the end of the existing data in the file. If data is later written at this point, subsequent reads of data in the gap shall return bytes with the value 0 until data is actually written into the gap.

    The lseek() function shall not, by itself, extend the size of a file.
    That still seems pretty clear on the subject.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  2. problem with lseek system call on unix
    By kheinz in forum C Programming
    Replies: 2
    Last Post: 06-03-2003, 12:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM