LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 2411|回复: 10

Linux下C语言编程--文件的操作(源代码)

[复制链接]
发表于 2003-6-7 10:19:01 | 显示全部楼层 |阅读模式
昨晚通宵到早上五点做好的
学lINUX以来写的第一个小程序
大家有什么意见尽管说,什么都可以说,
小弟感激不尽


  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. #include<fcntl.h>
  5. #include<unistd.h>
  6. #include<sys/stat.h>
  7. #include<sys/dir.h>
  8. #include<dirent.h>
  9. #include <sys/types.h>
  10. #define s 100
  11. main()
  12. {
  13.   char copybuf[s],pastebuf[s],copyname[s];
  14.    int len=0,tag=0,cord,i;
  15.   void treedisplay();
  16.   void treecreat();
  17.   void filecreatopen();
  18.   void fileread(char pathname[]);
  19.   void filewrite(char pathname[]);
  20.   void treeopen();
  21.   void treeback();
  22.   long treefilecopy(char copybuf[],char copyname[]);
  23.   void treefilepaste(int len,char pastebuf[],char copyname[]);
  24.   void treedelete();
  25.   void filedelete();
  26.   void treefileproperty();
  27.   void displayproperty();
  28.   void modifypurview(char pathname[]);
  29.   void treefilerename();
  30.   void changesize();
  31.   
  32.   do {printf("\n          mainmenu         \n");
  33.       printf("    1     tree-display       \n");/*显示目录*/
  34.       printf("    2     tree-creat         \n");/*目录创建*/
  35.       printf("    3     file-creat/open    \n");/*打开文件进行读写*/
  36.       printf("    4     tree-open          \n");/*改变当前目录*/
  37.       printf("    5     tree-back          \n");/*返回上一级目录*/
  38.       printf("    6     tree/file-copy     \n");
  39.       printf("    7     tree/file-paste    \n");
  40.       printf("    8     tree-delete        \n");
  41.       printf("    9     file-delete        \n");
  42.       printf("   10     tree/file-property \n");/*文件属性*/
  43.       printf("   11     end                \n");
  44.       printf("------------------------------\n");
  45.       printf("input your choice(1-11)");
  46.       scanf("%d",&cord);
  47.       switch(cord)
  48.         {case 1:{treedisplay();
  49.                 }break;
  50.          case 2:{treecreat();
  51.                 }break;
  52.          case 3:{filecreatopen();
  53.                 }break;
  54.          case 4:{treeopen();
  55.                 }break;
  56.          case 5:{treeback();
  57.                 }break;
  58.          case 6:{len=treefilecopy(copybuf,copyname); tag=1;
  59.                  for(i=0;i<len;i++)pastebuf[i]=copybuf[i];
  60.                 }break;
  61.          case 7:{if(tag&&len)treefilepaste(len,pastebuf,copyname);
  62.                  else   printf("paste failed\n");
  63.                 }break;
  64.          case 8:{treedelete();
  65.                 }break;
  66.          case 9:{filedelete();
  67.                 }break;
  68.          case 10:{treefileproperty();
  69.                 }break;
  70.          case 11:{system("clear");exit(0);
  71.                 }break;
  72.         }
  73.     }while(cord<=11);
  74. }

  75. void treedisplay()
  76. {
  77. DIR *dir;char buf[s];
  78. struct dirent *ent;
  79. getcwd(buf,s);
  80. dir=opendir(buf);
  81. while((ent=readdir(dir))!=NULL)
  82. puts(ent->d_name);
  83. closedir(dir);
  84. }

  85. void treecreat()
  86. {
  87. int fd;char pathname[s];
  88. do{
  89. printf("new tree=");scanf("%s",pathname);
  90. fd=mkdir(pathname,O_CREAT);
  91. if(fd==-1)
  92.         perror("creat failed,try again,the error");
  93. else
  94.         {chmod(pathname,0777);
  95.         printf("creat success!\n");
  96.         }
  97. }while(fd==-1);
  98. }

  99. void filecreatopen()
  100. {
  101. int fd;char ch,choose,pathname[s];
  102. printf("new/open file=");scanf("%s",pathname);
  103. fd=open(pathname,O_CREAT|O_APPEND|O_RDWR);
  104. if(fd==-1)
  105.         {perror("filecreat/open failed,try again,the error");
  106.         close(fd);return;
  107.         }
  108. else
  109.         {chmod(pathname,0777);
  110.         printf("creat/open success!\n");
  111.         fileread(pathname);
  112.         }
  113. printf("\ninput the text?(y/n)");
  114. scanf("%c",&choose);
  115. scanf("%c",&choose);

  116. if(choose=='y')filewrite(pathname);
  117. fileread(pathname);
  118. close(fd);
  119. }

  120. void fileread(char pathname[])
  121. {
  122. char ch;FILE *fp;
  123. fp=fopen(pathname,"r");
  124. ch=fgetc(fp);
  125. while(ch!=EOF)
  126.         {putchar(ch);
  127.         ch=fgetc(fp);
  128.         }
  129. fclose(fp);
  130. }

  131. void filewrite(char pathname[])
  132. {
  133. char ch;FILE *fp;long where;
  134. fp=fopen(pathname,"rb+");
  135. printf("input the where of write=");
  136. scanf("%ld",&where);
  137. fseek(fp,where,SEEK_SET);
  138. printf("'#' for the EOF,begin:\n");
  139. ch=getchar();
  140. while(ch!='#')
  141.           {
  142.           fwrite(&ch,1,1,fp);
  143.         ch=getchar();
  144.         }
  145. fclose(fp);
  146. }

  147. void treeopen()
  148. {
  149. int fd;char pathname[s],buf[s];
  150. system("ls -l");
  151. printf("open tree=");scanf("%s",pathname);
  152. fd=chdir(pathname);
  153. if(fd==-1)
  154.         {perror("treeopen failed,try again,the error");
  155.          close(fd);return;
  156.         }
  157. else     {
  158.          system("ls -l");
  159.          getcwd(buf,s);
  160.          printf("%s\n",buf);
  161.          printf("treeopen success!\n");
  162.          }
  163. }

  164. void treeback()
  165. {
  166. char buf[s];
  167. chdir("..");
  168. system("ls -l");
  169. getcwd(buf,s);
  170. printf("%s\n",buf);
  171. }

  172. long treefilecopy(char copybuf[],char copyname[])
  173. {
  174. struct stat buf;int fd;
  175. long  size;/*off_t*/
  176. system("ls -l");
  177. printf("copy file/tree=");scanf("%s",copyname);
  178. fd=open(copyname,O_RDONLY);
  179. if(fd==-1)
  180.         {perror("failed,try again,the error");
  181.          return 0;
  182.         }
  183. stat(copyname,&buf);
  184. size=buf.st_size;
  185. read(fd,copybuf,size);
  186. return  size;
  187. }

  188. void treefilepaste(int len,char pastebuf[],char copyname[])
  189. {int fd;char pastename[]={"re-"};
  190. strcat(pastename,copyname);
  191. fd=open(pastename,O_CREAT|O_APPEND|O_RDWR);
  192. if(fd==-1)
  193.         {perror("failed,try again,the error");
  194.          return;
  195.         }
  196. write(fd,pastebuf,len);
  197. system("ls -l");
  198. }

  199. void treedelete()
  200. {
  201. int fd;char pathname[s];
  202. system("ls -l");
  203. printf("delete tree=");scanf("%s",pathname);
  204. fd=rmdir(pathname);
  205. if(fd==-1)
  206.         {perror("treedelete failed,try again,the error");
  207.          return;
  208.         }
  209. else     {
  210.          system("ls -l");
  211.          printf("treedelete success!\n");
  212.          }
  213. }

  214. void filedelete()
  215. {
  216. int fd;char pathname[s];
  217. system("ls -l");
  218. printf("delete file=");scanf("%s",pathname);
  219. fd=unlink(pathname);
  220. if(fd==-1)
  221.         {perror("filedelete failed,try again,the error");
  222.          return;
  223.         }
  224. else     {
  225.          system("ls -l");
  226.          printf("filedelete success!\n");
  227.          }
  228. }

  229. void treefileproperty()
  230. {int cord;char choose;
  231. do { printf("\n          treefileproperty \n");
  232.       printf("    1     display-property   \n");
  233.       printf("    2     tree/file-rename   \n");
  234.       printf("    3     file-chsize        \n");
  235.       printf("    4     back mainmenu      \n");
  236.       printf("------------------------------\n");
  237.       printf("input your choice(1-4)");
  238.       scanf("%d",&cord);
  239.       switch(cord)
  240.         {case 1:{displayproperty();
  241.                 }break;
  242.          case 2:{treefilerename();
  243.                 }break;
  244.             case 3:{changesize();
  245.                 }break;
  246.          case 4:{system("clear");return;
  247.                 }break;
  248.             }
  249.     }while(cord<=3);
  250. }

  251. void displayproperty()
  252. {
  253. struct stat buf;
  254. mode_t mode;
  255. int fd;char pathname[s],choose;
  256. system("ls -l");
  257. printf("file/tree=");scanf("%s",pathname);
  258. fd=open(pathname,O_RDWR);
  259. if(fd==-1)
  260.         {perror("failed,try again,the error");
  261.          return;
  262.         }
  263. stat(pathname,&buf);
  264. mode=buf.st_mode;
  265. printf("%-10s","mode:");
  266. printf("%10s","read");printf("%10s","write");
  267. printf("%10s","execute\n");printf("%10s","all user");
  268. if((mode&0400)==S_IRUSR)printf("%10s","true");
  269. else printf("%10s","false");
  270. if((mode&0200)==S_IWUSR)printf("%10s","true");
  271. else printf("%10s","false");
  272. if((mode&0100)==S_IXUSR)printf("%10s","true\n");
  273. else printf("%10s","false\n");
  274. printf("%10s","group");
  275. if((mode&040)==S_IRGRP)printf("%10s","true");
  276. else printf("%10s","false");
  277. if((mode&020)==S_IWGRP)printf("%10s","true");
  278. else printf("%10s","false");
  279. if((mode&010)==S_IXGRP)printf("%10s","true\n");
  280. else printf("%10s","false\n");
  281. printf("%10s","other user");
  282. if((mode&04)==S_IROTH)printf("%10s","true");
  283. else printf("%10s","false");
  284. if((mode&02)==S_IWOTH)printf("%10s","true");
  285. else printf("%10s","false");
  286. if((mode&01)==S_IXOTH)printf("%10s","true\n");
  287. else printf("%10s","false\n");
  288. printf("size:%ld\n",buf.st_size);
  289. printf("BLK size:%ld\n",buf.st_blksize);

  290. printf("modify the purview?(y/n)");
  291. scanf("%c",&choose);scanf("%c",&choose);
  292. if(choose=='y')modifypurview(pathname);
  293. }

  294. void modifypurview(char pathname[])
  295. {
  296. char choose;int c=0;
  297. printf("all user read?(y/n)");
  298. scanf("%c",&choose);scanf("%c",&choose);
  299. if(choose=='y'){c=c+0400;chmod(pathname,c);}
  300. printf("all user write?(y/n)");
  301. scanf("%c",&choose);scanf("%c",&choose);
  302. if(choose=='y'){c=c+0200;chmod(pathname,c);}
  303. printf("all user execute?(y/n)");
  304. scanf("%c",&choose);scanf("%c",&choose);
  305. if(choose=='y'){c=c+0100;chmod(pathname,c);}
  306. printf("group read?(y/n)");
  307. scanf("%c",&choose);scanf("%c",&choose);
  308. if(choose=='y'){c=c+040;chmod(pathname,c);}
  309. printf("group write?(y/n)");
  310. scanf("%c",&choose);scanf("%c",&choose);
  311. if(choose=='y'){c=c+020;chmod(pathname,c);}
  312. printf("group execute?(y/n)");
  313. scanf("%c",&choose);scanf("%c",&choose);
  314. if(choose=='y'){c=c+010;chmod(pathname,c);}
  315. printf("other user read?(y/n)");
  316. scanf("%c",&choose);scanf("%c",&choose);
  317. if(choose=='y'){c=c+04;chmod(pathname,c);}
  318. printf("other user write?(y/n)");
  319. scanf("%c",&choose);scanf("%c",&choose);
  320. if(choose=='y'){c=c+02;chmod(pathname,c);}
  321. printf("other user execute?(y/n)");
  322. scanf("%c",&choose);scanf("%c",&choose);
  323. if(choose=='y'){c=c+01;chmod(pathname,c);}
  324. if(!c)chmod(pathname,c);
  325. displayproperty();
  326. }

  327. void treefilerename()
  328. {
  329. char oldname[s],newname[s];int fd;
  330. system("ls -l");
  331. printf("old name=");scanf("%s",oldname);
  332. printf("new name=");scanf("%s",newname);
  333. fd=rename(oldname,newname);
  334. if(fd==-1)
  335.         {perror("rename failed,try again,the error");
  336.         return;
  337.         }
  338. else     {
  339.          system("ls -l");
  340.          printf("rename success!\n");
  341.          }
  342. }


  343. void changesize()
  344. {  int fd,size;char pathname[s];
  345.    printf("change file=");scanf("%s",pathname);
  346.    printf("change size=");scanf("%d",&size);
  347.    fd=truncate(pathname, size);
  348.    if(fd==-1)perror("failed,try again,the error");
  349.    else    printf("success!\n");
  350. }
