在哪里可以找到 off_t 类型的完整定义? [英] Where to find the complete definition of off_t type?

查看:34
本文介绍了在哪里可以找到 off_t 类型的完整定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 TCP 从客户端向服务器发送文件.为了标记文件的结尾,我喜欢在实际数据之前发送文件大小.所以我使用 stat 系统调用来查找文件的大小.这是 off_t 类型.我想知道它占用了多少字节,以便我可以在服务器端正确读取它.它在 中定义.但我不明白定义.它只是将 __off_t 或 _off64_t 定义为 off_t.在哪里寻找 __off_t?__ 也是头文件中大多数内容的前缀的惯例,当我阅读头文件以更好地理解时,这让我感到害怕.如何更好地读取头文件?

I am sending file from client to server using TCP. To mark the end of the file I like to send file size before the actual data. So I use stat system call to find the size of the file. This is of type off_t. I like to know how many bytes it occupies so that I can read it properly on the server side. It is defined in the <sys/types.h>. But I do not understand the definition. It just defines __off_t or _off64_t to be off_t. Where to look for __off_t? Also is it convention that __ is prefixed for most of the things in header files and scares me when I read header files to understand better. How to read a header file better?

#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 的完整定义"的答案是:在标准中,而不是在头文件中".遵循标准意味着您的代码今天和明天都可以在任何机器上运行.

Since this answer still gets voted up, I want to point out that you should almost never need to look in the header files. If you want to write reliable code, you're much better served by looking in the standard. So, the answer to "where can I find the complete definition of off_t" is "in a standard, rather than a header file". Following the standard means that your code will work today and tomorrow, on any machine.

在这种情况下,off_t 不是由 C 标准定义的.它是 POSIX 标准的一部分,您可以在这里浏览.

In this case, off_t isn't defined by the C standard. It's part of the POSIX standard, which you can browse here.

不幸的是,off_t 的定义不是很严格.我能找到的所有定义它都在 sys/types.h:

Unfortunately, off_t isn't very rigorously defined. All I could find to define it is on the page on sys/types.h:

blkcnt_toff_t 应为有符号整数类型.

blkcnt_t and off_t shall be signed integer types.

这意味着您无法确定它有多大.如果您使用的是 GNU C,则可以使用下面的答案中的说明来确保它是 64 位.或者更好的是,您可以在将其放在电线上之前转换为标准定义的尺寸.这就是 Google 的Protocol Buffers 等项目的工作方式(尽管这是一个 C++ 项目).

This means that you can't be sure how big it is. If you're using GNU C, you can use the instructions in the answer below to ensure that it's 64 bits. Or better, you can convert to a standards defined size before putting it on the wire. This is how projects like Google's Protocol Buffers work (although that is a C++ project).

为了完整起见,这里是哪个头文件定义了 off_t?"的答案:

For completeness here's the answer to "which header file defines off_t?":

在我的机器上(和大多数使用 glibc 的机器)你会在 bits/types.h 中找到定义(正如顶部的评论所说,永远不要直接包含这个文件),但它是在一堆宏中有点模糊.试图解开它们的另一种方法是查看预处理器输出:

On my machine (and most machines using glibc) you'll find the definition in bits/types.h (as a comment says at the top, never directly include this file), but it's obscured a bit in a bunch of macros. An alternative to trying to unravel them is to look at the preprocessor output:

#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() 运算符.

However, if you want to know the size of something, you can always use the sizeof() operator.

刚刚看到关于 __ 的部分问题.这个答案有很好的讨论.关键是,以 __ 开头的名称是为实现保留的(所以你不应该用 __ 开始你自己的定义).

Just saw the part of your question about the __. This answer has a good discussion. The key point is that names starting with __ are reserved for the implementation (so you shouldn't start your own definitions with __).

这篇关于在哪里可以找到 off_t 类型的完整定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