免费注册 查看新帖 |

Chinaunix

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

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

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2003-04-14 21:59 |只看该作者 |倒序浏览
打开有一个文件,内容是:1234567890
假如我们输入1,文件就变成:2345678901
假如输入2,文件就变成:3456789012
请问要怎么设计算法?
请帮帮我。

论坛徽章:
0
2 [报告]
发表于 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
3 [报告]
发表于 2003-04-15 08:51 |只看该作者

请求一个文件的操作办法

请问  j = b - 0x30中的0x30代表什么??

论坛徽章:
0
4 [报告]
发表于 2003-04-15 08:58 |只看该作者

请求一个文件的操作办法

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

论坛徽章:
0
5 [报告]
发表于 2003-04-15 22:19 |只看该作者

请求一个文件的操作办法

谢谢大哥.但我们如果不知道文件有多长.那要怎么办?

论坛徽章:
0
6 [报告]
发表于 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 */

论坛徽章:
0
7 [报告]
发表于 2003-04-16 08:24 |只看该作者

请求一个文件的操作办法

有空看看unix高级编程吧

论坛徽章:
0
8 [报告]
发表于 2003-04-16 20:02 |只看该作者

请求一个文件的操作办法

因为我想将任意一个文件打开后,重新排列,就当是加密.
我学习C是自己看的书,有好多地方不是很明白.
有时候连问问题也问不清楚.
请大哥们见谅,多帮我.
十分感谢大哥们.

论坛徽章:
0
9 [报告]
发表于 2003-04-16 22:52 |只看该作者

请求一个文件的操作办法

先自己看书看能不能解决!老问虽然可以解决问题,但终究不是办法!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP