使用write系统调用无法将整个文件复制到另一个文件

时间:2013-09-28 05:54:57

标签: c buffer posix system-calls file-copying

我必须将file1的内容复制到缓冲区(大小为23字节),然后,我必须将数据从缓冲区复制到file2。

我无法确保将file1完全复制到缓冲区中。当缓冲区被复制到file2时,file2内容只是file1内容的一部分,输出表明只有4个字节的数据被复制到file2中。

我试图弄清楚我做错了什么,但到目前为止我没有运气。非常感谢您的帮助。

我正在使用Oracle VM VirtualBox,我安装了Ubuntu。

我也在使用make(MakeFile)在命令提示符下一次更新所有文件。

我的代码在C / POSIX下面。

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <errno.h>
#include <stdlib.h>

#define  My_Full_Name "AAA!"

int PrintSentence()
{

  size_t buffersize = (size_t) (4 * 5.75);  //or (4 bytes * 5.75) = 23 bytes
  char buffer[buffersize];
  char source_file[200];
  char destination_file[200];

  ssize_t bytes_read;

  int fdSource, fdDestination;

  mode_t mode = S_IRUSR | S_IWUSR;

  printf("Welcome to File Copy by %s\n", My_Full_Name);

  printf("Enter the name of the source file: ");

  scanf("%s", source_file);

  printf("Enter the name of the destination file: ");

  scanf("%s", destination_file);

  fdSource = open(source_file, O_RDONLY);

  if (fdSource < 0)
  {
    perror("Open failed!!");
    return 1;
  }

  else
  {

    bytes_read = read(fdSource, buffer, sizeof(buffer));

    fdDestination = open(destination_file, O_CREAT | O_WRONLY | mode);

    if (fdDestination < 0)
    {
      perror("Oups!! cannot create file again!!");

      return 1;
    }
    else
    {
      write(fdDestination, buffer, sizeof(buffer));
      printf("current content of buffer: %s\n", buffer); //just to check
      printf("current value of buffer size = %zd  \n", buffersize); //just to check
      printf("File copy was successful, with %d byte copied\n", fdDestination); //the output says only 4 bytes are copied

    }

  }

  return;

}

2 个答案:

答案 0 :(得分:1)

下面:

printf("File copy was successful, with %d byte copied\n", fdDestination );

fdDestination是一个文件描述符,它不是写入的字节数。 012是您的三个标准流,3将是您首先打开的输入文件,因此4将是您的输出文件,这就是为什么它总是输出4

您希望保存write()的返回值,并使用其值(在检查错误的返回值之后,当然,您应该为read()执行此操作)

编辑:略微修改您的代码:

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <errno.h>
#include <stdlib.h>

#define  My_Full_Name "AAA!"

int main(void) {
    size_t buffersize = (size_t) (4 * 5.75);
    char buffer[buffersize];
    char source_file[200];
    char destination_file[200];

    ssize_t bytes_read, bytes_written;
    int fdSource, fdDestination;

    mode_t mode = S_IRUSR | S_IWUSR;

    printf("Welcome to File Copy by %s\n", My_Full_Name);
    printf("Enter the name of the source file: ");
    scanf("%s", source_file);

    printf("Enter the name of the destination file: ");
    scanf("%s", destination_file);

    fdSource = open(source_file, O_RDONLY);

    if (fdSource < 0) {
        perror("Open failed!!");
        return 1;
    } else {
        bytes_read = read(fdSource, buffer, sizeof(buffer));
        fdDestination = open(destination_file, O_CREAT | O_WRONLY | mode);

        if (fdDestination < 0) {
            perror("Oups!! cannot create file again!!");
            return 1;
        } else {
            bytes_written = write(fdDestination, buffer, sizeof(buffer));
            printf("current content of buffer: %s\n", buffer);
            printf("current value of buffer size = %zd  \n", buffersize);
            printf("File copy was successful, with %d byte copied\n",
                    bytes_written);
        }
    }

    return 0;
}

给了我这个:

paul@local:~/src/c/fpc$ cat infile
12345678901234567890123
paul@local:~/src/c/fpc$ ./fpc
Welcome to File Copy by AAA!
Enter the name of the source file: infile
Enter the name of the destination file: outfile
current content of buffer: 12345678901234567890123
current value of buffer size = 23
File copy was successful, with 23 byte copied
paul@local:~/src/c/fpc$ cat outfile; echo ""
12345678901234567890123
paul@local:~/src/c/fpc$

答案 1 :(得分:0)

当缓冲区长度为23个字节时,您怎么能指望文件完全写入缓冲区?通过调用read,您只读取23个字节,并保持file1的其余内容不变。或者,您的程序应该只将其内容的23个字节复制到目标文件,这是您的预期行为吗?