IO file operation with c++ functions instead of Win32 commands

I was reading this tutorial: //www.winprog.org/tutorial/app_two.html about doing a simple notepad like text editor. However in the part about "Reading and Writing Files" the author talks about doing IO with other methods instead using the win 32 api commands. So I tried to do it making these modifications to the code of the bellow function. I substituted nearly every line of his code by what I thought would be the equivalent of c++ standard IO.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
BOOL LoadTextFileToEdit(HWND hEdit, LPCTSTR pszFileName)
{
    fstream file;
    //HANDLE hFile;
    BOOL bSuccess = FALSE;

      file.open ("a.txt");
    //hFile = CreateFile(pszFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
        //OPEN_EXISTING, 0, NULL);
      if (!file.is_open())
         return bSuccess;
      else          
    //if(hFile != INVALID_HANDLE_VALUE)
      {
            int fileSize;
          //DWORD dwFileSize;  

          file.seekg (0, ios::end);
          fileSize = file.tellg();
        //dwFileSize = GetFileSize(hFile, NULL);
          if(fileSize >0) 
        //if(dwFileSize != 0xFFFFFFFF)
        {
              char * allocateFile;
            //LPSTR pszFileText;
              allocateFile = new char [fileSize+1];
            //pszFileText = (LPSTR)GlobalAlloc(GPTR, dwFileSize + 1);
            //if(pszFileText != NULL)
            if (allocateFile != 0)
            {
                DWORD dwRead;
                if(ReadFile(file, allocateFile, fileSize, &dwRead, NULL))
                //if(ReadFile(hFile, pszFileText, dwFileSize, &dwRead, NULL))
                {
                    allocateFile[fileSize] = 0;                 
                    //pszFileText[dwFileSize] = 0; // Add null terminator
                    if(SetWindowText(hEdit, allocateFile));
                    //if(SetWindowText(hEdit, pszFileText))
                        bSuccess = TRUE; // It worked!
                }
                   delete [] allocateFile;
                //GlobalFree(pszFileText);
            }
        }
          file.close();
        //CloseHandle(hFile);
    }
    return bSuccess;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void DoFileOpen(HWND hwnd)
{
	OPENFILENAME ofn;
	char szFileName[MAX_PATH] = "";

	ZeroMemory(&ofn, sizeof(ofn));

	ofn.lStructSize = sizeof(OPENFILENAME);
	ofn.hwndOwner = hwnd;
	ofn.lpstrFilter = "Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0";
	ofn.lpstrFile = szFileName;
	ofn.nMaxFile = MAX_PATH;
	ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
	ofn.lpstrDefExt = "txt";

	if(GetOpenFileName(&ofn))
	{
		HWND hEdit = GetDlgItem(hwnd, IDC_MAIN_EDIT);
		LoadTextFileToEdit(hEdit, szFileName);
	}
}


The problem is that the text of file being loaded isn't showing in the edition menu, it keeps blank, so I wonder in what I did wrong or/and what else can be missing.

EDIT: I also included the code of the "DoFileOpen" function that calls the first function that I posted, because maybe some of my modifications, like including the name of a fixed file (a.txt) doesn't work with the way this first function deals with filenames
Last edited on
I'm beginning to feel like an evangelist rather than a programmer.

Do not try to find the file size like this:
1
2
file.seekg (0, ios::end);
fileSize = file.tellg();
Use stat().

Do not use open/close of file streams, use the constructor and let the destructor close the stream.

allocateFile = new char [fileSize+1];If this fails, it will throw an exception.

You can't use ReadFile with an fstream. You need to use the stream's read method. An that means you should have opened the stream as binary.
Thanks for the advice, I was able to open it now.

But I would like to know what is the advantage of using stat() and the constructor-destructor pair.

The second case is because is more object oriented this way?

And by last, a new doubt: How can I open a generic file, instead of hard-coding it, like a did with
file.open ("a.txt"), like was in the tutorial?
Topic archived. No new replies allowed.