0

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);
Nejc Galof
  • 2,538
  • 3
  • 31
  • 70
Elliot
  • 71
  • 1
  • 11
  • This is completely wrong, and it has nothing to do with file names or opening files:`TCHAR filename[512] = _T("C://Windows/EXAMPLE.txt"); //... return(filename);` -- You are returning a pointer to a local variable (array). This is **undefined behavior**. When the function returns, that array points to garbage. – PaulMcKenzie Dec 24 '16 at 20:52

1 Answers1

1

try:

 hFile = CreateFile(text_file, FILE_APPEND_DATA | FILE_READ_DATA, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_HIDDEN, NULL);

instead of:

 hFile = CreateFile(text_file, FILE_APPEND_DATA, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_HIDDEN, NULL);

Note that FILE_SHARE_READ allows other calls of CreateFile ask for read permissions, it does not affect the read permissions of your file handle.

Ynon
  • 334
  • 2
  • 7
  • this time error 183 - ALREADY_EXISTS... why ? i am not creating new one i am reading.. – Elliot Dec 24 '16 at 19:00
  • use CREATE_ALWAYS instead of OPEN_ALWAYS, or delete the file. – Ynon Dec 24 '16 at 19:02
  • But i dont want to contents remove... i want to read them. – Elliot Dec 24 '16 at 19:03
  • I suggest you refer to: https://msdn.microsoft.com/en-us/library/windows/desktop/aa363858%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396 and read which flag does what. – Ynon Dec 24 '16 at 19:03
  • Was the line that failed with 183 the ReadFile call or the CreateFile call? – Ynon Dec 24 '16 at 19:05
  • Readfile i think.. wait actually i made a if statment that if file exists its opening the file at main function and not creating new one... – Elliot Dec 24 '16 at 19:08
  • I'm having a hard time understanding why are you writing the content to the file and then read it, if you already have it in the buffer which you have written? – Ynon Dec 24 '16 at 19:09
  • Because i am writing from different places, i am gathering them in that file.. so all of them not in one variable.. its little complicated – Elliot Dec 24 '16 at 19:12
  • this is only little part of the program – Elliot Dec 24 '16 at 19:13
  • Anyway, I think that the 183 error is set by a CreateFile call with CREATE_NEW as the dwCreationDisposition parameter. – Ynon Dec 24 '16 at 19:47
  • @Elliot If you use that `save_text` function anywhere, it is completely wrong and none of the suggestions or the answer will work. See my comment in the main thread. If the rest of your program is like this, where you're returning pointers to local variables, it isn't surprising you're getting weird results. – PaulMcKenzie Dec 24 '16 at 20:54
  • Paul i used that function little different, i dont think there is a problem about that ;) i changed to " OPEN_EXISTING " ( when opening for read and write) and it doesnt give any error. buy when i print BytesRead variable its says 0. but the file is full up with 5 kb text... so how can i reach that text ? – Elliot Dec 25 '16 at 11:13
  • Well despite i am not getting any error when i output the loginchar its being empty.. i really need help.. ReadFile(hFile, loginchar, 10000, &BytesRead, NULL); – Elliot Dec 25 '16 at 15:26
  • @Elliot When you return a pointer to a local variable, the behavior of the program is *undefined*. Again, if you're doing anything like this in any other part of your program, expect undefined behavior to happen in other places in your program. Also, you have `text_file`, `txt_file`, etc. Nowhere do you post how those variables get their values. If you are getting the values using the `save_text` function, your program is wrong. – PaulMcKenzie Dec 25 '16 at 16:03
  • You really need to **fix** your `save_text` function -- that is the bottom line. You could start by using `std::wstring` instead of `TCHAR*`. – PaulMcKenzie Dec 25 '16 at 16:08
  • Paul i am copying content of that variable to another variable. so nothing to worry about that. i am using contents of this function from different variable.. the problem at read. either way what do you exatly mean with " fix " – Elliot Dec 25 '16 at 19:35
  • @Elliot -- As I stated previously, if you wrote code anything like your `save_text` function, then it becomes a logical conclusion that you may be making the same mistakes in other places in your program. If that `save_text` function has no bearing, then why did you post it? Since you didn't post a [mcve], and instead asking us to trust that you're setting those variables "error free", sorry, can't do that. Basically, it would be a good thing if we are certain that it really is a file I/O issue, and not a pointer mismanagement / scoping issue. – PaulMcKenzie Dec 27 '16 at 05:35
  • @Eliot [Please read this](http://stackoverflow.com/questions/7769998/how-to-return-local-array-in-c). That is the basic issue with your function. – PaulMcKenzie Dec 27 '16 at 05:43