programing

off_t 타입의 완전한 정의는 어디서 찾을 수 있습니까?

javaba 2022. 8. 1. 23:46
반응형

off_t 타입의 완전한 정의는 어디서 찾을 수 있습니까?

저는 TCP를 사용하여 클라이언트에서 서버로 파일을 보내고 있습니다.파일의 끝을 표시하기 위해 실제 데이터보다 파일 크기를 먼저 보내고 싶습니다. 저는 ★★★★★★★★★★★★★★★★★★★★★.stat시스템 호출을 수행하여 파일 크기를 찾습니다.이것은 종류입니다off_t서버측에서 올바르게 읽을 수 있도록 몇 바이트를 점유하고 있는지 알고 싶습니다.은 「아까운 이야기」에되어 있습니다.<sys/types.h>을 사용하다「 」를 정의하기만 하면 .__off_t or _off64_toff_t__off_t, 라고 하는 ★★★★★★★★★★★★★★★★★?__헤더 파일에는 대부분 접두사가 붙고 헤더 파일을 읽으면 더 잘 이해하기 위해 무서워집니다.헤더 파일을 더 잘 읽는 방법

#ifndef __off_t_defined
# ifndef __USE_FILE_OFFSET64
typedef __off_t off_t;
# else
typedef __off64_t off_t;
# endif
# define __off_t_defined
#endif 

이 답변은 아직 투표가 진행되기 때문에 헤더 파일을 찾을 필요가 거의 없다는 점을 지적하고 싶습니다.신뢰할 수 있는 코드를 작성하려면 표준을 확인하는 것이 훨씬 좋습니다. 수 요?'라는입니다.off_t는 " 표준입니다.표준에 준거하면, 오늘과 내일, 어느 머신에서도 코드가 동작합니다.

「」는,off_tC 표준으로 정의되어 있지 않습니다.POSIX 표준의 일부이며, 여기에서 참조할 수 있습니다.

★★★★★★★★★★★★★★★★★★★.off_t엄밀하게 정의되어 있지 않습니다.정의할 수 있는 것은 다음 페이지뿐입니다.

blkcnt_t ★★★★★★★★★★★★★★★★★」off_t부호 있는 정수형이어야 한다.

이것은 얼마나 큰지 확신할 수 없다는 것을 의미합니다.GNU C 를 사용하고 있는 경우는, 다음의 회답의 순서에 따라서, 64 비트가 되어 있는 것을 확인할 수 있습니다.또는 회선에 설치하기 전에 표준으로 정의된 크기로 변환할 수도 있습니다.Google의 Protocol Buffers(프로토콜 버퍼)와 같은 프로젝트는 C++ 프로젝트이지만 이렇게 작동합니다.


것은, 「 헤더 이 정의되고 있는가」에 대해서, 「 헤더 파일이 정의되고 있는가」에 .off_t

