0

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

我无法确保将 file1 完全复制到缓冲区中。当缓冲区复制到 file2 时,file2 只包含 file1 的部分内容,输出显示只有 4 个字节的数据已复制到 file2。

我试图弄清楚我做错了什么,但到目前为止我还没有运气。您的帮助将不胜感激。

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

我还在命令提示符下使用 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;

}
4

2 回答 2

1

这里:

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

fdDestination是一个文件描述符,它不是写入的字节数。01并且2是您的三个标准流,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$
于 2013-09-28T06:04:43.087 回答
0

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

于 2013-09-28T06:19:02.477 回答