免费注册 查看新帖 |

Chinaunix

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

[其他] pipe copy file [复制链接]

论坛徽章:
1
2015年迎新春徽章
日期:2015-03-04 09:49:03
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-10-17 14:57 |只看该作者 |倒序浏览
本帖最后由 shihyu 于 2015-10-17 14:59 编辑
  1. #include <sys/types.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <unistd.h>

  5. #define BUFFER_SIZE 80
  6. #define READ_END 0
  7. #define WRITE_END 1

  8. int main(int argc, char *argv[]) {

  9.     /* argc must be 3 to ensure the correct usage */
  10.     if (argc != 3) {
  11.         printf("The usage of this program is:\n");
  12.         printf("FileCopy input.txt copy.txt\n");
  13.         return 1;
  14.     }

  15.     else {
  16.         int pid;
  17.         /* initialize the pipe and fork variables */
  18.         int fd[2];/* argv[0] is the program name, argv[1] and argv[2] are the file names */
  19.         char *infile = argv[1];
  20.         char *outfile = argv[2];

  21.         /* create a pipe */
  22.         if (pipe(fd) == -1) {
  23.             fprintf(stderr, "Pipe failed");
  24.             return 1;
  25.         }

  26.         /* fork */
  27.         pid = fork();

  28.         /* if pid < 0 there is an error */
  29.         if (pid < 0) {
  30.             fprintf(stderr, "Fork error");
  31.             return 1;
  32.         }

  33.         /* parent code */
  34.         if (pid > 0) {
  35.             FILE *in;
  36.             char buffer[BUFFER_SIZE];
  37.             /* close the unused end of the pipe */
  38.             close(fd[READ_END]);
  39.             /* make a buffer to store input file information */


  40.             /* open input file */
  41.             in = fopen(infile, "r");

  42.             /* make sure it's not null */
  43.             if (in == NULL) {
  44.                 fprintf(stderr, "Error opening file %s\n", infile);
  45.                 return 1;
  46.             }

  47.             /* read from the file into the pipe thru the buffer */
  48.             while (fgets(buffer, BUFFER_SIZE, in)) {
  49.                 write(fd[WRITE_END], buffer, strlen(buffer)+1);
  50.                 printf("buf1:%s\n",buffer);
  51.                 buffer[0]='\0';
  52.             }

  53.             /* close the input file */
  54.             fclose(in);
  55.             close(fd[WRITE_END]);
  56.         }

  57.         /* child process has pid == 0 */
  58.         else {
  59.             FILE *out;
  60.             /* create a buffer to read from the pipe and store into the out file */
  61.             char buffer[BUFFER_SIZE];

  62.             /* close the unused end of the pipe */
  63.             close(fd[WRITE_END]);

  64.             /* initialize the output file */

  65.             /* w+ means open a file for writing and create it if it doesn't exist */
  66.             out = fopen(outfile, "w+");

  67.             while(1) {
  68.                 if (read(fd[READ_END], buffer, BUFFER_SIZE) != 0)
  69.                 {
  70.                     fprintf(out,"%s",buffer);
  71.                     fflush(out);
  72.                 }
  73.                 else
  74.                     break;
  75.             }

  76.             /* close the file and the pipe */
  77.             fclose(out);
  78.             close(fd[READ_END]);
  79.         }
  80.     }

  81.     return 0;
  82. }
复制代码
./copyfile  aa.txt  bb.txt


aa.txt
  1. hello world
  2. 123
  3. 445
  4. 66778
  5. hhhh
复制代码
为什么代码 copy 出来的 bb.txt 只有 hello world?

谢谢

论坛徽章:
16
CU十二周年纪念徽章
日期:2013-10-24 15:41:3415-16赛季CBA联赛之广东
日期:2015-12-23 21:21:55青铜圣斗士
日期:2015-12-05 10:35:30黄金圣斗士
日期:2015-11-26 20:42:16神斗士
日期:2015-11-19 12:47:50每日论坛发贴之星
日期:2015-11-18 06:20:00程序设计版块每日发帖之星
日期:2015-11-18 06:20:002015亚冠之城南
日期:2015-11-10 19:10:492015亚冠之萨济拖拉机
日期:2015-10-28 18:47:282015亚冠之柏太阳神
日期:2015-08-30 17:21:492015亚冠之山东鲁能
日期:2015-07-07 18:48:39摩羯座
日期:2014-08-29 23:01:42
2 [报告]
发表于 2015-10-17 18:38 |只看该作者
  1. #include <sys/types.h>

  2. #include <stdio.h>

  3. #include <string.h>

  4. #include <unistd.h>


  5. #define BUFFER_SIZE 80

  6. #define READ_END 0

  7. #define WRITE_END 1


  8. int main(int argc, char *argv[]) {


  9.     /* argc must be 3 to ensure the correct usage */

  10.     if (argc != 3) {

  11.         printf("The usage of this program is:\n");

  12.         printf("FileCopy input.txt copy.txt\n");

  13.         return 1;

  14.     }


  15.     else {

  16.         int pid;

  17.         /* initialize the pipe and fork variables */

  18.         int fd[2];/* argv[0] is the program name, argv[1] and argv[2] are the file names */

  19.         char *infile = argv[1];

  20.         char *outfile = argv[2];


  21.         /* create a pipe */

  22.         if (pipe(fd) == -1) {

  23.             fprintf(stderr, "Pipe failed");

  24.             return 1;

  25.         }


  26.         /* fork */

  27.         pid = fork();


  28.         /* if pid < 0 there is an error */

  29.         if (pid < 0) {

  30.             fprintf(stderr, "Fork error");

  31.             return 1;

  32.         }


  33.         /* parent code */

  34.         if (pid > 0) {

  35.             FILE *in;

  36.             char buffer[BUFFER_SIZE];

  37.             /* close the unused end of the pipe */

  38.             close(fd[READ_END]);

  39.             /* make a buffer to store input file information */



  40.             /* open input file */

  41.             in = fopen(infile, "r");


  42.             /* make sure it's not null */

  43.             if (in == NULL) {

  44.                 fprintf(stderr, "Error opening file %s\n", infile);

  45.                 return 1;

  46.             }


  47.             /* read from the file into the pipe thru the buffer */

  48.             while (fgets(buffer, BUFFER_SIZE, in)) {

  49.                 write(fd[WRITE_END], buffer, strlen(buffer)+1);

  50.                 printf("buf1:%s\n",buffer);

  51.                 buffer[0]='\0';

  52.             }


  53.             /* close the input file */

  54.             fclose(in);

  55.             close(fd[WRITE_END]);
  56. wait(NULL);
  57.         }


  58.         /* child process has pid == 0 */

  59.         else {

  60.             FILE *out;

  61.             /* create a buffer to read from the pipe and store into the out file */

  62.             char buffer[BUFFER_SIZE];


  63.             /* close the unused end of the pipe */

  64.             close(fd[WRITE_END]);


  65.             /* initialize the output file */


  66.             /* w+ means open a file for writing and create it if it doesn't exist */

  67.             out = fopen(outfile, "w+");


  68.             while(1) {

  69.                 if (read(fd[READ_END], buffer, BUFFER_SIZE) != 0)

  70.                 {

  71.                     fprintf(out,"%s",buffer);

  72.                     fflush(out);

  73.                 }

  74.                 else

  75.                     break;

  76.             }


  77.             /* close the file and the pipe */

  78.             fclose(out);

  79.             close(fd[READ_END]);

  80.         }

  81.     }


  82.     return 0;

  83. }
复制代码

论坛徽章:
16
CU十二周年纪念徽章
日期:2013-10-24 15:41:3415-16赛季CBA联赛之广东
日期:2015-12-23 21:21:55青铜圣斗士
日期:2015-12-05 10:35:30黄金圣斗士
日期:2015-11-26 20:42:16神斗士
日期:2015-11-19 12:47:50每日论坛发贴之星
日期:2015-11-18 06:20:00程序设计版块每日发帖之星
日期:2015-11-18 06:20:002015亚冠之城南
日期:2015-11-10 19:10:492015亚冠之萨济拖拉机
日期:2015-10-28 18:47:282015亚冠之柏太阳神
日期:2015-08-30 17:21:492015亚冠之山东鲁能
日期:2015-07-07 18:48:39摩羯座
日期:2014-08-29 23:01:42
3 [报告]
发表于 2015-10-17 18:39 |只看该作者
父进程要wait一下。。。

论坛徽章:
1
2015年迎新春徽章
日期:2015-03-04 09:49:03
4 [报告]
发表于 2015-10-17 19:23 |只看该作者
本帖最后由 shihyu 于 2015-10-17 19:24 编辑

父进程 不wait  会怎样吗?
  1.         while (fgets(buffer, BUFFER_SIZE, in)) {

  2.                 write(fd[WRITE_END], buffer, strlen(buffer)+1);

  3.                 printf("buf1:%s\n",buffer);

  4.                 buffer[0]='\0';

  5.             }
复制代码
write(fd[WRITE_END], buffer, strlen(buffer)+1);

做完离开  while 还需要 wait?

谢谢

论坛徽章:
1
2015年迎新春徽章
日期:2015-03-04 09:49:03
5 [报告]
发表于 2015-10-18 01:12 |只看该作者
本帖最后由 shihyu 于 2015-10-18 01:12 编辑

父进程加上wait 结果还是不正常 xd

论坛徽章:
0
6 [报告]
发表于 2015-10-18 10:09 |只看该作者
  1. #include <sys/types.h>
  2. #include <sys/wait.h>
  3. #include <fcntl.h>

  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <unistd.h>

  7. #define BUFFER_SIZE 80
  8. #define READ_END 0
  9. #define WRITE_END 1

  10. int main(int argc, char *argv[]) {
  11.         /* argc must be 3 to ensure the correct usage */
  12.         if (argc != 3) {
  13.                 printf("The usage of this program is:\n");
  14.                 printf("FileCopy input.txt copy.txt\n");
  15.                 return 1;
  16.         }
  17.         else {
  18.                 int pid;
  19.                 /* initialize the pipe and fork variables */
  20.                 int fd[2];/* argv[0] is the program name, argv[1] and argv[2] are the file names */
  21.                 char *infile = argv[1];
  22.                 char *outfile = argv[2];
  23.                 /* create a pipe */
  24.                 if (pipe(fd) == -1) {
  25.                         fprintf(stderr, "Pipe failed");
  26.                         return 1;
  27.                 }
  28.                 /* fork */
  29.                 pid = fork();
  30.                 /* if pid < 0 there is an error */
  31.                 if (pid < 0) {
  32.                         fprintf(stderr, "Fork error");
  33.                         return 1;
  34.                 }
  35.                 /* parent code */
  36.                 if (pid > 0) {
  37.                         int  in;
  38.                         int  len;
  39.                         char buffer[BUFFER_SIZE];
  40.                         /* close the unused end of the pipe */
  41.                         close(fd[READ_END]);
  42.                         /* make a buffer to store input file information */
  43.                         /* open input file */
  44.                         in = open(infile, O_RDONLY);
  45.                         /* make sure it's not null */
  46.                         if (in == -1) {
  47.                                 fprintf(stderr, "Error opening file %s\n", infile);
  48.                                 return 1;
  49.                         }
  50.                         /* read from the file into the pipe thru the buffer */
  51.                         while ( 1 ) {
  52.                                 memset( buffer, 0, sizeof(buffer));
  53.                                 len = read(in, buffer, BUFFER_SIZE);
  54.                                 if( len <= 0 ) break;
  55.                                 write(fd[WRITE_END], buffer, len );
  56.                         }
  57.                         /* close the input file */
  58.                         close(in);
  59.                         close(fd[WRITE_END]);
  60.                         wait(NULL);
  61.                 }
  62.                 /* child process has pid == 0 */
  63.                 else {
  64.                         int out;
  65.                         int len;
  66.                         /* create a buffer to read from the pipe and store into the out file */
  67.                         char buffer[BUFFER_SIZE];
  68.                         /* close the unused end of the pipe */
  69.                         close(fd[WRITE_END]);
  70.                         /* initialize the output file */
  71.                         /* w+ means open a file for writing and create it if it doesn't exist */
  72.                         out = open(outfile, O_RDWR | O_TRUNC | O_CREAT);
  73.                         while(1) {
  74.                                 memset(buffer, 0, sizeof(buffer));
  75.                                 len = read(fd[READ_END], buffer, BUFFER_SIZE) ;
  76.                                 if (len <= 0 ) break;
  77.                                 write(out,buffer, len );
  78.                                 printf("buf1:%s\n",buffer);
  79.                         }
  80.                         /* close the file and the pipe */
  81.                         close(out);
  82.                         close(fd[READ_END]);
  83.                 }
  84.         }
  85.         return 0;
  86. }
复制代码
文件拷贝,需要试用 open(), read(), write() 低级函数,上述程序测试通过。

论坛徽章:
1
2015年迎新春徽章
日期:2015-03-04 09:49:03
7 [报告]
发表于 2015-10-18 10:41 |只看该作者
本帖最后由 shihyu 于 2015-10-18 10:41 编辑

为什么用  fopen  fprintf + fflush 不行?

论坛徽章:
1
2015年迎新春徽章
日期:2015-03-04 09:49:03
8 [报告]
发表于 2015-10-19 21:57 |只看该作者
  1. #include <sys/types.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include <sys/wait.h>
  6. #include <fcntl.h>

  7. #define BUFFER_SIZE 80
  8. #define READ_END 0
  9. #define WRITE_END 1

  10. int main(int argc, char* argv[])
  11. {

  12.     /* argc must be 3 to ensure the correct usage */
  13.     if (argc != 3) {
  14.         printf("The usage of this program is:\n");
  15.         printf("FileCopy input.txt copy.txt\n");
  16.         return 1;
  17.     }

  18.     else {
  19.         int pid;
  20.         /* initialize the pipe and fork variables */
  21.         int fd[2];/* argv[0] is the program name, argv[1] and argv[2] are the file names */
  22.         char* infile = argv[1];
  23.         char* outfile = argv[2];

  24.         /* create a pipe */
  25.         if (pipe(fd) == -1) {
  26.             fprintf(stderr, "Pipe failed");
  27.             return 1;
  28.         }

  29.         /* fork */
  30.         pid = fork();

  31.         /* if pid < 0 there is an error */
  32.         if (pid < 0) {
  33.             fprintf(stderr, "Fork error");
  34.             return 1;
  35.         }

  36.         /* parent code */
  37.         if (pid > 0) {
  38.             FILE* in;
  39.             int len;
  40.             char buffer[BUFFER_SIZE];
  41.             int i;
  42.             /* close the unused end of the pipe */
  43.             close(fd[READ_END]);
  44.             /* make a buffer to store input file information */


  45.             /* open input file */
  46.             in = fopen(infile, "r");

  47.             /* make sure it's not null */
  48.             if (in == NULL) {
  49.                 fprintf(stderr, "Error opening file %s\n", infile);
  50.                 return 1;
  51.             }

  52.             /* read from the file into the pipe thru the buffer */
  53.             while (1) {
  54.                 memset(buffer, 0, sizeof(buffer));
  55.                 len = fread(buffer, sizeof(char), BUFFER_SIZE, in);

  56.                 // printf("len 1=%d\n", len);
  57.                 if (len <= 0) {
  58.                     break;
  59.                 }


  60.                 for (i = 0; i < strlen(buffer); ++i) {
  61.                     printf("%c", buffer[i]);
  62.                 }


  63.                 write(fd[WRITE_END], buffer, len);
  64.             }

  65.             /* close the input file */
  66.             fclose(in);
  67.             close(fd[WRITE_END]);
  68.             wait(NULL);
  69.         }

  70.         /* child process has pid == 0 */
  71.         else {
  72.             FILE* out;
  73.             int len;

  74.             /* create a buffer to read from the pipe and store into the out file */
  75.             char buffer[BUFFER_SIZE];

  76.             /* close the unused end of the pipe */
  77.             close(fd[WRITE_END]);

  78.             /* initialize the output file */

  79.             /* w+ means open a file for writing and create it if it doesn't exist */
  80.             out = fopen(outfile, "w");

  81.             while (1) {
  82.                 memset(buffer, 0, sizeof(buffer));
  83.                 len = read(fd[READ_END], buffer, BUFFER_SIZE) ;

  84.                 if (len <= 0) {
  85.                     break;
  86.                 }

  87.                 fwrite(buffer, sizeof(char), len, out);
  88.             }

  89.             /* close the file and the pipe */
  90.             fclose(out);
  91.             close(fd[READ_END]);
  92.         }
  93.     }

  94.     return 0;
  95. }
