I have a file called save1.sav that contains the following data:
1:1:544
What's the best way to read each value separated by ":"?
I tried to read it this way:
- Code:
TCHAR sep[2];
TCHAR ls_Temp[10000];
TCHAR *token;
_tcscpy(sep, TEXT(":"));
FILE *fp = _tfopen(GetPath(TEXT("save1.tms")), TEXT("r"));
if(fp == NULL) return;
_ftscanf(fp, "%s", &ls_Temp);
token = _tcstok(ls_Temp, sep);
data1 = _ttoi(token);
token = _tcstok(ls_Temp, sep);
data2 = _ttoi(token);
token = _tcstok(ls_Temp, sep);
data3 = _ttoi(token);
...But it crashes in my pocket pc and I have no idea if it reads the contents whatsoever.
What do you use to read a file with data separated by a delimiter?
Thanks in advance for any input you can share.
Regards,
Erick :)
Oh, and it works on the desktop by the way. But it kept crashing on my Pocket PC.
- Code:
Please(Pinch_of_salt++); // :)
Check the return from _ftscanf() ?
Switch the ls_Temp in the 2nd and 3rd calls to NULL?
- Code:
token = _tcstok(ls_Temp, sep);
data1 = _ttoi(token);
token = _tcstok(NULL, sep);
data2 = _ttoi(token);
token = _tcstok(NULL, sep);
data3 = _ttoi(token);
Why don't you simply do it as:
- Code:
_ftscanf(fp, "%d:%d:%d", &data1;, &data2;, &data3;);
It works great on the desktop and reads the values correctly, however, in my Pocket PC, it hangs. Thanks for the suggestions, I'll try some more workarounds.
I could be wrong, but I'm sure ALL the string parameters in 'wide' functions must be 'wide', so you should use the TEXT macro.
- Code:
_ftscanf(fp, "%s", &ls;_Temp);
should probably read:
- Code:
_ftscanf(fp, TEXT("%s"), &ls;_Temp);
Would explain why it works fine on the desktop
Jasper
Quick OT question: these boxes of "code" people have in their posts, I never see anything in them. Do I have to configure something to see them?
Are you using IE5 or 5.5? If you can, update to IE6, but not before you post for Dan East what you're using so he can try to resolve the problem.

Can you select the text in them? That should narrow down the problem.
Here's some code I use in Dr. Pocket:
- Code:
FILE *DrPFile(char *filename, char *flags)
{
#if defined(_WIN32_WCE)
TCHAR tpathname[MAX_PATH], tfilename[MAX_PATH];
char pathname[MAX_PATH];
mbstowcs(tfilename, filename, MAX_PATH);
PocketPC::GetFullPathName( tfilename, tpathname );
wcstombs(pathname, tpathname, MAX_PATH);
return fopen(pathname, flags);
#else
return fopen(filename, flags);
#endif
}
void Scores::ReadScores()
{
#if defined(_TRIALVERSION)
return;
#else
FILE *fp = DrPFile("drpscore.txt", "r");
int type, level, colors, speed, score;
if(!fp)
{
SaveScores();
return;
}
while(fscanf(fp, "[%d][%d][%d][%d] %d\r", &type;, &level;, &colors;, &speed;, &score;) > 0)
{
if((type < 2) && (level < 20) && (colors < 2) && (speed < 3))
highScore[type][level][colors][speed] = score;
}
fclose(fp);
#endif
}
Works every time, on both PC and PPC.
On reflection, you must already be using the TEXT macro on that parameter, otherwise it shouldn't compile

I am using IE 5.00.3315.1000 on Windows 2000. I only see a single line box with "Code:" and no other text, even when I select all the text in a post. I see some funky div span stuff in the HTML source though, with code fragments.
Back to generic text, I'd been meaning to upgrade to some sort of Text class that would encapsulate a _TCHAR* and manage it for me. I'm starting to do more parsing of text files (e.g. configuration file, my game level files, etc.) and after some prototype code, things are getting hairy enough that I want to clean it up.
I was thinking about maybe using vector<TCHAR> in the implementation. I though about string/wstring, but wasn't sure, and didn't think about just using basic_string<_TCHAR>.
But now I did just that, using typedef std::basic_string<_TCHAR> tstring. Now I use tstring everywhere in my code, just as I used to use std::string.
Initialization and copying is trivial. Memory management is automatic. Interfacing with legacy APIs (snprintf etc.) is via methods c_str() and size().
Note though that if you want to copy a _TCHAR* into an existing tstring, you have to either put it in a vector<_TCHAR> first (since tstring may not be contiguous memory), or just construct a new tstring initialized with the _TCHAR* and copy that new tstring into your existing tstring via the copy assignment operator.
Again, see Meyers' book Effective STL for how to safely interface std::string with legacy C APIs.