复制代码
 楼主| 发表于 2003-6-7 11:01:53 | 显示全部楼层

问几个基础的问题

用编译器能编译成功,可是它有很多警告,
怎么办?有影响吗?

下面是上面那个源程序的警告
chen.c:126: warning: type mismatch with previous implicit declaration
chen.c:121: warning: previous implicit declaration of `fileread'
chen.c:126: warning: `fileread' was previously implicitly declared to return `int'
chen.c:138: warning: type mismatch with previous implicit declaration
chen.c:120: warning: previous implicit declaration of `filewrite'
chen.c:138: warning: `filewrite' was previously implicitly declared to return `int'
chen.c:268: warning: type mismatch with previous implicit declaration
chen.c:255: warning: previous implicit declaration of `displayproperty'
chen.c:268: warning: `displayproperty' was previously implicitly declared to return `int'
chen.c:313: warning: type mismatch with previous implicit declaration
chen.c:309: warning: previous implicit declaration of `modifypurview'
chen.c:313: warning: `modifypurview' was previously implicitly declared to return `int'
chen.c:347: warning: type mismatch with previous implicit declaration
chen.c:257: warning: previous implicit declaration of `treefilerename'
chen.c:347: warning: `treefilerename' was previously implicitly declared to return `int'
chen.c:365: warning: type mismatch with previous implicit declaration
chen.c:259: warning: previous implicit declaration of `changesize'
chen.c:365: warning: `changesize' was previously implicitly declared to return `int'
 楼主| 发表于 2003-6-7 11:02:54 | 显示全部楼层
