繁体   English   中英

C系统程序 - 复制期间的读/写问题

[英]C Systems Program - Read/Write Issues During Copy

我正在编写一个C程序,它从标准的UNIX存档中提取并创建它存储的文件。

这是一个例子,如果我在vim中打开它会是什么样的:

!<arch>
yo              1382105439  501   20    100644  10        `
test1 lol
yo2             1382105444  501   20    100644  10        `
test2 lol

...其中“test1 lol”和“test2 lol”是每个文件的内容,“yo”和“yo2”是两个不同的文件名,其余的是以对应于标准ar.h的格式存储的元数据(在这里阅读更多内容: http//www.lehman.cuny.edu/cgi-bin/man-cgi?ar.h + 3

无论如何,我仍在编写函数的过程中,但这是我到目前为止所拥有的:

static void extract_files (int argc, char *argv[])
{

  int fd;
  int new_file_fd;
  int num_read = 0;
  int new_file_size;

  struct ar_hdr current_header;

  char name_buffer[16];
  char date_buffer[12];
  char uid_buffer[6];
  char gid_buffer[6];
  char mode_buffer[8];
  char size_buffer[10];
  char fmag_buffer[2];

  // grab the fd #
  fd = open(argv[2], O_RDWR | O_CREAT, 0666);

  // 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);
    int size = atoi(size_buffer);
    sscanf(current_header.ar_fmag, "%s", fmag_buffer);

    // Create a new file
    new_file_fd = creat(name_buffer, mode);
    // Grab new file size
    new_file_size = atoi(size_buffer);

    int io_size; // buffer size
    char buff[size];
    int read_cntr = 0;

    // from copy.c
    while ((io_size = read (fd, buff, new_file_size)) > 0)
    {
      read_cntr++;
      if (read_cntr > new_file_size)
        break;
      write (new_file_fd, buff, new_file_size);
    }

    close(new_file_fd);
    printf("%s\n", name_buffer);
    printf("%s\n", date_buffer);
    printf("%s\n", uid_buffer);
    printf("%s\n", gid_buffer);
    printf("%s\n", mode_buffer);
    printf("%s\n", size_buffer);
    printf("%s\n", fmag_buffer);

    /* Seek to next header. */
    lseek(fd, atoi(current_header.ar_size) + (atoi(current_header.ar_size)%2), SEEK_CUR);
  }

}

我遇到的问题在于上面代码中的第二个while循环:

    // from copy.c
while ((io_size = read (fd, buff, new_file_size)) > 0)
{
  read_cntr++;
  if (read_cntr > new_file_size)
    break;
  write (new_file_fd, buff, new_file_size);
}

由于某种原因,在此while循环中写入的文件不会运行到write指定的长度。 标准read()/ write()的第三个参数应该是要写入的字节数。 但是出于某种原因,我的代码导致整个存档被读入并写入第一个文件。

如果我打开生成的“yo”文件,我发现整个存档文件已写入其中

test1 lol
yo2             1382105444  501   20    100644  10        `
test2 lol

在读取10个字节并给出预期结果“test1 lol”之后,而不是终止。

我还可以确认“new_file_size”值确实为10.所以我的问题是:我在读取循环时读错了什么?

注意:预期输入将是命令行参数,类似于:./ extractor.c -x name_of_archive_file

我认为我需要在此函数中处理的唯一相关信息是存档文件的名称,我在extract_files的开头获取了fd。

补充:杂项 - 运行时的输出:

yo
1382105439
501
20
X
10
`

正如你所看到的,它永远不会看到yo2文件或打印出它的标题,因为它会被写入“yo”之前可能发生...因为这个流浪而循环:(

你的while()循环应该在它之后有大括号( { ... } ),否则你只是在不做任何其他事情的情况下递增read_cntr

你读取一个值size_buffer,并将其分配给size和new_file_size,你还创建一个相同大小的buffer[size]

int size = atoi(size_buffer);
sscanf(current_header.ar_fmag, "%s", fmag_buffer);
//...
new_file_size = atoi(size_buffer);
//...
char buff[size];

Read返回范围[0..new_file_size]的ssize_t字节数,你设置为io_size,意识到read(2)可能return < new_file_size字节,这就是你需要while循环的原因。 因此,您需要编写已阅读的所有内容,直到达到写入限制为止。 我已经做了一些评论来指导你。

// from copy.c
while ((io_size = read (fd, buff, new_file_size)) > 0)
{
    read_cntr++;
    //perhaps you mean read_cntr += io_size;
    //you probably mean to write io_size bytes here, regardless
    //write(new_file_fd, buff, io_size);
    if (read_cntr > new_file_size) //probably you want >= here
        break;
    //you may have broke before you write...
    write (new_file_fd, buff, new_file_size);
}

这个副本的一个更典型的习惯用法是你选择一个读/写缓冲区大小,比如4*1024 (4K)16*1024 (16K)等,并读取块大小,直到你剩下的块大小少于; 例如,

//decide how big to make buffer for read()
#define BUFSIZE (16*1024) //16K
//you need min(
#define min(x,y) ( ((x)<(y)) ? (x) : (y) )
ssize_t fdreader(int fd, int ofd, ssize_t new_file_size )
{
    ssize_t remaining = new_file_size;
    ssize_t readtotal = 0;
    ssize_t readcount;
    unsigned char buffer[BUFSIZE];
    for(  ; readcount=read(fd,buffer,min(sizeof(buffer),remaining));  )
    {
        readtotal += readcount;
        if( readcount > remaining ) //only keep remaining
            readcount = remaining;
        write( ofd, buffer, readcount);
        remaining -= readcount;
        if( remaining <= 0 ) break; //done
    }
    return readtotal;
}

尝试这个,

#include<stdio.h>
#include<stdlib.h>

void usage(char*progname)
{
    printf("need 2 files\n");
    printf("%s <infile> <outfile>\n",progname);
}

//decide how big to make buffer for read()
#define BUFSIZE (16*1024) //16K
//you need min(
#define min(x,y) ( ((x)<(y)) ? (x) : (y) )
ssize_t fdreader(int fd, int ofd, ssize_t new_file_size )
{
    ssize_t remaining = new_file_size;
    ssize_t readtotal = 0;
    ssize_t readcount;
    unsigned char buffer[BUFSIZE];
    for(  ; readcount=read(fd,buffer,min(sizeof(buffer),remaining));  )
    {
        readtotal += readcount;
        if( readcount > remaining ) //only keep remaining
            readcount = remaining;
        write( ofd, buffer, readcount);
        remaining -= readcount;
        if( remaining <= 0 ) break; //done
    }
    return readtotal;
}

int main(int argc,char**argv)
{
    int i=0; /* the infamous 'i' */
    FILE*infh;
    FILE*outfh;

    if( argc < 3 )
    {
        usage(argv[0]);
        return 0;
    }

    printf("%s %s\n",argv[1],argv[2]); fflush(stdout);
    if( !(infh=fopen(argv[1],"r")) )
    {
        printf("cannot open %s\n",argv[2]); fflush(stdout);
        return(2);
    }
    if( !(outfh=fopen(argv[2],"w+")) )
    {
        printf("cannot open %s\n",argv[3]); fflush(stdout);
        return(3);
    }

    int x = fdreader(fileno(infh), fileno(outfh), 512 );

    return 0;
}

暂无
暂无

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

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