Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Lập Trình C

Kiểu dữ liệu FILE trong C

FILE là kiểu dữ liệu mờ (opaque type) - triển khai ẩn, phụ thuộc hệ thống. Trên Ubuntu (glibc):

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;
    _IO_off_t _old_offset;
    unsigned short _cur_column;
    signed char _vtable_offset;
    char _shortbuf[1];
    _IO_lock_t *_lock;
    // ... wide char fields, padding ...
};

Ví dụ dùng FILE

FILE *fp = fopen("/tmp/test.txt", "r");
char buf[255];
fscanf(fp, "%s", buf); printf("1: %s\n", buf);
fgets(buf, 255, fp); printf("2: %s\n", buf);
fgets(buf, 255, fp); printf("3: %s\n", buf);
fclose(fp);