LINUX ANSI C库函数FOPEN()的文件流指针结构体访问代码

 1 [root@localhost ccode]# cat ptr_struct_file.c 
 2 #include <stdlib.h>
 3 #include <stdio.h>
 4 #include <string.h>
 5 
 6 #define ptr(CONTENT, MSG) printf(CONTENT":\t%p\n",MSG)
 7 
 8 int main(int argc, char *argv[])
 9 {
10     FILE *fp_src, *fp_des;
11     char buffer[10], buffer1[128];
12     int i = 0;
13     if((fp_src = fopen(argv[1], "r+")) == NULL)
14     {
15         perror("open1");
16         exit(EXIT_FAILURE);
17     }
18     if((fp_des = fopen(argv[2], "w+")) == NULL)
19     {
20         perror("open2");
21         exit(EXIT_FAILURE);
22     }
23     setvbuf(fp_src, buffer1, _IOLBF, 128);
24 
25     do
26     {
27         ptr("src_IO_read_ptr", fp_src->_IO_read_ptr);
28         ptr("_IO_read_end", fp_src->_IO_read_end);
29         ptr("_IO_read_base", fp_src->_IO_read_base);
30         ptr("src_IO_write_ptr", fp_src->_IO_write_ptr);
31         ptr("_IO_write_base", fp_src->_IO_write_base);
32         ptr("_IO_write_end", fp_src->_IO_write_end);
33         ptr("_IO_buffer_base\t", fp_src->_IO_buf_base);
34         ptr("_IO_buffer_end\t", fp_src->_IO_buf_end);
35         memset(buffer, '\0', 10);
36         i = fread(buffer, 1, 10, fp_src);
37         fwrite(buffer, 1, i, fp_des);
38         printf("i's value is :%d\n", i);
39         ptr("des_IO_read_ptr", fp_des->_IO_read_ptr);
40         ptr("des_IO_write_ptr", fp_des->_IO_write_ptr);
41     }while(i == 10);
42     fclose(fp_src);
43     fclose(fp_des);
44 }

因为不太解决BUFFER方面设置和FWRITE()和FOPEN()的参数,

特意加了I值输出,以便更好领会。。

LINUX ANSI C库函数FOPEN()的文件流指针结构体访问代码

上一篇:参加Windows7深圳社区发布会


下一篇:An error message cannot be displayed because an optional resource assembly containing it cannot be found 异常问题处理