在c语言中文件的指针是什么,C语言中文件描述符和文件指针的本质区别
1,首先了解進程運行時默認打開的文件指針以及打開的文件
/* Standard streams. /
extern struct _IO_FILE stdin; / Standard input stream. 標準輸入/
extern struct _IO_FILE stdout; / Standard output stream. 標準輸出*/
extern struct _IO_FILE stderr; / Standard error output stream. 標準出錯*/
進程運行時,默認打開兩個文件:鍵盤,屏幕
文件描述符
0 標準輸入
1 標準輸出(有緩沖區) printf()
2 標準出錯(無緩沖輸出)perror()
看懂了上面的解釋我們再往下看
我們來理解下這段代碼:
char buf[20]={};
read(0, buf, 12);
fread(buf, 12, 1, stdin);
代碼中read()與fread()效果一樣,都是從鍵盤輸入中讀取12個字節放到buf這個緩沖區內;
所以我們可以知道0和stdin分別是同一個文件的文件描述符和文件指針
它們兩的聯系是什么呢?接著看
2,我們來了解一下FILE是如何定義的
/usr/include/x86_64-linux-gnu/bits/types/FILE.h:7:typedef struct _IO_FILE FILE;
在Ubuntu下的/usr/include/x86_64-linux-gnu/bits/types/路徑下的FILE.h內有這么一段代碼:
typedef struct _IO_FILE FILE; //給struct _IO_FILE這個結構體取個別名叫FILE;
這里我們知道了,原來FILE是代表struct _IO_FILE這個結構體;
那我們接下來就要找到struct _IO_FILE這個結構體又是在哪里定義的;
/usr/include/x86_64-linux-gnu/bits/
在Ubuntu下的/usr/include/x86_64-linux-gnu/bits/路徑下的libio.h文件內有這么一段代碼:
struct _IO_FILE {
int _flags;/* High-order word is _IO_MAGIC; rest is flags. */
#define _IO_file_flags _flags
/* The following pointers correspond to the C++ streambuf protocol. */
/* Note: Tk uses the _IO_read_ptr and _IO_read_end fields directly. */
char* _IO_read_ptr;/* Current read pointer */
char* _IO_read_end;/* End of get area. */
char* _IO_read_base;/* Start of putback+get area. */
char* _IO_write_base;/* Start of put area. */
char* _IO_write_ptr;/* Current put pointer. */
char* _IO_write_end;/* End of put area. */
char* _IO_buf_base;/* Start of reserve area. */
char* _IO_buf_end;/* End of reserve area. */
/* The following fields are used to support backing up and undo. */
char *_IO_save_base; /* Pointer to start of non-current get area. */
char *_IO_backup_base; /* Pointer to first valid character of backup area */
char *_IO_save_end; /* Pointer to end of non-current get area. */
struct _IO_marker *_markers;
struct _IO_FILE *_chain;
int _fileno; =====>>文件描述符
#if 0
int _blksize;
#else
int _flags2;
#endif
_IO_off_t _old_offset; /* This used to be _offset but it's too small. */
#define __HAVE_COLUMN /* temporary */
/* 1+column number of pbase(); 0 is unknown. */
unsigned short _cur_column;
signed char _vtable_offset;
char _shortbuf[1];
/* char* _save_gptr; char* _save_egptr; */
_IO_lock_t *_lock;
#ifdef _IO_USE_OLD_IO_FILE
};
struct _IO_FILE_complete
{
struct _IO_FILE _file;
#endif
#if defined _G_IO_IO_FILE_VERSION && _G_IO_IO_FILE_VERSION == 0x20001
_IO_off64_t _offset;
# if defined _LIBC || defined _GLIBCPP_USE_WCHAR_T
/* Wide character stream stuff. */
struct _IO_codecvt *_codecvt;
struct _IO_wide_data *_wide_data;
struct _IO_FILE *_freeres_list;
void *_freeres_buf;
# else
void *__pad1;
void *__pad2;
void *__pad3;
void *__pad4;
# endif
size_t __pad5;
int _mode;
/* Make sure we don't get into trouble again. */
char _unused2[15 * sizeof (int) - 4 * sizeof (void *) - sizeof (size_t)];
#endif
};
這段代碼定義了這個結構體
看起來很嚇人,其實我們這里只需要知道int _fileno就是文件描述符就可以了;其它代碼就是用來管理緩沖區的;
知道這些后我們再來通過代碼驗證一下,如這段代碼:
#include #include #include int main(int argc, char **argv)
{
FILE *fp = fopen("./1.txt", "r+");
if(fp == NULL )
{
perror("fopen failed!");
return -1;
}
//打印文件描述符
printf("%d\n", fp->_fileno);
//fwrite("good boy",8,1,fp);
write(fp->_fileno, "good boy", 8);
fclose(fp);
return 0;
}
總結:
文件指針是一個結構體,它里面也有文件描述符
其他成員是用來管理緩沖區
總結
以上是生活随笔為你收集整理的在c语言中文件的指针是什么,C语言中文件描述符和文件指针的本质区别的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 解读route命令
- 下一篇: CentOS---网络配置详解