简体   繁体   English

C带有指定字节长度的读写问题

[英]C Read and write Issues w/ Specified Byte Length

I have a while loop in which I am reading from an archive file and extracting the first file. 我有一个while循环,我从档案文件中读取并提取第一个文件。

  int fd = open(argv[2], O_RDWR | O_CREAT, 0666);
  char name_buffer[16];
  char size_buffer[10];

  // go to the first header
  lseek(fd, SARMAG, SEEK_CUR);

  // store the number of bits read in a struct current_header
  // until its size equal to the size of the entire
  // header, or in other words, until the entire
  // header is read
  while ((num_read = read(fd, (char*) &current_header, 
    sizeof(struct ar_hdr))) == sizeof(struct ar_hdr))
  {

    // scans the current string in header and stores
    // in nameStr array
    sscanf(current_header.ar_name, "%s", name_buffer);
    sscanf(current_header.ar_date, "%s", date_buffer);
    sscanf(current_header.ar_uid, "%s", uid_buffer);
    sscanf(current_header.ar_gid, "%s", gid_buffer);

    int mode;
    sscanf(current_header.ar_mode, "%o", &mode);
    sscanf(current_header.ar_size, "%s", size_buffer);
    sscanf(current_header.ar_fmag, "%s", fmag_buffer);

    new_file_fd = open(name_buffer, O_WRONLY | O_CREAT | O_TRUNC);
    int size = atoi(size_buffer);
    char buf[size];
    size_t count = size;

    while ((n_read = read(fd, buf, size)) > 0)
    {
      n_write = 0;
      do {
        n = write(new_file_fd, &buf[n_write], n_read - n_write);
        n_write += n;
      } while (n_write < n_read);

    }
    close(new_file_fd);
   }
   lseek(fd, size + (size%2), SEEK_CUR);

For a given archive file with the structure: 对于具有以下结构的给定存档文件:

!<arch>
File1             1382142494  501   20    100644  29        `
Testing 123

File2             1382142504  501   20    100644  23        `
Testing 246

The expected output should be a file "File1" that contains only the contents "Testing123". 预期的输出应该是仅包含内容“ Testing123”的文件“ File1”。 Instead, I get File1 with the contents: Testing 123 而是,我得到包含以下内容的File1:测试123

File2             1382142504  501   20    100644  23        `
Testing 246

For some reason, even though I've specified the amount of bits to read and write (parameter 3 is "size" which returns 29 -- the size of File1) it keeps reading and writing past 29 bytes. 由于某种原因,即使我已指定要读取和写入的位数(参数3为“ size”,它返回29-File1的大小),它仍会继续读取和写入超过29个字节。

Any ideas why this might be happening? 任何想法为什么会发生这种情况?

while ((n_read = read(fd, buf, size)) > 0)

You need to update size within the loop. 您需要在循环中更新大小。

size -= n_read

Otherwise you're just going to continue looping until you get to the end of the file. 否则,您将继续循环直到到达文件末尾。 The reason you need to loop a call to read() is just that it doesn't guarantee it will read the specified number of bytes on the first call, it only guarantees it won't exceed that. 您需要循环调用read()的原因仅在于,它不能保证将在第一次调用时读取指定数目的字节,而只能保证不会超过指定的字节数。 So you need to keep calling it until you've read all the bytes you want. 因此,您需要继续调用它,直到您读取了所有想要的字节。 But you do need to update the bytes parameter since the file descriptor fd will keep advancing to the end of the file otherwise. 但是您确实需要更新bytes参数,因为否则文件描述符fd会一直前进到文件末尾。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM