Alright, I'm back.
I have a question about how to open files.
I have been watching this one Indian guy explaining how to open a file (video is here at
https://www.youtube.com/watch?v=-iMGhSlvIR0). However, in his video, he uses fopen(), which is unsecure according to Visual Studio. Visual Studio recommends I use fopen_s(), which I do.
Now, I try to open the file, but it just so happens that it gives me an assertion error when I try to actually read something (because the FILE* struct returns NULL for some reason, and thus the fread() basically reads nothing and gives a breakpoint).
When I try to debug it, I find that I need to use a perror() function that only prints to the console log, which I don't have (and miserably tried to redirect to a .txt file with stderr and stdout).
My code is below:
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 50
|
//opens files
void openFile(HWND hWnd) {
tagOFNA ofn;
const int maxInt = 100;
char fileName[maxInt];
memset(&ofn, 0, sizeof(tagOFNA));
ofn.lStructSize = sizeof(tagOFNA);
ofn.hwndOwner = hWnd;
ofn.lpstrFile = fileName;
ofn.lpstrFile[0] = '\0';
ofn.nMaxFile = maxInt;
ofn.lpstrFilter = "PRTD files\0*.prtd\0";
ofn.nFilterIndex = 1;
GetOpenFileNameA(&ofn);
displayFile(ofn.lpstrFile, hWnd);
}
void displayFile(char* name, HWND hWnd) { //I pass the name and window to this function
FILE* file;
stdout > (FILE*)"log.txt";
stderr > stdout;
file = (FILE*)fopen_s(&file, name, "rb");
if (file != NULL) {
fseek(file, 0, SEEK_END);
int _size = ftell(file);
rewind(file);
char* data = new char(_size + 1);
fread(data, _size, 1, file);
data[_size] = '\0';
MessageBoxA(hWnd, data, "hi", MB_OK);
} else {
perror("Error: ");
MessageBoxA(hWnd, "bruh", "bruh moment error", MB_OK); //always happens
}
}
| |
Can I fix this, or do I have to do an entirely different method?
Much appreciated.
Edit: Before anybody starts hogging on the functions, they are declared already.