Tuesday 15 April 2014

visual c++ - Unable to Read from File while using ReadFIle() function in C++ -



visual c++ - Unable to Read from File while using ReadFIle() function in C++ -

i facing issues while reading info file using readfile() function of c++ (microsoft specific probably).

here code

write on file

void clienta::sharepublickey() { printf("sharing public key\n"); handle hfile = null; hfile = createfile(text("d:\\my_proj\\shared\\publickeyb.txt"), // name of write generic_write, // open writing file_share_write, // not share null, // default security create_new, // create new file file_attribute_normal, // normal file null); // no attr. template if (hfile == invalid_handle_value) { //displayerror(text("createfile")); //_tprintf(text("terminal failure: unable open file \"%s\" write.\n"), argv[1]); return; } // _tprintf(text("writing %d bytes %s.\n"), dwbytestowrite, argv[1]); bool berrorflag = writefile( hfile, // open file handle pbpublickey, // start of info write dwpublickeylen, // number of bytes write &lpnumberofbyteswritten, // number of bytes written null); // no overlapped construction if (false == berrorflag) { // displayerror(text("writefile")); printf("terminal failure: unable write file.\n"); return; } else { if (lpnumberofbyteswritten != dwpublickeylen) { // error because synchronous write results in // success (writefile returns true) should write info // requested. not case // asynchronous writes. printf("error: dwbyteswritten != dwbytestowrite\n"); } else { // _tprintf(text("wrote %d bytes %s successfully.\n"), dwbyteswritten, argv[1]); } } closehandle(hfile); }

read file

void clienta::readpublickeyofotherpeer() { handle hfile = null; dword dwbytesread = 0; byte* readbuffer = null; overlapped ol = {0}; hfile = createfile(text("d:\\my_proj\\shared\\publickeyb.txt"), // file open generic_read, // open reading file_share_read, // share reading null, // default security open_existing, // existing file file_attribute_normal | file_flag_overlapped, // normal file null // no attr. template ); if (hfile == invalid_handle_value) { _tprintf(text("createfile\n")); _tprintf(text("terminal failure: unable open file \"%s\" read.\n")); printf("error %x\n", getlasterror()); return; } if( false == readfile(hfile, readbuffer, dwpublickeylen, &lpnumberofbyteswritten, &ol) ) { // displayerror(text("readfile")); printf("terminal failure: unable read file.\n getlasterror=%08x\n", getlasterror()); closehandle(hfile); return; } if (dwbytesread > 0 && dwbytesread <= dwpublickeylen-1) { readbuffer[dwbytesread]='\0'; // null character //_tprintf(text("data read %s (%d bytes): \n"), argv[1], dwbytesread); printf("%s\n", readbuffer); } else if (dwbytesread == 0) { //_tprintf(text("no info read file %s\n"), argv[1]); } else { // printf("\n ** unexpected value dwbytesread ** \n"); } retrievedpublicbytearray = readbuffer; closehandle(hfile); }

by sharepublickey method saving info in file. , have checked saves info on file , info on files seems valid.

and readpublickeyofotherpeer method reading file saved. reading not successful in out set found next line -

terminal failure: unable read file. getlasterror=000003e6

you passing uninitialized pointer readbuffer readfile. need buffer big plenty receive results.

c++ visual-c++ file-io msdn file-management

No comments:

Post a Comment