8

K&R の C プログラミング言語の 176 ページを読んだとき、とても興奮しました。構造体 FILE (私が探していたもの) のすべてのメンバーを見つけました。しかし、gcc は、エラー: 'FILE' には 'fd' という名前のメンバーがありません。これは、物事が変わったことを意味します。グーグルで検索しましたが、見つかりませんでした。助けてください、よろしくお願いします。

fileno() を使用してファイル記述子を取得できますが、抽象化レベルで作業するのは嫌いです。

int
main ( int argc, char **argv ){

    FILE *fp = fopen ("ct.c", "r");
    printf ("%i", fp->fd);

    return 0;
}
4

3 に答える 3

0

@unwind の回答は良いのですが、GCC を使用する Unix ライクなシステムの別の回避策を見つけました。

残念ながら、C はリフレクションをサポートしていません (C でのリフレクション サポート) が、C プリプロセッサの出力を解析できる場合があります。


シェルスクリプトのソースコード

setivolkylany$~/Downloads$ cat script.sh 

# a tempfile for source code on the C programming language
FILE_C=`tempfile`

# a tempfile for preprocessor`s output
FILE_I=`tempfile`

printf "#include \"stdio.h\"\nint main() {return 0;}" > $FILE_C
cpp $FILE_C > $FILE_I

# parse content of the tempfile for preprocessor`s output
# and display only the structure
print_it=false
while read line; do
    if [ "$line" == "struct _IO_FILE {" ]; then
        print_it=true
    fi;
    if [ "$print_it" = true ]; then
        echo $line
    fi;
    if [ "$line" == "};" ]; then
        print_it=false
    fi;
done < $FILE_I

# clean tempfiles
rm $FILE_C $FILE_I

出力

setivolkylany$~/Downloads$ ./script.sh 
struct _IO_FILE {
int _flags;




char* _IO_read_ptr;
char* _IO_read_end;
char* _IO_read_base;
char* _IO_write_base;
char* _IO_write_ptr;
char* _IO_write_end;
char* _IO_buf_base;
char* _IO_buf_end;

char *_IO_save_base;
char *_IO_backup_base;
char *_IO_save_end;

struct _IO_marker *_markers;

struct _IO_FILE *_chain;

int _fileno;



int _flags2;

__off_t _old_offset;



unsigned short _cur_column;
signed char _vtable_offset;
char _shortbuf[1];



_IO_lock_t *_lock;
# 293 "/usr/include/libio.h" 3 4
__off64_t _offset;
# 302 "/usr/include/libio.h" 3 4
void *__pad1;
void *__pad2;
void *__pad3;
void *__pad4;
size_t __pad5;

int _mode;

char _unused2[15 chrome-remote-desktop_current_amd64.deb data_structures_algorithms_tutorial.pdf dict-uk_ua-3-5-1.oxt getline.c jquery-3.1.1.min.js ld-linux.so (1).2 ld-linux.so.2 Makefile Portable Microsoft Office 2003.exe Python-3.5.2 script.sh teamviewer_12.0.71510_amd64.deb teamviewer_12.0.71510_i386.deb text_editor.zip sizeof (int) - 4 chrome-remote-desktop_current_amd64.deb data_structures_algorithms_tutorial.pdf dict-uk_ua-3-5-1.oxt getline.c jquery-3.1.1.min.js ld-linux.so (1).2 ld-linux.so.2 Makefile Portable Microsoft Office 2003.exe Python-3.5.2 script.sh teamviewer_12.0.71510_amd64.deb teamviewer_12.0.71510_i386.deb text_editor.zip sizeof (void *) - sizeof (size_t)];

};

この決定はあまりデリケートではなく、C プログラミング言語での実装の反映を試みるものです。

于 2017-03-06T18:44:03.293 に答える