Thread: Fseek Ftell Non-Compliant

  1. #1
    Banned
    Join Date
    Jul 2022
    Posts
    112

    Fseek Ftell Non-Compliant

    FIO19-C. Do not use fseek() and ftell() to compute the size of a regular file - SEI CERT C Coding Standard - Confluence


    Is a buffered fread / read to obtain size considered a bad approach,
    compared to fseek / ftell ?

  2. #2
    Registered User
    Join Date
    Feb 2019
    Posts
    1,048
    stat()

  3. #3
    Banned
    Join Date
    Jul 2022
    Posts
    112
    Stat is posix.

    Even if fread / read can determine file size,
    is it a good approach ?

  4. #4
    Banned
    Join Date
    Jul 2022
    Posts
    112
    Also one has to take into account a performance issue,
    since fread / read requires a buffer.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,436
    It might be your only approach if you're going to eschew creature comforts offered by POSIX
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Banned
    Join Date
    Jul 2022
    Posts
    112
    _stat is a function that is not native to Windows. It's a helper function to ease the porting of UNIX programs to Windows. But the UNIX file model just doesn't apply to Windows, so not all fields make sense. For instance, Windows has real ACL's, not rwx bits. There's just no way to fit all the ACL information in st_mode.

    May 23, 2011 at 8:50
    MSalters
    c++ - How does _stat() under Windows exactly work - Stack Overflow

  7. #7
    Banned
    Join Date
    Jul 2022
    Posts
    112
    off_t, File offset, measured in bytes from the beginning of a file or device. off_t is normally defined as a signed, 32-bit integer. In the programming environment which enables large files, off_t is defined to be a signed, 64-bit integer.
    IBM Documentation

    Returned value
    If successful, the ftello() function returns the calculated value.

    If unsuccessful, the ftello() function returns (off_t)-1 and sets errno to one of the following values:
    IBM Documentation


    off_t is 32 / 64 bit integer,
    and with a large file, it will overflow.

    Author may be misinformed..
    Last edited by kodax; 11-21-2022 at 01:59 PM.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,436
    Dribble fest.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Doing a malloc from an ftell
    By Asymptotic in forum C Programming
    Replies: 1
    Last Post: 01-21-2019, 03:21 PM
  2. Is the following code Standard compliant ?
    By manasij7479 in forum C++ Programming
    Replies: 10
    Last Post: 05-08-2011, 08:15 PM
  3. Can vc++2005eebe set up to be ansi C compliant
    By MickD in forum C Programming
    Replies: 4
    Last Post: 08-29-2006, 05:53 PM
  4. Reg. fseek
    By pokks in forum C Programming
    Replies: 1
    Last Post: 01-16-2006, 01:28 PM
  5. How to make Dev-C++ more C99 compliant ??
    By Antigloss in forum C Programming
    Replies: 13
    Last Post: 10-06-2005, 02:13 PM

Tags for this Thread