我读的比写在文件里的多

I read more than I write in file

我有一个文件,分区为固定大小的块。我正在将 test_file.txt 复制到文件的第三个块中。我读取并复制了 18 个字节。

然后我试图从文件中复制我刚刚导入的完全相同的 .txt 文件到新创建的 .txt,但我正在向新文件写入 256 个字节。而且,当我尝试阅读它时,它充满了垃圾。

第一个函数用于导入 .txt,第二个函数用于导出。

void copy_file(int mfs_desc, char* filename, Superblock* s, MDS mds) {
  if(mds.size == 0)
    return;
  char buffer[s->block_size];
  int i = 0;
  for (; i < s->block_size; ++i) {
    buffer[i] = '[=10=]';
  }

  int source_desc = open(filename, O_RDONLY);
  // error handling
  if (source_desc == -1) {
    perror("opening file in copy file");
    exit(1);
  }

  ssize_t nread;
  int total = 0;

  off_t seek = lseek(mfs_desc,
               sizeof(Superblock) + mds.datablocks[0] * s->block_size,
               SEEK_SET);
  printf("offset = %d\n", mds.datablocks[0]);
  if (seek < 0) {
    perror("seek");
    exit(1);
  }

  total = 0;
  while ((nread = read(source_desc, buffer, s->block_size)) > 0) {
    total += nread;
    write(mfs_desc, buffer, s->block_size);
  }
  printf("read and copied: %d bytes\n", total);

  if (close(source_desc) == -1) {
    perror("closing file in copy file");
    exit(1);
  }
}

int copy_file_export(int mfs_desc, char* filename, Superblock s, MDS mds) {
  if(mds.size == 0) {
    printf("File is empty, abort\n");
    return 0;
  }
  char buffer[s.block_size];
  int i = 0;
  for (; i < s.block_size; ++i) {
    buffer[i] = '[=10=]';
  }

  int destination_desc = open(filename, O_CREAT | O_WRONLY);
  // error handling
  if (destination_desc == -1) {
    printf("filename = |%s|\n", filename);
    perror("opening file in copy file export");
    exit(1);
  }

  ssize_t nread;
  int total = 0;

  off_t seek = lseek(mfs_desc,
               sizeof(Superblock) + mds.datablocks[0] * s.block_size,
               SEEK_SET);
  printf("offset = %d\n", mds.datablocks[0]);
  if (seek < 0) {
    perror("seek");
    exit(1);
  }
  for(i = 0; i < mds.size; ++i) {
    nread = read(mfs_desc, buffer, s.block_size);
    total += nread;
    write(destination_desc, buffer, nread);
  }
  printf("wrote: %d bytes\n", total);

  if (close(destination_desc) == -1) {
    perror("closing file in copy file");
    exit(1);
  }
  return 1;
}

输出:

import test_file.txt ... / <-- just a command
offset = 2
read and copied: 18 bytes
export test_file.txt ... ../../ <-- just a command
offset = 2
wrote: 256 bytes

我做错了什么?

我会替换

write(mfs_desc, buffer, s->block_size);

write(mfs_desc, buffer, nread);

在这段代码中:

while ((nread = read(source_desc, buffer, s->block_size)) > 0) {
    total += nread;
    write(mfs_desc, buffer, s->block_size);
}

您很可能对最后一个 write() 处理不当。您只需要写入您读取的字节。

    write(mfs_desc, buffer, nread);

此外,这些行很可能是伪造的:

char buffer[s->block_size];

char buffer[s.block_size];

您正在尝试对堆栈上的数组使用可变大小的分配。你不能那样做。这些分配的大小必须固定(编译时间常数)。