Tape read and write

zhaozj2021-02-16  82

The storage of the tape is performed according to the block, so the reading and writing to the tape is in units of block, which is different from the maximum disk file. Every time you read Read, you must specify the size of the read size is the size of the block. When writing tapes, try to make according to the size of the block.

For example, a block size of a tape is 10K, then if read call is N = Read (FD, BUF, 100); then the error will be returned: Cannot Allocate Memory, which is obviously an inexplicable error message. But if this is called N = Read (FD, BUF, 10 * 1024 100); then only one 10k data can be read, and the 100 bytes of the following will not be read.

Obviously for high-level applications, this constraint is extremely unhappy. Therefore, it is necessary to read and write the tape.

Give an example of using cache read tape, I have just written

TAPE classes include several member variables: char * read_buf; // read buffer unsigned int RBUF_PTR; // The current read pointer in the cushioning area UNSigned int RBUF_LEN; / / The data length that has been read in the buffer

Unsigned int block_size; // tape block size

INT TAPE :: read (cost) {if (! is_open ()) {if (! open ()) {return -1;}}}

INT NREAD = 0; int NTODO = Size; while (1) {// See if''re Enough Chars We can provide in read_buf, // instead ofread from tape. int nremain = rbuf_len - rbuf_ptr; if (nremain> = Ntodo) {// Yes, How Happy We Are. Memcpy (Buffer Nread, READ_BUF RBUF_PTR, NTODO; RBUF_PTR = NTODO; NREAD = Ntodo; Break;} // Not Enough? Okay, Let's Bring Out What We Have, firstly if (nremain) {memcpy (buffer nread, read_buf rbuf_ptr, nremain); NREAD = nremain; Ntodo - = nremain;

// and then, we read a block more from time. int N = :: read (fd, read_buf, block_size); if (n <0) {ok = false; err = string ("TAPE:: Read ()" ) strrror (errno); RETURN-1;} RBUF_LEN = N; RBUF_PTR = 0;

IF (n == 0) // read nothing, hopefully an eof meturn nread;}

How to get a magnetic band size is an easy problem, because the maximum size of the general tape block is 256K, so you will read a piece, see the length of the data read (FD, BUF) N = Read (FD, BUF 256 * 1024); or simpler, use the following command DD if = / dev / time = / tmp / blk bs = 256k count = 1LS -LH / TMP / BLK this file size is

转载请注明原文地址:https://www.9cbs.com/read-14464.html

New Post(0)