免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
论坛 程序设计 C/C++ !
12
最近访问板块 发新帖
楼主: superdoctor
打印 上一主题 下一主题

! [复制链接]

论坛徽章:
0
11 [报告]
发表于 2003-08-13 12:49 |只看该作者

如何用c将一个文件从一个目录拷到另一个目录去?

用c编译通过在unix 下
#include <stdio.h>;

void main(void)
{
   FILE *fp=NULL;
   FILE *newfile=NULL;
   char outinfo[128];
   char filename[32];
   char *address=NULL;
   char info[128];

   fp = fopen("a.txt","r";
   if(fp==NULL)
   {
      printf("error open file\n";
      exit(-1);
    }
   fgets(outinfo,sizeof(outinfo),fp);
   fclose(fp);
   printf("read out info =%s\n",outinfo);
   strcpy(info,outinfo);

  address = strstr(outinfo,"=";
  if(address == NULL)
  {
    printf("no find file name \n";
    exit(0);
  }
  sprintf(filename,"%s",address+1);


   info[strlen(outinfo)-strlen(filename)-1]= '\0';/*取到=之前的内容*/
   printf("before = string is =%s\n",info);

   if(filename[strlen(filename)-1]=='\n')  /*将回车符去掉*/
   filename[strlen(filename)-1]= '\0';
   printf("get file name is=%s\n",filename);
   newfile = fopen(filename,"w";
   if(newfile == NULL)
   {
      printf("cannot create file \n";
      exit(-1);
    }
   fputs(info,newfile);
   fclose(newfile);
}

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
12 [报告]
发表于 2003-08-13 13:05 |只看该作者

如何用c将一个文件从一个目录拷到另一个目录去?

楼上几位购热情了

论坛徽章:
0
13 [报告]
发表于 2003-08-13 13:39 |只看该作者

如何用c将一个文件从一个目录拷到另一个目录去?

偶也来凑个热闹,各位大哥可不要笑话小弟的程序写的丑喔.


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

  5. int main(int argc, char* argv[])
  6. {
  7.         int srcfd, destfd;
  8.         char content[128];
  9.         char* ptr;

  10.         if (argc != 2) {
  11.                 fprintf(stderr, "Usage: %s [source file]\n", argv[0]);
  12.                 exit(1);
  13.         }

  14.         srcfd = open(argv[1], O_RDONLY);
  15.         if (srcfd < 0) {
  16.                 perror("open");
  17.                 exit(1);
  18.         }

  19.         if (read(srcfd, content, 128) < 0) {
  20.                 perror("read");
  21.                 exit(2);
  22.         }
  23.         content[strlen(content) - 1] = '\0';  /* get rid of the '\n' */
  24.         ptr = strchr(content, '=');  /* get the position of the '=' */
  25.         *ptr = '\0';  /* separate the string into 2 strings */

  26.         destfd = open(++ptr, O_WRONLY | O_CREAT | O_TRUNC, 0644);
  27.         if (destfd < 0) {
  28.                 perror("creat");
  29.                 exit(3);
  30.         }

  31.         if (write(destfd, content, strlen(content)) < 0) {
  32.                 perror("write");
  33.                 exit(4);
  34.         }

  35.         close(srcfd);
  36.         close(destfd);
  37.         exit(0);
  38. }

复制代码


PS.本程序在RH8.0, GCC3.3下测试通过

论坛徽章:
0
14 [报告]
发表于 2003-08-13 14:06 |只看该作者

如何用c将一个文件从一个目录拷到另一个目录去?

上面的代码再简化一下,就可以这样写:


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

  5. int main()
  6. {
  7.         int srcfd, destfd;
  8.         char content[128];
  9.         char* ptr;

  10.         srcfd = open("file", O_RDONLY);
  11.         read(srcfd, content, 128);
  12.         content[strlen(content) - 1] = '\0'; /* get rid of the '\n' */
  13.         ptr = strchr(content, '='); /* get the position of the '=' */
  14.         *ptr = '\0'; /* separate the string into 2 strings */

  15.         destfd = open(++ptr, O_WRONLY | O_CREAT | O_TRUNC, 0644);
  16.         write(destfd, content, strlen(content));

  17.         exit(0);
  18. }
复制代码


是不是很简单?嘻嘻

论坛徽章:
0
15 [报告]
发表于 2003-08-13 22:43 |只看该作者

如何用c将一个文件从一个目录拷到另一个目录去?

晕!!!
有人写代码不重视健壮性,还自鸣得意!

论坛徽章:
0
16 [报告]
发表于 2003-08-13 23:17 |只看该作者

如何用c将一个文件从一个目录拷到另一个目录去?

没想到各位为小弟的一个浅薄之题如此慷慨相助,真让人感怀莫名啊
Chinaunix 论坛 程序设计 C/C++ !
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP