簡體   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