-2

I have a program that reads a file, returned from an OPENFILENAME. It has a large edit control. That is supposed to get the text from the file and let the user save it. I just cannot wrap my head around it. fopen(path, "r+") throws an exception for some odd reason.

Exact error message: "Exception thrown at 0x00007FF87B5A86E2 (ucrtbased.dll) in Notepad (C).exe: 0xC0000005: Access violation reading location 0x000000000000722B."

Here is the function reading it:

void ReadOpenedFile(char* path, HWND hTextBox)
{
// Warning suppressor because it doesn't build without fopen_s it seems.
#pragma warning (disable: 4996)
    FILE* f = fopen(path, 'r+');
    SetWindowText(hTextBox, f);
    fclose(f);
}

I had gotten it from the documents folder, but I knew because Windows protects it, I tried the music folder. It still didn't work. I tried many different websites. They all said the same approach to open files. I always get the same exception. I had tested the OPENFILENAME was returning the correct file path, and it does.

Any help is appreciated!

  • 1
    `'r+' == 0x722B`, when you need `"r+"`, then `SetWindowText(hTextBox, f);` also wrong, *f* not a string – RbMm Feb 04 '22 at 18:34
  • @RbMm what should i do then – TheWaterWave222 Feb 04 '22 at 18:35
  • If you are reading text from a file use `"rt"`. The `"r+"` is for when you wish to modify the file, which you don't. You need to read a string from the file with `fgets()` and also remove the trailing newline. Then pass *that* string to `SetWindowText()`. Don't ignore warnings. You have disabled warnings about functions like `scanf()` which Microsoft **wrongly** considers to be deprecated but that should not have prevented a warning about mismatched argument types. `SetWindowText()` does not take a `FILE*` argument. – Weather Vane Feb 04 '22 at 18:43
  • @WeatherVane ```SetWindowText(hTextBox, f)``` yes i removed that. Also, I want the file to be written to by the edit control, whatever the user types. – TheWaterWave222 Feb 04 '22 at 18:47
  • You closed the file so you can't anyway. When you filled the control value, you didn't need to update the file. – Weather Vane Feb 04 '22 at 18:48
  • @WeatherVane I am new at this so I am not understanding – TheWaterWave222 Feb 04 '22 at 18:50
  • When you filled the control value, you didn't need to update the file and you closed it. When you successfully get a new value from the edit box, open the file again with `"wt"`and write the new value (destroying the whole previous file). – Weather Vane Feb 04 '22 at 18:50
  • @WeatherVane I can't, the error is not going away – TheWaterWave222 Feb 04 '22 at 18:51
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/241737/discussion-between-madhav-balakrishnan-nair-and-weather-vane). – TheWaterWave222 Feb 04 '22 at 18:53
  • *"I have a program that reads a file"* - You do not. You have a program that **opens** a file. Reading a file is done, to no surprise, by calling `fread`. Important take away: C is not a programming language you'll get far with with guessing alone. You will need to get a few [books](https://stackoverflow.com/q/562303/1889329). – IInspectable Feb 05 '22 at 16:50
  • @IInspectable i do have a C course, but I haven't gotten to file I/O yet. – TheWaterWave222 Feb 05 '22 at 19:02

1 Answers1

1

You never read the file but only open it. It should be (beware untested):

void ReadOpenedFile(char* path, HWND hTextBox)
{
// Warning suppressor because it doesn't build without fopen_s it seems.
#pragma warning (disable: 4996)
    FILE* f = fopen(path, "rt");
    // error test for f == NULL omitted for brievety
    char buf[16384];            // adjust per you requirement up to ~32000
    size_t sz = fread(buf, 1, sizeof(buf) - 1, f);
    buf[sz] = '\0';
    SetWindowTextA(hTextBox, buf);
    fclose(f);
}
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252