PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Writing to Files in the Application Directory

Have any of you found a better way to write to files in the application directory other than this:

Code:
FILE *fp;

#if defined(_WIN32_WCE)
TCHAR pathname[MAX_PATH];
char filename[MAX_PATH];

// get the path and copy it into a char array
PocketPC::GetFullPathName( _T("filename.txt"), pathname );
for(int i=0; i<MAX_PATH; i++)
filename[i] = (char) pathname[i];

if(!(fp = fopen(filename, "w")))
#else
if(!(fp = fopen("filename.txt", "w")))
#endif
// couldn't open file...
return;

// file opened successfully
fprintf(fp, "blah blah blah");

fclose(fp);

I didn't want my files to be written to the "My Device" directory, and this is the best way I could come up with for keeping them in the application directory. The standard fopen command doesn't like TCHAR (unsigned short) types for the filename, so I just loop through to put them into a char array.

If this was stumping you too, feel free to use the code. If you see a potential failure in the code, please let me know.
I have two minor suggestions:

1. When inside the _WIN32_WCE scope you don't need the _T() macro since this will always result in a widechar string. So, you might as well make it explicit, using the L macro like this:
Code:
PocketPC::GetFullPathName( L"filename.txt", pathname );

2. When converting between different character codes, it's best to use one of the special functions written to handle these conversions. Just to be safe. Since a filename has a pretty restricted number of valid characters this doesn't really matter in your case (I think), but in general it's better to do something like this:
Code:
wcstomb(filename,pathname,MAX_PATH);

Reference for the wcstombs can be found on this link:
http://www.mkssoftware.com/docs/man3/wcstombs.3.asp
Well, I'm not sure I'm really answering your question directly, but this might help anyway...

Code:

// I usually just make this a global var for simplicity's sake...
TCHAR myEXEPath[128];

// Then I make this call during initialization...
GetModuleDirectory(GetModuleHandle(NULL), myEXEPath, sizeof(myEXEPath) / sizeof(TCHAR));
// You probably should check the return value for errors too

// This is the function that stores the path to my executable in myEXEPath above...
DWORD GetModuleDirectory(HMODULE hModule, LPTSTR szPath, DWORD dwLength) {
DWORD dwLen;
LPTSTR p;
if (0 == (dwLen = GetModuleFileName(hModule, szPath, dwLength)))
return(0);
p = szPath + dwLen;
while (p != szPath) {
if (TEXT('\\') == *--p) {
*(++p) = 0;
break;
}
}
return(p - szPath);
}

// Then later on, when I want to access a file in the same directory, it's easy (pretend this is in a real function somewhere as an example)...
TCHAR fileName[128] = L"";
wcscat(fileName, myEXEPath);
wcscat(fileName, L"myFile.txt");
HANDLE hFile = CreateFile(fileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_HIDDEN, NULL);
// ...And so on...

Not sure if that's any help, or if it's actually any better than what your doing now, but it's what I do, so if it helps, great!
Uhm, try this...

Code:

FILE *fp;

TCHAR pathname[MAX_PATH];

// try to get fullpath, works on wince and win32 so
// we don't need to write #if defined(_WIN32_WCE)...
if (PocketPC::GetFullPathName(_T("filename.txt"), pathname) == 0)
return;

// open a file with _tfopen that support TCHAR on wince and win32
if ((fp = _tfopen(pathname, _T("w"))) == NULL)
return;

// file opened successfully
fprintf(fp, "blah blah blah");

fclose(fp);


This works on wince and win32... Easy no? :)
D'oh! You know, come to think of it, I saw that method and didn't think to look at it further for this. Cool, thank you!
Thanks everyone! The code looks so nice and tidy now, it almost makes me giddy... almost. =)
a note on TCHAR...

TCHAR can be either UNICODE or ANSI. thats why it exists; on PPC TCHAR is always UNICODE (unsigned short *).

to convert char * to TCHAR...
Code:
USES_CONVERSION;
...
functionThatUsesTchar(A2T(myString));
...


to go the other way...
Code:
USES_CONVERSION;
...
functionThatUsesChar(T2A(myString));
...


you have to #include the appropriate header... dont remember what it is off the top of my head. if you search the docs you can find it. you HAVE to put "USES_CONVERSION;" in the code prior to the conversion.

(all this code works fine with PF, i use it in my PF apps)

hope i was of help
:)