简体   繁体   中英

how to print the information in file descriptor in C.

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

in GDB if we print the file pointer we get the descriptor. just wondering how do we do it in C code like printf ?

Assuming you want the int file descriptor value associated with a valid FILE * stream object, you use the fileno() function :

NAME

fileno - map a stream pointer to a file descriptor

SYNOPSIS

 #include <stdio.h> int fileno(FILE *stream); 

DESCRIPTION

The fileno() function shall return the integer file descriptor associated with the stream pointed to by stream .

RETURN VALUE

Upon successful completion, fileno() shall return the integer value of the file descriptor associated with stream . Otherwise, the value -1 shall be returned and errno set to indicate the error.

You do not dereference the FILE * stream object itself. Doing so results in fragile, non-portable code that's prone to breakage. On some implementations, the FILE * structure simply can not be dereferenced.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM