Discussion:
fopen, errno 2, insane
(too old to reply)
r***@gmail.com
16 years ago
Permalink
I keep getting errno 2 (path does not exist) errors when attempting to
fopen filenames obtained from FileDlg in MFC.

If I copy the path (which I direct into the debugger) and through it
into Explorer it finds the file with no trouble.

Permissions are fine. Located locally. Could someone please help? I
already wasted 2 hrs into this.

*snip*
CString file_name = FileDlg.GetPathName();
FILE * pFile;
const char * name = (const char *)file_name.GetString();
output.Format(_T("trying to open this file: %s\n"),name);
OutputDebugString(output);
pFile = fopen ( name , "rb");
if (!pFile)
{output.Format(_T("Error reading file! Error code: %i. \n(Google
'errno %i' to see what it means)\n"),errno, errno);
AfxMessageBox(output);}
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Ulrich Eckhardt
16 years ago
Permalink
Post by r***@gmail.com
I keep getting errno 2 (path does not exist) errors when attempting to
fopen filenames obtained from FileDlg in MFC.
[...]
Post by r***@gmail.com
CString file_name = FileDlg.GetPathName();
FILE * pFile;
const char * name = (const char *)file_name.GetString();
Stop. If you need that cast, your code is wrong. Remove the cast, fix the
ensuing errors and your problem will vanish. Note that using casts and
especially C-style casts is something you should never do unless you
really, really know what you are doing.

My guess is that your encoding is messed up i.e. that CString, compiled with
_UNICODE, is using wchar_t and not char. TCHAR is not char! Further, note
that this is MFC/win32 specific, so you should actually redirect your
questions to a win32 newsgroup.

Uli
--
Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Goran
16 years ago
Permalink
Post by r***@gmail.com
const char * name = (const char *)file_name.GetString();
Did you compile with "_UNICODE"/"UNICODE"? If so, the above is bad,
since GetString gave you UTF-2 string and you cast it to, let's say,
"one-byte-per-char with unknown encoding" ;-). (And the next line in
your code works by accident ;-)).

You shouldn't use C casts if you can avoid them. And here, you can.
This oughta work:

pFile = _tfopen (FileDlg.GetPathName(), "rb");

HTH,

Goran.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Martin Bonner
16 years ago
Permalink
Post by r***@gmail.com
I keep getting errno 2 (path does not exist) errors when attempting to
fopen filenames obtained from FileDlg in MFC.
If I copy the path (which I direct into the debugger) and through it
into Explorer it finds the file with no trouble.
Permissions are fine. Located locally. Could someone please help? I
already wasted 2 hrs into this.
*snip*
CString file_name = FileDlg.GetPathName();
FILE * pFile;
const char * name = (const char *)file_name.GetString();
What happens if you remove this cast? If the compiler complains that
it can't convert const wchar_t * to const char *, then that is your
problem: you have a UTF-16 string, and you need to convert it to
something that fopen will understand.
Post by r***@gmail.com
output.Format(_T("trying to open this file: %s\n"),name);
OutputDebugString(output);
What does this output?

Are you running a Unicode build? If so, I suspect the %s expects a
const wchar_t *; you are passing a const char * which invokes
undefined behaviour, and unfortunately that behaviour is to just
output the string :-(.
Post by r***@gmail.com
pFile = fopen ( name , "rb");
if (!pFile)
{output.Format(_T("Error reading file! Error code: %i. \n(Google
'errno %i' to see what it means)\n"),errno, errno);
Help your users. Tell this which file too.
Post by r***@gmail.com
AfxMessageBox(output);}
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Seungbeom Kim
16 years ago
Permalink
Post by r***@gmail.com
pFile = fopen ( name , "rb");
if (!pFile)
{output.Format(_T("Error reading file! Error code: %i. \n(Google
'errno %i' to see what it means)\n"),errno, errno);
AfxMessageBox(output);}
Why don't you try strerror(errno) instead of directing users
to resort to Google?
--
Seungbeom Kim

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Loading...