Chinaunix

标题: 请求一个文件的操作办法 [打印本页]

作者: 春华    时间: 2003-04-14 21:59
标题: 请求一个文件的操作办法
打开有一个文件,内容是:1234567890
假如我们输入1,文件就变成:2345678901
假如输入2,文件就变成:3456789012
请问要怎么设计算法?
请帮帮我。
作者: wangz    时间: 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
作者: mgw    时间: 2003-04-15 08:51
标题: 请求一个文件的操作办法
请问  j = b - 0x30中的0x30代表什么??
作者: wangz    时间: 2003-04-15 08:58
标题: 请求一个文件的操作办法
0的ascii码是0x30( 十进制48 )
作者: 春华    时间: 2003-04-15 22:19
标题: 请求一个文件的操作办法
谢谢大哥.但我们如果不知道文件有多长.那要怎么办?
作者: wangz    时间: 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 */
作者: kinghood    时间: 2003-04-16 08:24
标题: 请求一个文件的操作办法
有空看看unix高级编程吧
作者: 春华    时间: 2003-04-16 20:02
标题: 请求一个文件的操作办法
因为我想将任意一个文件打开后,重新排列,就当是加密.
我学习C是自己看的书,有好多地方不是很明白.
有时候连问问题也问不清楚.
请大哥们见谅,多帮我.
十分感谢大哥们.
作者: 54sp    时间: 2003-04-16 22:52
标题: 请求一个文件的操作办法
先自己看书看能不能解决!老问虽然可以解决问题,但终究不是办法!




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2