Chinaunix

标题: ! [打印本页]

作者: superdoctor    时间: 2003-08-11 23:00
标题: !
我知道在win下有API copyfile可以完成,在linux下呢?

另外我想用c在linux下做一个程序实现以下功能:
    1.能打开目录1下的一个文件a.txt,a.txt里原来的内容为:  
    1234=b.txt
      2.用程序实现读出=后面的内容做为文件名建立一个新文件。即建立b.txt文件,新建的文件内容为a.txt里=前面的内容,即1234(也是用程序读出a.txt里=前面的内容)

[ 本帖最后由 superdoctor 于 2007-3-15 15:29 编辑 ]
作者: superdoctor    时间: 2003-08-12 14:31
标题: 如何用c将一个文件从一个目录拷到另一个目录去?
郁闷
作者: qjlemon    时间: 2003-08-12 14:35
标题: 如何用c将一个文件从一个目录拷到另一个目录去?
这种问题还是先看一看C的教材吧!
基本的文件操作:fopen fgets
字符串操作:strtok 或者直接操作数组
这些东西还要别人给你写出来吗?自己正好练练手嘛!
作者: superdoctor    时间: 2003-08-12 14:44
标题: 如何用c将一个文件从一个目录拷到另一个目录去?
我现在处在一个封闭的盒子里面,没有机会接触到那些书,能将代码写给我吗?万分感激,楼上的请就我一命
作者: 蓝色键盘    时间: 2003-08-12 14:51
标题: 如何用c将一个文件从一个目录拷到另一个目录去?
到网上找找。

姑姑啊
作者: superdoctor    时间: 2003-08-12 14:58
标题: 如何用c将一个文件从一个目录拷到另一个目录去?
无奈资质浅薄,没有找到合适的,为什么不能写一个给我呢?不是说入门级问题吗?
   这样没有解答问题仅仅灌水的帖子我们能变变吗?
作者: qjlemon    时间: 2003-08-12 15:08
标题: 如何用c将一个文件从一个目录拷到另一个目录去?
首先偶没有义务为你写代码,偶只给自己和雇偶的老板写代码。
char buf[1000];
FILE * fp_a, *fp_b;

fgets(buf, sizeof(buf), fp_a);
fputs(fp_b, strtok(buf, "=");
作者: kj501    时间: 2003-08-12 19:51
标题: 如何用c将一个文件从一个目录拷到另一个目录去?
我来凑热闹,写了个C++的,算是练练手吧。

  1. #include <iostream>;
  2. #include <fstream>;
  3. #include <string>;

  4. using namespace std;

  5. int main()
  6. {
  7.     string filename;
  8.     cout << "please input the filename:";
  9.     cin >;>; filename;
  10.    
  11.     ifstream in(filename.c_str());
  12.     if (!in) {
  13.         cout << "can't open file " << filename << endl;
  14.         exit(1);
  15.     }
  16.     // 读入文件的每一行,将'='后面的部分当成文件名,'='前面
  17.     //  的部分成为文件内容。
  18.     string s;
  19.     while (getline(in, s)) {
  20.         string::size_type idx = s.find('=');
  21.         //如果这一行中没有'=',则给出提示并继续检查下一行。
  22.         if(idx == string::npos) {         
  23.                 cout << "can't find \"=\" in " << s << endl;
  24.                 continue;
  25.         }
  26.         else {
  27.                 string outFile = s.substr(idx+1);
  28.                 string outFileContent = s.substr(0, idx);
  29.                 ofstream out(outFile.c_str());
  30.                 out << outFileContent;
  31.                 out.close();
  32.         }
  33.     }
  34. }
复制代码

程序在win98和dev-c++下编译通过。
测试时发现楼主给出的例子"1234=b.txt"中的'='是全角字符,估计是用中文输入法造成的,实际的文件应该用的是半角字符。所以程序中是按照半角字符来分隔字符串。
作者: superdoctor    时间: 2003-08-12 20:50
标题: 如何用c将一个文件从一个目录拷到另一个目录去?
太感谢了,我的生活从此充满了阳光,现在终于知道----世上还是好人多啊
作者: bsj2100a    时间: 2003-08-12 22:27
标题: 如何用c将一个文件从一个目录拷到另一个目录去?
NO namespace
Use 'cout' once and 'get' twice ,all is ok

passed in solaris 8
作者: yuxq    时间: 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);
}
作者: 蓝色键盘    时间: 2003-08-13 13:05
标题: 如何用c将一个文件从一个目录拷到另一个目录去?
楼上几位购热情了
作者: Joran    时间: 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下测试通过
作者: Joran    时间: 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. }
复制代码


是不是很简单?嘻嘻
作者: kj501    时间: 2003-08-13 22:43
标题: 如何用c将一个文件从一个目录拷到另一个目录去?
晕!!!
有人写代码不重视健壮性,还自鸣得意!
作者: superdoctor    时间: 2003-08-13 23:17
标题: 如何用c将一个文件从一个目录拷到另一个目录去?
没想到各位为小弟的一个浅薄之题如此慷慨相助,真让人感怀莫名啊




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