• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 263
  • Last Modified:

need help with strings

I am writing an FTP client program, what I need is tp parse some part of string


227 Entering Passive Mode (199,232,41,7,223,110)

in this line I need to have numbers after the 4th ',', and have them in one string or buffer

what I am currently doing is to strstr() and get the pointer to the 4th comma, I need help with how to get the rest of teh numbers and ssave them somewhere without comma


                char *p;
                p=calloc(50, sizeof(char)); // error check calloc
                int i;
                for(i=0 ; i<4 ; i++)
                {
                        p=strstr(respond,",");
                }

could you tell me also if it is the correct way to do it?
0
zahmadian
Asked:
zahmadian
  • 3
  • 2
1 Solution
 
Kent OlsenData Warehouse Architect / DBACommented:
Hi zahmadian,

That's the right idea, but the wrong implementation.  The strstr() function that you're calling will always return the same address as you always start from the same place.

It's an easy fix, though:

                char *p;
                char *s;

                p=calloc(50, sizeof(char)); // error check calloc
    //  Note that something needs to put the string into p.  So far you only have a buffer;

                int i;
                for(i=0, s=p; i<4 ; i++)
                {
                        s=strstr(s,",");
                        if (s == NULL)    // error check that there are enough commas
                        {}
                        ++s;
                }
     // *s* now points to the string "233,110)".  You can further split this easily with sscanf().




Good Luck!
Kent
0
 
zahmadianAuthor Commented:
Hi kent, thanks alot for strstr, correction, I actually have a problem with sscanf implimentation:

sscanf(s, %d);

?
0
 
zahmadianAuthor Commented:
the problem is that, I dont know how many integer I have after the last comma
it could be like 123,3456) or 32,5342) or 2,78) or any other combination, so I should first count how many numbers I have is it the right way to count it?
in the while loop


 char *p;
                char *s;
                char *q;
                p=calloc(50, sizeof(char)); // error check calloc
               
                s=calloc(50, sizeof(char)); // error check calloc
                 
                       
                q=calloc(50, sizeof(char)); // error check calloc
               
               
//  Note that something needs to put the string into p.  So far you only have a buffer;

                int i;  
                p=strstr(respond,"(");
                       
                for(i=0, s=p; i<4 ; i++)
                {
                        s=strstr(s,",");
                        if (s == NULL)    // error check that there are enough commas
                        {}
                        ++s;
                }
     // *s* now points to the string "233,110)".  You can further split this easily with sscanf().
                int j; // counter
                j=0;
                q=s;
                while(*q!=strstr(s,')'))
                {
                     j++;
                }
                j=j-1; // - comma character
                sscanf(s, ) // second parameter should it be j times %d? how can I write it like that?


0
 
Kent OlsenData Warehouse Architect / DBACommented:
Hi zahmadian,

You dont really care how many digits is in each integer as long as you know how many integers are in the list.

  if (sprintf (s, "%d,%d", &Number1, &Number2) == 2)
  //  sprintf parts 123,456 or 1,123151 or 12351089,8  etc.
  else
  //  not enough numbers were found.


Kent
0
 
zahmadianAuthor Commented:
Thanks Kent!

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now