0

For whence parameter of fseek (or lseek in POSIX), some languages that calls C lib assume SEEK_SET, SEEK_CUR, SEEK_END are 0,1,2, respectively, such as gfortran, Nim [^py].

[^py]: while Python's seek specializes concrete values of SEEK_*, CPython does map 0,1,2 to C lib's values internally.

I've looked up C and POSIX specialization, neither specializes concrete values of SEEK_*.

In code, I wonder in which OS and C compiler, the following code's output isn't 012:

#include <stdio.h>

int main(){
    printf("%d%d%d\n", SEEK_SET, SEEK_CUR, SEEK_END);
}

I've tested on some OSes and C compilers, where the output is just 012:

  • Windows: gcc(mingw), MSVC
  • Debian: clang, gcc
  • FreeBSD: clang
  • Solaris(SunOS5): Oracle Solaris Studio C Compiler
  • Android(Termux): clang

And if there're none, then it's less meaningful to add fix for the existing implementations using fixed values.

10
  • To clearify, I wonder if there is any C libraries/OSes whose SEEK_* values differ Commented Aug 1, 2024 at 3:31
  • 2
    Why does that matter? If you use the SEEK_* constants, the actual values should be irrelevant to you regardless of the library or OS. That's the entire reason those identifiers exist. Commented Aug 1, 2024 at 3:37
  • Because, as mentioned above, some langauges' implementation used fixed values instead of SEEK_*. I wonder if they'll fail on some libraries/OSes Commented Aug 1, 2024 at 3:45
  • And as I said, the values assigned to SEEK_* should not matter to you. If the SEEK_* defines don't exist, then clearly you'll have to use that language's fixed values instead. Asking us to provide a list of those languages isn't an appropriate use of this site. Commented Aug 1, 2024 at 3:49
  • "I wonder if they'll fail on some libraries/OSes", I'm just thinking if any C lib/OSes uses other values for SEEK_ macros Commented Aug 1, 2024 at 3:54

2 Answers 2

1

Will SEEK_SET SEEK_CUR SEEK_END aren't 0,1,2 on any C lib?

Maybe. Yet if not today, maybe tomorrow.


If you must use 0, 1, 2 in calling fseek(), use the number as an array index.
I doubt there is a real case where one must do so.

#include <stdio.h>
const int whence [] = { SEEK_SET, SEEK_CUR, SEEK_END };

int result = fseek(stream, 0, whence[2]);  // Go to the end
Sign up to request clarification or add additional context in comments.

Comments

1

In the ideal instance, you would want to write a C program to create an include file for the constants for the language that does not have those constants defined. e.g.

  #include <stdio.h> 

  int 
  main (int argc, char *argv []) 
  {
    printf ("INTEGER, PARAMETER :: SEEK_SET = %d, SEEK_CUR = %d, SEEK_END = %d\n", 
        SEEK_SET, SEEK_CUR, SEEK_END);
  }

I have no idea what fortran include files look like, this would need to be fixed.

In this particular instance, SEEK_SET, SEEK_CUR and SEEK_END are well defined as 0, 1, and 2, and you aren't going to have a problem with them.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.