免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12
最近访问板块 发新帖
楼主: wzg_8041
打印 上一主题 下一主题

判断文件大小 [复制链接]

论坛徽章:
8
白羊座
日期:2015-01-21 18:35:03巳蛇
日期:2015-02-03 17:30:37处女座
日期:2015-02-03 17:31:02羊年新春福章
日期:2015-02-03 17:31:21巨蟹座
日期:2015-02-05 16:01:06申猴
日期:2015-02-05 16:01:31摩羯座
日期:2015-02-05 16:01:41酉鸡
日期:2015-02-05 16:02:37
11 [报告]
发表于 2005-10-10 17:05 |只看该作者

判断文件大小


  1. #include <sys\stat.h>;
  2. #include <stdio.h>;
  3. #include <time.h>;

  4. #define FILENAME "TEST.$$$"

  5. int main(void)
  6. {
  7.    struct stat statbuf;
  8.    FILE *stream;

  9.    /* open a file for update */
  10.    if ((stream = fopen(FILENAME, "w+")) == NULL)
  11.    {
  12.       fprintf(stderr, "Cannot open output file.\n");
  13.       return(1);
  14.    }

  15.    /* get information about the file */
  16.    stat(FILENAME, &statbuf);

  17.    fclose(stream);

  18.    /* display the information returned */
  19.    if (statbuf.st_mode & S_IFCHR)

  20.       printf("Handle refers to a device.\n");
  21.    if (statbuf.st_mode & S_IFREG)
  22.       printf("Handle refers to an ordinary file.\n");
  23.    if (statbuf.st_mode & S_IREAD)
  24.       printf("User has read permission on file.\n");
  25.    if (statbuf.st_mode & S_IWRITE)
  26.       printf("User has write permission on file.\n");

  27.    printf("Drive letter of file: %c\n", 'A'+statbuf.st_dev);
  28.    printf("Size of file in bytes: %ld\n", statbuf.st_size);
  29.    printf("Time file last opened: %s\n", ctime(&statbuf.st_ctime));

  30.    return 0;
  31. }
复制代码

论坛徽章:
8
白羊座
日期:2015-01-21 18:35:03巳蛇
日期:2015-02-03 17:30:37处女座
日期:2015-02-03 17:31:02羊年新春福章
日期:2015-02-03 17:31:21巨蟹座
日期:2015-02-05 16:01:06申猴
日期:2015-02-05 16:01:31摩羯座
日期:2015-02-05 16:01:41酉鸡
日期:2015-02-05 16:02:37
12 [报告]
发表于 2005-10-10 17:06 |只看该作者

判断文件大小

当然,fstat、lseek也可以:

  1. #include <sys\stat.h>;
  2. #include <string.h>;
  3. #include <stdio.h>;
  4. #include <fcntl.h>;
  5. #include <io.h>;

  6. int main(void)
  7. {
  8.     int handle;
  9.     char msg[] = "This is a test";
  10.     char ch;

  11.     /* create a file */
  12.     handle = open("TEST.$$$", O_CREAT | O_RDWR, S_IREAD | S_IWRITE);

  13.     /* write some data to the file */
  14.     write(handle, msg, strlen(msg));

  15.     /* seek to the beginning of the file */
  16.     lseek(handle, 0L, SEEK_SET);

  17.     /* reads chars from the file until we hit EOF */

  18.     do
  19.     {
  20.        read(handle, &ch, 1);
  21.        printf("%c", ch);
  22.     }  while (!eof(handle));

  23.     close(handle);
  24.     return 0;
  25. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP