I'm trying to read file content, but despite it's able to write to same file. I can't read from it! The program is running as Adminstator.
I have Tried to give " FILE_SHARE_WRITE | FILE_SHARE_READ " rights, but still not work.
DWORD dwBytesWritten = 0;
unsigned long BytesRead = 0;
HANDLE hFile = INVALID_HANDLE_VALUE;
wchar_t text_file[MAX_PATH] = { 0 };
TCHAR *save_text(void) {
OPENFILENAME ofn = { 0 };
TCHAR filename[512] = _T("C://Windows/EXAMPLE.txt");
ofn.lStructSize = sizeof(ofn);
ofn.lpstrFilter = L"Txt files (*.txt)\0*.txt\0All Files\0*.*\0";
ofn.lpstrFile = filename;
ofn.nMaxFile = sizeof(filename);
ofn.Flags = OFN_NONETWORKBUTTON | OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST | OFN_LONGNAMES | OFN_EXPLORER | OFN_HIDEREADONLY;
ofn.nFilterIndex = 1;
return(filename);
}
void WriteToFile(TCHAR *wText)
{
wchar_t loginchar[1000];
hFile = CreateFile(text_file, FILE_APPEND_DATA, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_HIDDEN, NULL);
WriteFile(hFile, wText, wcslen(wText) * sizeof(wchar_t), &dwBytesWritten, NULL); // its writing without problem
ReadFile(hFile, loginchar, wcslen(loginchar) * sizeof(wchar_t), &BytesRead, NULL); // accses denied
ResultInFile(GetLastError()); // ResultInFile funcitons writes paramater to the file
//ResultInFile(BytesRead); // to see how many bytes read, but of course doesnt work..
CloseHandle(hFile);
}
// this is the how file created at main function :
hFile = CreateFile(txt_file, FILE_APPEND_DATA, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_HIDDEN, NULL);