谁能帮我解释那些警告?

我英文不好
发表于 2003-6-7 11:09:19 | 显示全部楼层

没有影响

你把函数声明放的main()以外就没有这些警告了
 楼主| 发表于 2003-6-7 12:16:44 | 显示全部楼层

回复: 没有影响

最初由 colored 发表
你把函数声明放的main()以外就没有这些警告了


谢谢

我刚学LINUX
给我的源程序点意见啊
发表于 2003-6-7 16:45:17 | 显示全部楼层
代码的可读性好差!

反正我不喜欢看这种乱代码.要写的清楚,多分几行.每个函数分开来.
还有最好用模块把它们分开来!

你看你函数的原形都在main中.去 看看C99比较好!
发表于 2003-6-7 16:49:31 | 显示全部楼层
还请问楼主在LINUX下用的是集成开发环境还是GCC什么?

我们可以做好朋友,我是想搞LINUX下的C/C++编程的!有精力搞搞数据库开发!我用的是ANJUTA,说实话,还是新手对于LINUX,对编程还不能算什么水平!希望以后多交流!
 楼主| 发表于 2003-6-7 18:11:45 | 显示全部楼层
最初由 Andy84920 发表
代码的可读性好差!

反正我不喜欢看这种乱代码.要写的清楚,多分几行.每个函数分开来.
还有最好用模块把它们分开来!

你看你函数的原形都在main中.去 看看C99比较好!


谢谢
 楼主| 发表于 2003-6-7 18:12:34 | 显示全部楼层
最初由 Andy84920 发表
还请问楼主在LINUX下用的是集成开发环境还是GCC什么?

我们可以做好朋友,我是想搞LINUX下的C/C++编程的!有精力搞搞数据库开发!我用的是ANJUTA,说实话,还是新手对于LINUX,对编程还不能算什么水平!希望以后多交流!


我也刚学啊
发表于 2003-6-7 18:32:55 | 显示全部楼层
我把你的代码把格式给调了一下.
有些我不懂.有些你的实现起来真的不好.
你看你一般用两个scanf("%c",&ch);这样的语句来处理一个字符与一个回车字符是吧?你还不来个函数来处理就行.
我会仔细分析一下你的程序的.我会把我改你的程序给你看的.
有不懂的还得指教!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表