免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1836 | 回复: 2
打印 上一主题 下一主题

请求一个文件的操作办法 [复制链接]

论坛徽章:
0
1 [报告]
发表于 2003-04-15 08:33 |显示全部楼层

请求一个文件的操作办法


  1. #include <sys/types.h>;
  2. #include <stdio.h>;
  3. main()
  4. {
  5. char a[11], b;
  6. char c[11];
  7. int i,j,k;
  8. FILE *fp;

  9.         if( !( fp = fopen( "aaaa", "r" )))
  10.         {
  11.                 printf("open file error!\n");
  12.         }
  13.         fgets( a, 256, fp );
  14.         while(1)
  15.         {
  16.                 b = getchar();
  17.                 if (b == 'Q' || b == 'q')
  18.                 {
  19.                         fclose(fp);
  20.                         exit(0);
  21.                 }
  22.                 j = b - 0x30;
  23.                 if ((j >;= 0 ) && (j <= 9)) break;
  24.         }
  25.         if ( j == 0 ) j = 10;
  26.         for( i = j, k = 0; k < 10; i ++, k ++)
  27.         {
  28.                 c[k] = a[i%10];
  29.         }
  30.         c[10] = 0;
  31.         printf("%s\n", c);
  32.         fclose(fp);
  33. }
复制代码

结果:
1:2345678901
2:3456789012
...
9:0123456789
0:1234567890

论坛徽章:
0
2 [报告]
发表于 2003-04-15 08:58 |显示全部楼层

请求一个文件的操作办法

0的ascii码是0x30( 十进制48 )

论坛徽章:
0
3 [报告]
发表于 2003-04-16 08:20 |显示全部楼层

请求一个文件的操作办法

文件是多行的还是一行?能把需求说的具体些吗?
如果只想知道文件状态,可以用stst()函数取。

#include <sys/types.h>; #include <sys/stat.h>;

int stat(const char *path, struct stat *buf);

dev_t    st_dev;       /* ID of device containing a */
                       /* directory entry for this file */
ino_t    st_ino;       /* Inode number */
ushort   st_fstype;    /* Type of filesystem this file  */
                       /* is in; see sysfs(2) */
ushort   st_mode;      /* File type, attributes, and */
                       /* access control summary */
ushort   st_basemode   /* Permission bits (see chmod(1)) */
ushort   st_nlink;     /* Number of links */
uid_t    st_uid;       /* User ID of file owner */
gid_t    st_gid;       /* Group ID of file group */
dev_t    st_rdev;      /* Device ID; this entry defined */
                       /* only for char or blk spec files */
off_t    st_size;      /* File size (bytes) */
time_t   st_atime;     /* Time of last access */
time_t   st_mtime;     /* Last modification time */
time_t   st_ctime;     /* Last file status change time */
                       /* Measured in secs since */
                       /* 00:00:00 GMT, Jan 1, 1970 */
long     st_blksize;   /* File system block size */
uint     st_acl:1;     /* Set if the file has optional */
                       /* access control list entries */
                       /* HFS File Systems only */
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP