Chinaunix首页 | 论坛 | 博客
  • 博客访问: 633182
  • 博文数量: 79
  • 博客积分: 2616
  • 博客等级: 少校
  • 技术积分: 1036
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-28 17:41
个人简介

苏北下邳附近人氏, 跟项羽、刘邦老乡,吕布很不幸,死在俺家门口那块小麦田上。 爱好家乡的小麦煎饼、盐豆子! 新浪微博:@dodolovely

文章分类

全部博文(79)

文章存档

2013年(2)

2012年(67)

2011年(1)

2010年(9)

分类: C/C++

2012-08-01 10:57:19


 

示例: fflush.c


点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. int writefile(char *filename, char *buf, int length)
  6. {
  7.     FILE *fp;
  8.     int j = 0;
  9.     size_t i = 0;
  10.     fp = fopen(filename, "w");
  11.     if (fp == NULL) {
  12.         printf("\topen file error!\n");
  13.         return -1;
  14.     } else {
  15.         while (length != 0) {
  16.             i = fwrite(&buf[j], 1, 1, fp);
  17.             length--;
  18.             j ;
  19.         }
  20.     }
  21.     fflush(fp);
  22.     fclose(fp);
  23.     return 0;
  24. }
  25.  
  26. int readfile(char *filename, char *buf)
  27. {
  28.     FILE *fp;
  29.     int j = 0;
  30.     size_t i;
  31.     fp = fopen(filename, "rb");
  32.     if (fp == NULL) {
  33.         printf("error\n");
  34.         return -1;
  35.     } else {
  36.         while (1) {
  37.             i = fread(&buf[j], 1, 1, fp);
  38.             if (i == 0)
  39.                 break;
  40.             j ;
  41.         }
  42.     }
  43.     fflush(fp);
  44.     fclose(fp);
  45.     return 111;
  46. }
  47. int main(int argc, char **argv)
  48. {
  49.     char msg[128] = "hello,xiong feng!";
  50.     char fbuf[1024] = "";
  51.     char *filename = "/xiongfeng";
  52.     int w_ret = 0;
  53.     int r_ret = 0;
  54.  
  55.     w_ret = writefile(filename, msg, strlen(msg));
  56.     if (w_ret < 0) {
  57.         printf("Write file %s fail!\n", filename);
  58.     }
  59.     printf("msg:%s\n", msg); // 如果在未加入fflush(fp), 该输出有可能不能正常输出,特别是在perl调用该程序时, perl不能获取该程序中的输出
  60.  
  61.     r_ret = readfile(filename, fbuf);
  62.     if (r_ret < 0) {
  63.         printf("Read file %s fail!\n", filename);
  64.     }
  65.     printf("fbuf:%s\n", fbuf); // 这行同上面的: printf("msg:%s\n", msg);
  66.     return 0;
  67. }


网上还有二次封装的fflush的函数, 如下:


点击(此处)折叠或打开

  1. void flush(FILE *stream)
  2. {
  3.      int duphandle;

  4.      /* flush the stream's internal buffer */
  5.      fflush(stream);

  6.      /* make a duplicate file handle */
  7.      duphandle = dup(fileno(stream));

  8.      /* close the duplicate handle to flush/
  9.         the DOS buffer */
  10.      close(duphandle);
  11. }


下面是这两个函数的分析:


点击(此处)折叠或打开

  1. dup():
  2. int dup(int handle);
  3. 作用:复制一个文件句柄


fileno():


示例:fileno.c:


点击(此处)折叠或打开

  1. #include <stdio.h>
  2. int main(int argc, char **argv)
  3. {
  4.     FILE *fp;
  5.     int fd;
  6.     fp = fopen("/xiongfeng", "r");
  7.     fd = fileno(fp);
  8.     printf("fd = %d\n", fd);
  9.     fclose(fp);
  10.     return 0;
  11. }
  12. 运行结果: fd = 3


阅读(1878) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~