Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 我在文件里读的比写的多_C_Linux_File_Io_Posix - Fatal编程技术网

C 我在文件里读的比写的多

C 我在文件里读的比写的多,c,linux,file,io,posix,C,Linux,File,Io,Posix,我有一个文件,分为固定大小的块。我正在将一个test_file.txt复制到文件的第三个块中。我读并复制了18个字节 然后,我试图从文件中复制刚刚导入到新创建的.txt文件中的.txt文件,但我正在向新文件中写入256字节。此外,当我试图阅读它时,它充满了垃圾 第一个函数用于导入.txt,第二个函数用于导出它 void copy_file(int mfs_desc, char* filename, Superblock* s, MDS mds) { if(mds.size == 0)

我有一个文件,分为固定大小的块。我正在将一个
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] = '\0';
  }

  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] = '\0';
  }

  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;
}
void copy_文件(int-mfs_-desc,char*filename,Superblock*s,MDS-MDS){
如果(mds.size==0)
返回;
字符缓冲区[s->block_size];
int i=0;
对于(;iblock_size;++i){
缓冲区[i]='\0';
}
int source_desc=open(文件名,仅限ordu);
//错误处理
如果(源描述==-1){
perror(“在副本文件中打开文件”);
出口(1);
}
ssize_t nread;
int-total=0;
关闭搜索=lseek(mfs描述,
sizeof(Superblock)+mds.datablock[0]*s->block\u size,
搜索集);
printf(“偏移量=%d\n”,mds.datablock[0]);
如果(搜索<0){
佩罗(“寻求”);
出口(1);
}
总数=0;
而((nread=read(source\u desc,buffer,s->block\u size))>0){
总+=nread;
写入(mfs_desc、缓冲区、s->block_size);
}
printf(“读取和复制:%d字节\n”,总计);
如果(关闭(源描述)=-1){
perror(“在副本文件中关闭文件”);
出口(1);
}
}
int copy_file_export(int mfs_desc,char*文件名,超级块s,MDS MDS){
如果(mds.size==0){
printf(“文件为空,中止\n”);
返回0;
}
字符缓冲区[s.block_size];
int i=0;
对于(;i
输出:

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
import test_file.txt…/ 我会取代

write(mfs\u desc,buffer,s->block\u size)

写入(mfs_desc、缓冲区、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];
您试图对堆栈上的数组使用可变大小的分配。你不能那样做;。这些分配的大小必须固定(编译时常量)。

这些分配的大小必须固定(编译时常量)。那么...怎么样查找输入可变长度数组。