Jump to content
xisto Community
Sign in to follow this  
CaptainCPS-X

Help Spliting A String Into 3 Variables In C++ I have searched on the net but havent found much help

Recommended Posts

Hi there im new around here ^^, my original language is spanish so sorry for any wrong word i use,...

Im working on some features for my program to load a Favorite games list from a text file but the problem I have is not parsing the file, is processing a string and spliting it into 3 variables, then I will use them to insert as items in a List View control, I just dont know what is wrong in the following code...

//------------------------------------------------------------			TCHAR* pszRomname = NULL;		TCHAR* pszTitle= NULL;		TCHAR* pszHardware = NULL;		char romname[256] = "";		char title[256] = "";		char hardware[256] = "";		int i = 0;		char str[] = "mslug:Metal Slug Super Vehicle-001:Neo-Geo,";				char *pch;		pch = strtok(str,":");		while(pch != NULL)		{			if(i == 0) { sprintf(romname,"%s",pch); }			if(i == 1) { sprintf(title,"%s",pch); }			if(i == 2) { sprintf(hardware,"%s",pch); }			pch = strtok(NULL,":");			i++;		}		_stprintf(pszRomname, L"%s", romname);		_stprintf(pszTitle, L"%s", title);		_stprintf(pszHardware, L"%s", hardware);		//------------------------------------------------------------

The app crashes with this code, maybe is because im converting the strings in a wrong way or something, I need them at the end like TCHARs and not 'char' that's why i use the _stprintf()...

oh, btw if I remove this it will not crash but of course I need those strings to add the items in the List View control...

_stprintf(pszRomname, L"%s", romname);		_stprintf(pszTitle, L"%s", title);		_stprintf(pszHardware, L"%s", hardware);

ThanX in advance for any help, it will be really appreciated :ph34r:

SeeYaa!
^^

Share this post


Link to post
Share on other sites

I'm no expert when it comes to Windows stuff in C++ - however, I can tell you that your problem must be in converting the char* to TCHAR*. I checked the values of the variables, and they're fine. I couldn't find the _sprintf function when I looked around, so I can't help you there, but try checking the documentation for what you're using to make sure you're using that function correctly.

Share this post


Link to post
Share on other sites

i didn't see where you initialised your TCHAR variables... you've declared them as pointers (pointing at NULL) -- so you're probably just trying to send data into the VOID...try initialising them with: pszRomname = new char[256]; pszTitle = new char[256]; pszHardware = new char[256]; _stprintf(pszRomname, L"%s", romname); _stprintf(pszTitle, L"%s", title); _stprintf(pszHardware, L"%s", hardware); // insert them into your list ... // but don't forget to call // delete [] pszRomname; // delete [] pszTitle; // delete [] pszHardware;also, your recursion is a little big...don't forget you can increment pointer variables and and check the current pointer against ':'

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.