Jump to content

QUESTION Strcat/Strstr/strcpy Question

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
wgre0111

wgre0111

    Newbie

  • Members
  • Pip
  • 3 posts
Greetings,

I am making an update to a ESQLC program that is using topend to communicate with Windows..

I have having a problem separating a string.

I have column on a table that holds the address of a printer in the following format. 111.111.111.111:4000
Its not always guaranteed to be that length.

I pull that list back via FETC into.... but NOW i want to split it into two different variables. One with the IP and the other with the Port.
so char oIPnPort[] = "111.123.123.123:5000";
would be split up and copied into two separate variables

char oIP[] = "111.1223.123.123";
char oPort[] = "5000";

SOMETIME though there will be instances where there is NO port data available... for instance somtime it will just be the IP address with NO :'s at all... I need a test to see if the : is present and if it is split the two sections up... ELSE set the PORT to \0 or just null....

Could someone give me an example of how to do this via C???

ALSO
Use of pointers or anything else would be ok... just need help...

This split would remove the : between the port and IP as well... I know I want to copy whats to the right of the : in one variable"port" and whats to the left of the : in IP.

THANKS!

Edited by wgre0111, 19 October 2008 - 02:27 PM.


#2
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts
#include <stdio.h>

int main(void)
{
   const char oIPnPort[] = "111.123.123.123:5000";
   char oIP[16], oPort[6];
   if ( sscanf(oIPnPort, "%15[^:]:%5s", oIP, oPort) == 2 )
   {
      printf("oIP = \"%s\", oPort = \"%s\"\n", oIP, oPort);
   }
   return 0;
}

/* my ouput
oIP = "111.123.123.123", oPort = "5000"
*/