Advertisement

opening files

Started by April 03, 2003 07:47 AM
3 comments, last by pIpEs ClaNGoR 21 years, 10 months ago
I am trying open and save my files with the windows dialog box but I need to change ofn.lpstrFile from char to a const char. Because I can use this to open my files with fopen but when I close them the program crashes. Is there any way around this? I hope that makes sense! If not just tell me. [edited by - pipes clangor on April 3, 2003 10:15:56 AM]
---------------------------The pipes clangor all the time.
Try:

CFileDialog dlgOpen(TRUE);// Set up your dialog''s filter with:// dlgOpen.m_ofnif(dlgOpen.DoModal()==IDOK){     CString szFullFilePath = dlgOpen.GetPathName();     FILE* pFile = fopen(LPCTSTR(szFullFilePath), "rb");          if(pFile)     {          // Read from the file.          fclose(pFile);     }} 


I''m not sure what would cause your crash. I''m pretty sure it''s not your string, unless you''re doing something funny with allocating/deallocating it. Since you''re already using MFC to open, this is a pretty Windows/MFC centric solution. Your issue may be that you''re trying to open the file and it''s not succeeding, so your File pointer is invalid?

Hard to say really without any code.

Good luck.

- sighuh?
- sighuh?
Advertisement
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hwnd;
ofn.lpstrFile = szFile;
ofn.nMaxFile = sizeof(szFile);
ofn.lpstrFilter = "My Files\0*.sex\0All\0*.*\0";
ofn.nFilterIndex = 1;
ofn.lpstrFileTitle = NULL;
ofn.lpstrInitialDir = NULL;
ofn.lpstrDefExt = "*.sex";

if (GetOpenFileName(&ofn)==TRUE)
hf = CreateFile(ofn.lpstrFile, //drive, dir, and file name
GENERIC_READ,
0,
(LPSECURITY_ATTRIBUTES) NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
(HANDLE) NULL);

FILE *filein = fopen(ofn.lpstrFile, "rb"); //will open here
fclose(filein); /*when filein closes this is where it
crashes! (strange because I didn't think
filein would have a prob
with ofn.lpstrFile*/

[edited by - pipes clangor on April 3, 2003 12:01:32 PM]
---------------------------The pipes clangor all the time.
you could as well use the GetBuffer function of CString

don t know if it returns a pointer or if you have to pass an array right now but you can use the normal "char" data type
http://www.8ung.at/basiror/theironcross.html
quote:
Original post by Basiror
you could as well use the GetBuffer function of CString

don t know if it returns a pointer or if you have to pass an array right now but you can use the normal "char" data type

It returns the actual buffer, so if you edit it like a char*, those changes will be reflected in the CString. After you call this, do a .ReleaseBuffer() so that it knows to manage the CString again. CStrings do a lot of weird stuff...I prefer casting them to const char*'s (LPCTSTR())...


- sighuh?

[edited by - redragon on April 7, 2003 9:48:56 AM]
- sighuh?

This topic is closed to new replies.

Advertisement