split string into two arrays

Hello guys, i hope you all fine.
suppose that i have an array of characters containing data in this form
int@other_data
how can i get the int part alone and other_data ??
if for example we have data = 123@hello1245ss
i need to get
p1=123
p2=hello1245ss
i've tried the following function but it only works fine with string data
but fails if used with files (input/output read/write)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int segSplit(char* buf,int bufSize)
{
    char * pnt;
    pnt=strtok( buf, "@" ); //extract seqNum
    if(pnt != NULL) 
    {
      int seqNum = atoi(pnt); //string to int 
      int data_pos=strlen(pnt)+1;
      int data_size=bufSize-data_pos;
      memmove (buf,buf+data_pos,data_size); //extract data 
      buf[data_size]='\0'; ///* null character manually added */
      return seqNum;
    }
}
use sscanf or stringstream:

1
2
3
4
5
6
#include <cstdio>
...
char *string = {"123@hello1245ss"};
int p1;
char p2[50];
sscanf(string, "%d@%s", &p1, p2);


1
2
3
4
5
6
7
#include <sstream>
...
std::string word = "123@hello1245ss";
int p1;
char temp;
std::string p2(50, '\0');
std::istringstream (word) >> p1 >> temp >> p2;
Last edited on
Thank you very much
but i have same problem when dealing with files not just string word
i will post the problem in details soon.
You can read from the file straight into a stringstream, then do whatever you need to with that

1
2
3
4
5
6
7
8
 stringstream ss("");
  if (myfile.is_open())
  {
    while ( getline (myfile,line) )
    {
       ss << line << '\n';
    }
    myfile.close();
Please guys check the problem in details here
http://www.cplusplus.com/forum/general/122721/
thank you very much
Topic archived. No new replies allowed.