Wednesday, 15 August 2012

c++ - Using WriteFile to fill up a cluster -



c++ - Using WriteFile to fill up a cluster -

i want utilize writefile fill end of every file until reaches end of lastly cluster. want delete wrote , repeat process(attempting rid info might have been there). have 2 issues:

writefile gives me error: error_invalid_parameter depending on type of file, writefile() gives me different results

so first issue realized parameter nnumberofbytestowrite in writefile() has multiple of bytes per sector(my case 512 bytes). limitation of function or doing wrong? in sec issue, i'm using 2 dummy files(.txt , .html) on external hard drive write random info to. in case of .txt file, info written end of file need. however, .html file writes origin of file , replaces info there. here code snippets relevant issue:

hfile = createfile(result, generic_read | generic_write |file_read_attributes, file_share_read | file_share_write, 0, open_existing, file_flag_no_buffering, 0); if (hfile == invalid_handle_value) { cout << "file not exist" << endl; closehandle(hfile); } dword dwbyteswritten; char * wfilebuff = new char[512]; memset (wfilebuff,'0',512); returnz = setfilepointer(hfile, 0,null,file_end); if(returnz ==0){ cout<<"error: "<<getlasterror()<<endl; }; lockfile(hfile, returnz, 0, 512, 0) returnz =writefile(hfile, wfilebuff, 512, &dwbyteswritten, null); if(returnz ==0){ cout<<"error: "<<getlasterror()<<endl; } unlockfile(hfile, returnz, 0, 512, 0); cout<<dwbyteswritten<<endl<<endl;

i using static numbers @ moment test out functions. is there anyway can write the end of file no matter type of file? tried setfilepointer(hfile, 0,(filesize - slackspace + 1),file_begin); didn't work.

you need heed info in documentation concerning file_flag_no_buffering. section:

as discussed, application must meet requirements when working files opened file_flag_no_buffering. next specifics apply:

file access sizes, including optional file offset in overlapped structure, if specified, must number of bytes integer multiple of volume sector size. example, if sector size 512 bytes, application can request reads , writes of 512, 1,024, 1,536, or 2,048 bytes, not of 335, 981, or 7,171 bytes. file access buffer addresses read , write operations should physical sector-aligned, means aligned on addresses in memory integer multiples of volume's physical sector size. depending on disk, requirement may not enforced.

c++ winapi

No comments:

Post a Comment