如何在C中打印文件描述符中的信息。

时间:2017-03-30 09:36:52

标签: c printf fopen

(gdb) print* data_out_file[0]
$1 = {_flags = -1, _IO_read_ptr = 0xffffffffffffffff <Address 0xffffffffffffffff out of bounds>, 
  _IO_read_end = 0xffffffffffffffff <Address 0xffffffffffffffff out of bounds>, 
  _IO_read_base = 0xffffffffffffffff <Address 0xffffffffffffffff out of bounds>, 
  _IO_write_base = 0xffffffffffffffff <Address 0xffffffffffffffff out of bounds>, 
  _IO_write_ptr = 0xffffffffffffffff <Address 0xffffffffffffffff out of bounds>, 
  _IO_write_end = 0xffffffffffffffff <Address 0xffffffffffffffff out of bounds>, 
  _IO_buf_base = 0xffffffffffffffff <Address 0xffffffffffffffff out of bounds>, 
  _IO_buf_end = 0xffffffffffffffff <Address 0xffffffffffffffff out of bounds>, 
  _IO_save_base = 0xffffffffffffffff <Address 0xffffffffffffffff out of bounds>, 
  _IO_backup_base = 0xffffffffffffffff <Address 0xffffffffffffffff out of bounds>, 
  _IO_save_end = 0xffffffffffffffff <Address 0xffffffffffffffff out of bounds>, _markers = 0xffffffffffffffff, _chain = 0x11004147ffffffff, 
  _fileno = 498074112, _flags2 = 12648704, _old_offset = 135392766359363840, _cur_column = 240, _vtable_offset = 66 'B', _shortbuf = "á", 
  _lock = 0xecfc392019202004, _offset = -35, __pad1 = 0xffffffffffffffff, __pad2 = 0xffffffffffffffff, __pad3 = 0xffffffffffffffff, 
  __pad4 = 0xffffffffffffffff, __pad5 = 18446744073709551615, _mode = -1, _unused2 = 'ÿ' <repeats 20 times>}
如果我们打印文件指针,我们得到描述符,在GDB中

。 只是想知道我们如何在像printf这样的C代码中做到这一点?

1 个答案:

答案 0 :(得分:3)

假设您希望int 文件描述符值与有效的FILE *流对象相关联,请使用the fileno() function

  

命名

     

fileno - 将流指针映射到文件描述符

     

<强>概要

#include <stdio.h>

int fileno(FILE *stream);
     

<强>描述

     

fileno()函数应返回整数文件描述符   与stream指向的stream相关联。

     

返回值

     

成功完成后,fileno()将返回整数值   与stream关联的文件描述符的。否则,价值   应返回-1并设置errno以指示错误。

取消引用FILE *流对象本身。这样做会导致脆弱的,不可移植的代码容易破损。在某些实现中,FILE *结构只能被解除引用。