943,520 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 4090
  • C RSS
Dec 23rd, 2008
0

Split / explode-like function

Expand Post »
Hello,
I need to decode certain packets which I need to split first. They are splitted using '@@@' (in this example I only use one '@').

I'm having this code now: (It's basic C code for a NDS platform)

  1. char buf2[256] = "test1@test2@test3@";
  2. char packets[30][256];
  3.  
  4. int main(int argc, char ** argv)
  5. {
  6. split(buf2);
  7. //here show code for NDS
  8. }
  9.  
  10. void split(char *original)
  11. {
  12. int x = 0;
  13. int i = 0;
  14.  
  15. while (original[i] != '\0')
  16. {
  17. if (original[i] == '@')
  18. {
  19. packets[x][i] = '\0';
  20. x++;
  21. }
  22. else
  23. {
  24. packets[x][i] = original[i];
  25. }
  26. i++;
  27. PA_WaitForVBL();
  28. }
  29. }

Result:
packets[0] = 'test1'
packets[1] = (nothing)
packets[2] = (nothing)

What am I doing wrong? [1] and [2] should be filled too I guess!

Thanks in advance.
Last edited by Darkmystery; Dec 23rd, 2008 at 12:00 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Darkmystery is offline Offline
3 posts
since Dec 2008
Dec 23rd, 2008
0

Re: Split / explode-like function

You might want to take a look at strtok.
Reputation Points: 12
Solved Threads: 3
Newbie Poster
mike_g is offline Offline
14 posts
since Jul 2008
Dec 23rd, 2008
0

Re: Split / explode-like function

or, you at least need another index for the characters you put into packets. When you switch to a new x index, you need to reset the output index to 0.

  1. void split(char *original)
  2. {
  3. int x = 0;
  4. int i = 0;
  5. int jj = 0;
  6.  
  7. while (original[i] != '\0')
  8. {
  9. if (original[i] == '@')
  10. {
  11. packets[x][jj] = '\0';
  12. x++;
  13. jj = 0;
  14. }
  15. else
  16. {
  17. packets[x][jj++] = original[i];
  18. }
  19. i++;
  20. }
  21. }

if the PA_WaitForVBL(); is necessary, what does it do?
Reputation Points: 344
Solved Threads: 116
Practically a Master Poster
Murtan is offline Offline
670 posts
since May 2008
Dec 23rd, 2008
0

Re: Split / explode-like function

And don't forget to terminate the last token when break the loop...
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Dec 23rd, 2008
0

Re: Split / explode-like function

Click to Expand / Collapse  Quote originally posted by Murtan ...
if the PA_WaitForVBL(); is necessary, what does it do?
Well, I'm programming on the Nintendo DS. ANd if I don’t put it, the program will function, MUCH faster than the screen rate and many things just won’t work at all.

And thanks guys, I got i now.

I still got another question:
I load the packet command in a string, but how do I compare it? (I'm still quite new at C)
Since this piece of code is not working:
  1. char cmd[3];
  2. memcpy(cmd,packets,3); // cmd contains 'MSG' (confirmed)
  3. if (cmd == "MSG") // not working
  4. {
  5. PA_OutputText(1, 1, lineNum, packets); //show on DS screen
  6. }
  7.  
  8. // not working either
  9. if ((cmd[0] == "M") && (cmd[1] == "S") && (cmd[3] == "G"))
  10. {
  11. PA_OutputText(1, 1, lineNum, packets); //show on DS screen
  12. }

Thanks in advance again.
Last edited by Darkmystery; Dec 23rd, 2008 at 3:34 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Darkmystery is offline Offline
3 posts
since Dec 2008
Dec 23rd, 2008
0

Re: Split / explode-like function

  1. char cmd[3]; // 3 byte array [0] [1] [2]
  2. memcpy(cmd,packets,3); // cmd contains 'MSG' (confirmed)
  3. if (cmd == "MSG") // not working
  4. // try memcmp(cmd, "MSG", 3) == 0
  5. {
  6. PA_OutputText(1, 1, lineNum, packets); //show on DS screen
  7. }
  8.  
  9. // not working either
  10. // if the last one was cmd[2] it might work
  11. if ((cmd[0] == "M") && (cmd[1] == "S") && (cmd[3] == "G"))
  12. {
  13. PA_OutputText(1, 1, lineNum, packets); //show on DS screen
  14. }
Reputation Points: 344
Solved Threads: 116
Practically a Master Poster
Murtan is offline Offline
670 posts
since May 2008
Dec 23rd, 2008
0

Re: Split / explode-like function

  1. //... memcpy...
  2. //... use memcmp(...,...,...);
  3.  
  4. //...
  5. //...to compare two string :
  6. int s_compare(const char *str1, const char *str2)
  7. {
  8. if (strlen(str1) != strlen(str2))
  9. return 1;
  10. for (;*str1!='\0';*str1++,*str2++)
  11. if (*str1 != *str2)
  12. return 1;
  13. return 0;
  14. }
  15.  
  16. // what's this...??
  17. if ((cmd[0] == "M") && (cmd[1] == "S") && (cmd[3] == "G"))
  18. {
  19. PA_OutputText(1, 1, lineNum, packets); //show on DS screen
  20. }
  21.  
  22. // maybe, if (cmd[0] == 'M')
Last edited by cikara21; Dec 23rd, 2008 at 4:29 pm.
Reputation Points: 47
Solved Threads: 69
Posting Whiz
cikara21 is offline Offline
340 posts
since Jul 2008
Dec 23rd, 2008
0

Re: Split / explode-like function

Thanks alot guys, got it all working now.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Darkmystery is offline Offline
3 posts
since Dec 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Please help me in libpng1.2.33
Next Thread in C Forum Timeline: Any function to restart from the beginning of the same line?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC