【问题标题】:Copy data of one file to another将一个文件的数据复制到另一个文件
【发布时间】:2014-09-18 11:17:29
【问题描述】:

我正在尝试使用“C”将数据从一个文件复制到 UNIX 中的另一个文件。复制过程中的条件是 1) 源文件不退出 2) 源文件存在但目标文件不存在。 3) 如果源文件和目标文件都存在,则选择覆盖目标文件或将源文件数据附加到目标文件数据。

以下代码在前两种情况下运行良好。但不适用于最后一种情况(覆盖和附加)。当我执行程序并输入现有的源文件时,目标文件位置选择程序表示数据已被附加但实际上数据没有附加或覆盖的选项之一。

请告诉我如何使程序在最后一种情况下正常工作。

#include<stdio.h>
#include<fcntl.h>
#include<unistd.h>

#define BUFFSIZE 512
#define PERM 0644

int main (int argc, char *argv[])
{
    char buffer[BUFFSIZE];
    int infile;
    int outfile;
    int choice;
    size_t size;

    if((infile = open(argv[1], O_RDONLY,0)) < 0)
    {
        printf("Source file does not exist\n");
        return -1;
    }

    if((outfile=open(argv[2],O_WRONLY,PERM))>0)

    {
        //  printf("Destination Fiel Exists , Do you wish to Overwrite or Appened destination file Data :   \n Enter 1 to overwrite or ,\n 0 to Append \n");
        scanf("%d",&choice);

        if(choice==1)
        {
            if((outfile=open(argv[2],O_WRONLY|O_CREAT |O_EXCL, PERM))>=0)
            {
                printf("Destination file data is overwriten by source file data \n");
                return -2;
            }
        }// end if (choice =1)

        else
        {
            if(choice==0)
            {
                if((outfile=open(argv[2],O_WRONLY |O_CREAT |O_APPEND, PERM ))>=0)
                {
                    printf("Destination file data is appended with source file data : \n");
                }
            } // end if(choice==0)

            else
            {
                printf("Entered invalid choice.: \n");
            }
            return -3;
        }
    }
    else
    {
        if((outfile=open(argv[2],O_WRONLY | O_CREAT | O_EXCL,PERM))<0)
        {
            printf("Enter the desitination file along with source file \n");
            return -4;
        }
        else {
            printf(" Data has been copied to \t%s\n", argv[2]);
        }
    }

    while ((size=read(infile, buffer, BUFFSIZE)) > 0)
    {
        write(outfile, buffer, size);
    }

    close(infile);
    close(outfile);
    return 0;

}

【问题讨论】:

    标签: c file unix copy


    【解决方案1】:
       if((outfile=open(argv[2],O_WRONLY|O_CREAT |O_EXCL, PERM))>=0)
       {
            printf("Destination file data is overwriten by source file data \n");
       }
    
    // instead use 
    
       if((outfile=open(argv[2],O_WRONLY|O_CREAT |O_TRUNC, PERM))>=0)
       {
            printf("Destination file data is overwriten by source file data \n");
       }
    

    有几点我想指出:

    1. 尝试覆盖目标文件时,需要使用 O_TRUNC 而不是 O_EXCL。这将确保目标文件数据的长度 设为 0,然后将源文件复制到其中。
    2. 我也有 使用 switch case 而不是 if-else 子句,以使其更容易 阅读
    3. 当你使用'return'时,控制回到主 其余的语句将不会被执行。

    您可以使用 fread() 和 fwrite() 使任务更快地完成。

    【讨论】:

    • 我试过你的建议还是不行。
    【解决方案2】:
    if((outfile=open(argv[2],O_WRONLY|O_CREAT |O_EXCL, PERM))>=0)
    {
        printf("Destination file data is overwriten by source file data \n");
    }
    
    // instead use 
    
    if((outfile=open(argv[2],O_WRONLY|O_CREAT |O_TRUNC, PERM))>=0)
    {
        printf("Destination file data is overwriten by source file data \n");
    }
    

    有几点我想指出:

    1) 尝试覆盖目标文件时,需要使用O_TRUNC 而不是O_EXCL。这将确保目标文件数据的长度为0,然后将源文件复制到其中。

    2) 我还使用了 switch case 而不是 if-else 子句,以使其更易于阅读。

    3) 当你使用return 时,控制回到main,剩下的语句不会被执行。您可以使用fread()fwrite() 让任务更快完成。

    【讨论】:

      猜你喜欢
      • 2019-04-11
      • 2015-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多