复制代码
我改成 fread + write 可以 , 但是下面這樣就不能, 是 fgets 跟 write  不能混用嗎?
  1. /* read from the file into the pipe thru the buffer */
  2. while (fgets(buffer, BUFFER_SIZE, in)) {
  3.     write(fd[WRITE_END], buffer, strlen(buffer)+1);
  4.     buffer[0]='\0';
  5. }

复制代码
  1. while(1) {
  2.     if (read(fd[READ_END], buffer, BUFFER_SIZE) != 0)
  3.     {
  4.         fprintf(out,"%s",buffer);
  5.         fflush(out);
  6.     }
  7.     else
  8.         break;
  9. }
复制代码

论坛徽章:
1
2015年迎新春徽章
日期:2015-03-04 09:49:03
9 [报告]
发表于 2015-10-19 22:52 |只看该作者
本帖最后由 shihyu 于 2015-10-19 22:53 编辑

找到原因 fgets 字符串最后自动加入 '\0' , 改回 '\n' 之后 write

read 之后在 buffer 最后加上 '\0' 往 fprintf 送即可
  1. #include <sys/types.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <unistd.h>

  5. #define BUFFER_SIZE 200
  6. #define READ_END 0
  7. #define WRITE_END 1

  8. int main(int argc, char *argv[]) {

  9.     /* argc must be 3 to ensure the correct usage */
  10.     if (argc != 3) {
  11.         printf("The usage of this program is:\n");
  12.         printf("FileCopy input.txt copy.txt\n");
  13.         return 1;
  14.     }

  15.     else {
  16.         int pid;
  17.         /* initialize the pipe and fork variables */
  18.         int fd[2];/* argv[0] is the program name, argv[1] and argv[2] are the file names */
  19.         char *infile = argv[1];
  20.         char *outfile = argv[2];

  21.         /* create a pipe */
  22.         if (pipe(fd) == -1) {
  23.             fprintf(stderr, "Pipe failed");
  24.             return 1;
  25.         }

  26.         /* fork */
  27.         pid = fork();

  28.         /* if pid < 0 there is an error */
  29.         if (pid < 0) {
  30.             fprintf(stderr, "Fork error");
  31.             return 1;
  32.         }

  33.         /* parent code */
  34.         if (pid > 0) {
  35.             FILE *in;
  36.             char buffer[BUFFER_SIZE];
  37.             int len;
  38.             /* close the unused end of the pipe */
  39.             close(fd[READ_END]);
  40.             /* make a buffer to store input file information */


  41.             /* open input file */
  42.             in = fopen(infile, "r");

  43.             /* make sure it's not null */
  44.             if (in == NULL) {
  45.                 fprintf(stderr, "Error opening file %s\n", infile);
  46.                 return 1;
  47.             }

  48.             /* read from the file into the pipe thru the buffer */
  49.             while (fgets(buffer, BUFFER_SIZE, in)) {
  50.                 len = strlen(buffer);
  51.                 buffer[len]='\n';
  52.                 write(fd[WRITE_END], buffer, len);
  53.             }

  54.             /* close the input file */
  55.             close(fd[WRITE_END]);
  56.             fclose(in);
  57.             wait(NULL);
  58.         }

  59.         /* child process has pid == 0 */
  60.         else {
  61.             FILE *out;
  62.             int len;

  63.             /* create a buffer to read from the pipe and store into the out file */
  64.             char buffer[BUFFER_SIZE];

  65.             /* close the unused end of the pipe */
  66.             close(fd[WRITE_END]);

  67.             /* initialize the output file */

  68.             /* w+ means open a file for writing and create it if it doesn't exist */
  69.             out = fopen(outfile, "w+");

  70.             while(1) {
  71.                 memset(buffer, 0, sizeof(buffer));
  72.                 len = read(fd[READ_END], buffer, BUFFER_SIZE) ;

  73.                 if (len <= 0) {
  74.                     break;
  75.                 }

  76.                 buffer[len + 1] = '\0';

  77.                 printf("buffer=%s, len 2=%ld\n", buffer, strlen(buffer));
  78.                 fprintf(out,"%s", buffer);
  79.                 fflush(out);
  80.             }

  81.             /* close the file and the pipe */
  82.             fclose(out);
  83.             close(fd[READ_END]);
  84.         }
  85.     }

  86.     return 0;
  87. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP