我正在寻找一个C ++程序来将一些模式写入磁盘设备,但是当我尝试设置一个大于0的偏移量时,我遇到了一个问题。请参阅下面的示例和代码。
谁能告诉我我做错了什么?
谢谢,
root@solaris:~/# CC simple_test.cc
root@solaris:~/# ./a.out
Insert device name, for example: /dev/rdsk/c2t600A0B800011238000001D3D45DA78A7d0s1
/dev/rdsk/c0t6000402E5000000035969F0C97324D24d0s2
iteration: 0. offset: 0
offset: 1048577
iteration: 1. offset: 1048577
WRITE operation error: 22; offset: 1048577
offset: 2097154
iteration: 2. offset: 2097154
WRITE operation error: 22; offset: 2097154
offset: 3145731
iteration: 3. offset: 3145731
WRITE operation error: 22; offset: 3145731
offset: 4194308
iteration: 4. offset: 4194308
WRITE operation error: 22; offset: 4194308
offset: 5242885
Test completed
root@solaris:~/# cat simple_test.cc
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sstream>
#define BS 1048576
using namespace std;
const char PATTERN[ ] = { 'A' };
int main(int argc, char **argv)
{
char dev[ 100 ];
printf( "Insert device name, for example: /dev/rdsk/c2t600A0B800011238000001D3D45DA78A7d0s1\n" );
scanf( "%s", dev );
int fd;
if ( ( fd = open( dev, O_RDWR ) ) == -1 )
{
printf( "error opening device: %s\n", dev );
return 1;
}
char* pattern = new char[ BS ];
memset ( pattern, PATTERN[0], BS );
int offset = 0;
int nbytes = 0;
for (int i=0; i<5; i++ ) {
cout << "iteration: " << i << ". offset: " << offset << endl;
nbytes = pwrite( fd, pattern, BS, offset );
if ( nbytes != BS )
printf( "WRITE operation error: %d; offset: %ld\n", errno, offset );
offset += BS + 1;
cout << "offset: " << offset << endl;
}
delete pattern;
printf( "Test completed\n" );
return 0;
}
答案 0 :(得分:1)
我不确定,但您是不是需要写入块中的块设备?因此,您的偏移量必须是块大小的倍数。但是你用BS + 1递增它。为什么+1?我想如果你坚持使用+ = BS它可能会有效。