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

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

问题描述

我从客户端使用TCP发送文件到服务器。来标记我喜欢的实际数据之前发送的文件大小的文件的末尾。所以我用统计系统调用来查找文件的大小。这是键入 off_t 的。我想知道它有多少字节占用,这样我可以在服务器端正确读取它。它在中所定义; SYS / types.h中> 。但我不明白的定义。它只是定义了 __ off_t或_off64_t off_t 。到哪里寻找 __ off_t ?也是它约定 __ 是prefixed对于大多数的东西头文件和恐吓我,当我读到的头文件,以更好地理解。如何更好地读取头文件?

 的#ifndef __off_t_defined
的#ifndef __USE_FILE_OFFSET64
的typedef __off_t off_t;
其他#
的typedef __off64_t off_t;
# 万一
#定义__off_t_defined
#万一


解决方案

由于这个答案仍然得到了投票,我想指出的是,你应该几乎从不需要在头文件的样子。如果你想编写可靠的code,你通过查看标准的服务要好得多。不是如何定义 off_t 我的机器上更好的问题是:如何是 off_t 定义的标准? 。根据标准意味着你的code今天和明天将工作,任何机器上。

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

不幸的是, off_t 不是很严格的定义。所有我能找到的定义是在<一个页面上href=\"http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/sys_types.h.html#tag_13_67\"><$c$c>sys/types.h:


  

blkcnt_t off_t 应符号整数类型。


这意味着你不能确定它有多大。如果您使用GNU C,您可以使用下面的答案,以确保它是64位。或者更好的,你可以转换为把它在电线上之前定义大小的标准。这是怎么样的谷歌的 Protocol Buffers的工作(尽管这是一个C ++项目)。


所以,我觉得我在哪里找到我的头文件的定义是不是最好的问题。
但是,对于完整性这里的答案是:

您会发现在位/ types.h中的定义(作为一个评论说,在顶部,从不直接包含这个文件),但它掩盖了位一堆宏。在试图解开他们的另一种方法是看preprocessor输出:

 的#include&LT;&stdio.h中GT;
#包括LT&; SYS / types.h中&GT;诠释主要(无效){
  off_t等等;  返回0;
}

和则:

  $ gcc的-E sizes.c | grep的__off_t
的typedef长整型__off_t;
....

不过,如果你想知道的东西的大小,你可以随时使用的sizeof()运营商。

编辑:刚才看到你对问题__ 部分。 这个答案有一个很好的讨论。关键的一点是,开始的 __名保留用于执行(所以你不应该开始与自己的定义__ )。

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 

解决方案

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. A better question than "how is off_t defined on my machine" is "how is off_t defined by the standard?". Following the standard means that your code will work today and tomorrow, on any machine.

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

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

blkcnt_t and off_t shall be signed integer types.

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).


So, I think "where do I find the definition in my header files" isn't the best question. But, for completeness here's the answer:

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;
}

And then:

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

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

Edit: 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天全站免登陆