Welcome Guest ( Log In | Register )



 
Reply to this topicStart new topic
> Help Spliting A String Into 3 Variables In C++, I have searched on the net but havent found much help
CaptainCPS-X
post Apr 25 2007, 12:52 AM
Post #1


Newbie [ Level 1 ]
Group Icon

Group: Members
Posts: 1
Joined: 25-April 07
Member No.: 21,628



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...

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...

CODE
_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 smile.gif

SeeYaa!
^^
Go to the top of the page
 
+Quote Post
bluefish
post May 3 2007, 01:30 AM
Post #2


Member [ Level 2 ]
Group Icon

Group: Members
Posts: 71
Joined: 16-December 06
Member No.: 18,419



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.
Go to the top of the page
 
+Quote Post
polarysekt
post Sep 25 2007, 08:41 PM
Post #3


Advanced Member
Group Icon

Group: Members
Posts: 128
Joined: 12-February 05
From: St. Louis, MO
Member No.: 2,612



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 ':'
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Vulnerability Was Found In All Major Browsers(20)
  2. Php : Variables Included Dont Work In Functions(4)
  3. MSN Searched Added To Dogpile Searcher(9)
  4. Some Facts I Found(13)
  5. Neem: A Natural Treatment That Works?(12)
  6. Hottest Searched Keywords Over The Internet(7)
  7. My FTP Quit Working(10)
  8. User Authentication Session Handling Problems(14)
  9. Note: How To Retrieve Variables From Function To Another(5)
  10. Quickly Create Form Variables(5)
  11. Burn Iso To Hd?(20)
  12. Check Out This Interesting Information I Found.(0)
  13. Pascal For Beginners - Part One(7)
  14. Php String To Int Typecasting(6)
  15. C# Tutorial : Lesson 2 - Variables & Operators(0)
  1. Major Flaw Found In .ani File Through All Major Ms Operating Systems(2)
  2. How To Remove Query String Using Regular Expressions(4)
  3. Sorting A String Vector(0)
  4. Php Any Variable In String.(1)
  5. Folder Of Frontpage?(8)
  6. $_get Issue (urgent!)(11)
  7. Php Long Variables(5)
  8. Check For Occurence Of Substring In String(3)
  9. A Lot Of Infomation About Cod4 Found On The Internet(0)
  10. Strange Ascii Code 22 Character Detected In Connection String(9)
  11. Looking For The Perfect Cms...(6)
  12. String Theory(9)
  13. When Does Human Life Begin?(1)


 



- Lo-Fi Version Time is now: 7th October 2008 - 08:09 AM