machine(및사용하는 에서는 my machine('glibc')에 bits/types.h(상부에 코멘트가 있듯이, 이 파일은 직접 포함하지 말아 주세요).다만, 매크로의 집합에서는 조금 흐릿하게 되어 있습니다.이러한 문제를 해결하기 위한 다른 방법은 프리프로세서의 출력을 확인하는 것입니다.

#include <stdio.h>
#include <sys/types.h>

int main(void) {
  off_t blah;

  return 0;
}

그 후:

$ gcc -E sizes.c  | grep __off_t
typedef long int __off_t;
....

그 '크기', '크기', '크기', '크기', '크기', '크기', '크기', '크기', '크기', '크기', '크기', '크기', '크기', '크기'를 쓰면 됩니다.sizeof()환입니니다다

집 about에 .__이 답변은 토론이 잘 된다.요점은 이름이 다음 글자로 시작된다는 것이다.__되어 있기 에 (자신의 를 '자신의 정의'로 하는 것이.)__를 참조해 주세요.

"GNU C 라이브러리 참조 설명서"에 나와 있는 바와 같이

off_t
    This is a signed integer type used to represent file sizes. 
    In the GNU C Library, this type is no narrower than int.
    If the source is compiled with _FILE_OFFSET_BITS == 64 this 
    type is transparently replaced by off64_t.

그리고.

off64_t
    This type is used similar to off_t. The difference is that 
    even on 32 bit machines, where the off_t type would have 32 bits,
    off64_t has 64 bits and so is able to address files up to 2^63 bytes
    in length. When compiling with _FILE_OFFSET_BITS == 64 this type 
    is available under the name off_t.

따라서 클라이언트와 서버 간의 파일사이즈를 신뢰성 높은 방법으로 표현하고 싶은 경우는, 다음과 같이 할 수 있습니다.

  1. off64_t 및 " " "stat64()에 따라 기능한다때).stat64이 들어있어요.off64_t★★★★★★★★★★★★★★★★★」「」라고 입력합니다.off64_t는 32비트머신 및 64비트머신에서 같은 사이즈를 보증합니다.
  2. 코드 컴파일 전에 설명한 바와 같이-D_FILE_OFFSET_BITS == 64의 「이행」을 사용합니다.off_t ★★★★★★★★★★★★★★★★★」stat().
  3. ★★off_tint64_t고정 사이즈(C99 표준).주의: (저희 책 'C in a Nutshell'에서는 C99가 표준이지만 구현에서는 옵션이라고 합니다.)최신 C11 규격은 다음과 같습니다.
7.20.1.1 Exact-width integer types

    1 The typedef name intN_t designates a signed integer type with width N ,
    no padding bits, and a two’s complement representation. Thus, int8_t 
    denotes such a signed integer type with a width of exactly 8 bits.
    without mentioning.

구현에 대해서:

7.20 Integer types <stdint.h>

    ... An implementation shall provide those types described as ‘‘required’’,
    but need not provide any of the others (described as ‘‘optional’’).
    ...
    The following types are required:
    int_least8_t  uint_least8_t
    int_least16_t uint_least16_t
    int_least32_t uint_least32_t
    int_least64_t uint_least64_t
    All other types of this form are optional.

따라서 일반적으로 C 규격은 크기가 고정된 유형을 보장할 수 없습니다.그러나 대부분의 컴파일러(gcc 포함)는 이 기능을 지원합니다.

정의를 추적하는 데 문제가 있는 경우 컴파일러의 전처리된 출력을 사용하면 필요한 모든 것을 알 수 있습니다.예.

$ cat test.c
#include <stdio.h>
$ cc -E test.c | grep off_t
typedef long int __off_t;
typedef __off64_t __loff_t;
  __off_t __pos;
  __off_t _old_offset;
typedef __off_t off_t;
extern int fseeko (FILE *__stream, __off_t __off, int __whence);
extern __off_t ftello (FILE *__stream) ;

전체 출력을 보면 헤더 파일의 정확한 위치와 정의된 행 번호도 확인할 수 있습니다.

# 132 "/usr/include/bits/types.h" 2 3 4


typedef unsigned long int __dev_t;
typedef unsigned int __uid_t;
typedef unsigned int __gid_t;
typedef unsigned long int __ino_t;
typedef unsigned long int __ino64_t;
typedef unsigned int __mode_t;
typedef unsigned long int __nlink_t;
typedef long int __off_t;
typedef long int __off64_t;

...

# 91 "/usr/include/stdio.h" 3 4
typedef __off_t off_t;

만약 당신이 휴대용 코드를 쓰고 있다면, 정답은 "알 수 없다"입니다. 좋은 소식은 그럴 필요가 없다는 것입니다.프로토콜에는 (예를 들어) "8 옥텟, 빅 엔디안 형식"으로 크기를 쓰는 것이 포함되어야 합니다(이상적으로는 실제 크기가 8 옥텟에 맞는지 확인합니다).

언급URL : https://stackoverflow.com/questions/9073667/where-to-find-the-complete-definition-of-off-t-type

반응형