Fill array
-
Hi I have two char array which I need to fill by parsing the string. char * str = "1,aaa\r\n2,bbb\r\n3,ccc\r\n4,ddd\r\n" now the the first value(1,2,3,4 in this case ) has to fill in first array and second value(aaa,bbb,ccc,ddd in this case) has to fill in second array. Can anybody please help me how to do this. please provide me a sample code if possible. Thanks
does it not look as simple as tokenizing the strings on ',' and getting the last character of tokenized string to store in one array and the rest of the part in another after removing
\r\n
. If the last character is not a \n then only you need to add it to the array containing numbers.I am a HUMAN. I have that keyword (??? too much) in my name........ ;-)_AnsHUMAN_b>
-
Tokenize thrice. Once with "\r\n" as the delimiter to count the number of array elements. Allocate two new arrays with that size. Then repeat the previous tokenizing to split one line and then tokenize the got line with "," to get the number and the text. Keep an index counter and fill both the arrays simultaneously. You must be able to code what I said.
...byte till it megahertz...
-
does it not look as simple as tokenizing the strings on ',' and getting the last character of tokenized string to store in one array and the rest of the part in another after removing
\r\n
. If the last character is not a \n then only you need to add it to the array containing numbers.I am a HUMAN. I have that keyword (??? too much) in my name........ ;-)_AnsHUMAN_b>
-
Hi I have two char array which I need to fill by parsing the string. char * str = "1,aaa\r\n2,bbb\r\n3,ccc\r\n4,ddd\r\n" now the the first value(1,2,3,4 in this case ) has to fill in first array and second value(aaa,bbb,ccc,ddd in this case) has to fill in second array. Can anybody please help me how to do this. please provide me a sample code if possible. Thanks
As others already told you, the answer is tokenize the string alternating
","
and"\r\n"
as delimiters. Nobody will give you a sample nor will do the work for you; if you don't know how to tokenize a string, look at strtok, wcstok, _mbstok (CRT)[^] -
As others already told you, the answer is tokenize the string alternating
","
and"\r\n"
as delimiters. Nobody will give you a sample nor will do the work for you; if you don't know how to tokenize a string, look at strtok, wcstok, _mbstok (CRT)[^]its nothing like that I hv not tried, but some where I stucked , Here by I m putting my code what I hv done till nw. char* szStr = "2,aaa\r\n3,bbb\r\n4,ccc\r\n5,ddd\r\n6,eee\r\n" ; int len = strlen(szStr); char* tempstrlen = new char[len + 1 ]; char* tempstrst = new char[len + 1 ] ; char* tempstr1 = NULL ; char* tempstr2 = NULL ; int nlang[12] ; char* szTag[5] ; memset(tempstrlen,0x00,sizeof(char) * (len + 1)) ; memcpy(tempstrlen,szStr,sizeof(char) * len); memset(tempstrst,0x00,sizeof(char) * (len + 1)); memcpy(tempstrst,szStr,sizeof(char) * len) ; tempstr1 = tempstrlen ; tempstr1 = strtok(tempstrst,"\r\n"); // nlang[0] = atoi(tempstr1) ; szTag[0]= tempstr1 ; int nCount = 0 ; int nCountChar = 0 ; while((tempstrst != NULL) && (nCount != 4)) { tempstr1 = strtok(NULL,"\r\n"); szTag[nCount + 1]= tempstr1 ; nCount++ ; } int i = 0; char* txt = NULL ; for(i = 0 ;i < 5 ; i++ ); { txt = strtok(szTag[i],","); nlang[0] = atoi(txt) ; while(szTag!=NULL && i>=0) { txt = strtok(szTag[i],","); nlang[i] = atoi(txt); i++ ; } i++ ; } please go through that and tell me where I m going wrong.
-
its nothing like that I hv not tried, but some where I stucked , Here by I m putting my code what I hv done till nw. char* szStr = "2,aaa\r\n3,bbb\r\n4,ccc\r\n5,ddd\r\n6,eee\r\n" ; int len = strlen(szStr); char* tempstrlen = new char[len + 1 ]; char* tempstrst = new char[len + 1 ] ; char* tempstr1 = NULL ; char* tempstr2 = NULL ; int nlang[12] ; char* szTag[5] ; memset(tempstrlen,0x00,sizeof(char) * (len + 1)) ; memcpy(tempstrlen,szStr,sizeof(char) * len); memset(tempstrst,0x00,sizeof(char) * (len + 1)); memcpy(tempstrst,szStr,sizeof(char) * len) ; tempstr1 = tempstrlen ; tempstr1 = strtok(tempstrst,"\r\n"); // nlang[0] = atoi(tempstr1) ; szTag[0]= tempstr1 ; int nCount = 0 ; int nCountChar = 0 ; while((tempstrst != NULL) && (nCount != 4)) { tempstr1 = strtok(NULL,"\r\n"); szTag[nCount + 1]= tempstr1 ; nCount++ ; } int i = 0; char* txt = NULL ; for(i = 0 ;i < 5 ; i++ ); { txt = strtok(szTag[i],","); nlang[0] = atoi(txt) ; while(szTag!=NULL && i>=0) { txt = strtok(szTag[i],","); nlang[i] = atoi(txt); i++ ; } i++ ; } please go through that and tell me where I m going wrong.
Try to work around this snippet:
char *szStr = "2,aaa\r\n3,bbb\r\n4,ccc\r\n5,ddd\r\n6,eee\r\n";
char *token1 = strtok(szStr, ",");
char *token2 = strtok(NULL, "\r\n");// Get the first pair number/text from token1 and token2
int n = 1;
while (token1 != NULL && token2 != NULL && n < 5)
{
token1 = strtok(NULL, ",");
token2 = strtok(NULL, "\r\n");// Get the n-th pair number/text from token1 and token2
n++;
}modified on Friday, August 6, 2010 7:49 AM
-
Try to work around this snippet:
char *szStr = "2,aaa\r\n3,bbb\r\n4,ccc\r\n5,ddd\r\n6,eee\r\n";
char *token1 = strtok(szStr, ",");
char *token2 = strtok(NULL, "\r\n");// Get the first pair number/text from token1 and token2
int n = 1;
while (token1 != NULL && token2 != NULL && n < 5)
{
token1 = strtok(NULL, ",");
token2 = strtok(NULL, "\r\n");// Get the n-th pair number/text from token1 and token2
n++;
}modified on Friday, August 6, 2010 7:49 AM
-
Well, because you have indeed attempted something, I'll help with a very very basic implementation.
void Job()
{
char *data = strdup("1,aaa\r\n2,bbb\r\n3,ccc\r\n4,ddd\r\n");// count the dest array size char \*p = data; int n = 0; while(\*p) { if('\\r' == \*p) { n++; p++; } p++; } // create the dest arrays char \*\*text = new char\*\[n\]; int \*nums = new int\[n\]; // start splitting int i = 0; char \*tok = strtok(data, "\\r\\n"); while(tok) { char \*t = tok; while(\*tok) { if(',' == \*tok) { \*tok = 0; nums\[i\] = atoi(t); t = tok+1; } tok++; } text\[i\] = strdup(t); i++; tok = strtok(NULL, "\\r\\n"); } // clear all for(i = 0; i<n; i++) free(text\[i\]); delete \[\] text; delete \[\] nums; free(data);
}
It is not recommended to play around with too many pointers. Use std::string and std::vector in place of arrays. And use strtok_s(...) instead of strtok(...) to reduce those loops into simple function calls. I am using VC6 which doesn't have that and hence have written out those loops.
...byte till it megahertz...
-
wow. Thats ridden with so many flaws. The main one is using tokenizing on a const char*. Did you simply type that out here ?
...byte till it megahertz...
:-O It was lunch time and my stomach got the control over my brain! :laugh:
-
wow. Thats ridden with so many flaws. The main one is using tokenizing on a const char*. Did you simply type that out here ?
...byte till it megahertz...
Now it works properly:
char szStr[] = "2,aaa\r\n3,bbb\r\n4,ccc\r\n5,ddd\r\n6,eee\r\n";
char *token1 = strtok(szStr, ",");
char *token2 = strtok(NULL, "\r\n");std::vector<int> nums;
std::vectorstd::string strings;// Get the first pair number/text from token1 and token2
if (token1 != NULL && token2 != NULL)
{
nums.push_back(atoi(token1));
strings.push_back(token2);
}while (token1 != NULL && token2 != NULL)
{
token1 = strtok(NULL, ",");
token2 = strtok(NULL, "\r\n");// Get the n-th pair number/text from token1 and token2
if (token1 != NULL && token2 != NULL)
{
nums.push_back(atoi(token1));
strings.push_back(token2);
}
} -
its nothing like that I hv not tried, but some where I stucked , Here by I m putting my code what I hv done till nw. char* szStr = "2,aaa\r\n3,bbb\r\n4,ccc\r\n5,ddd\r\n6,eee\r\n" ; int len = strlen(szStr); char* tempstrlen = new char[len + 1 ]; char* tempstrst = new char[len + 1 ] ; char* tempstr1 = NULL ; char* tempstr2 = NULL ; int nlang[12] ; char* szTag[5] ; memset(tempstrlen,0x00,sizeof(char) * (len + 1)) ; memcpy(tempstrlen,szStr,sizeof(char) * len); memset(tempstrst,0x00,sizeof(char) * (len + 1)); memcpy(tempstrst,szStr,sizeof(char) * len) ; tempstr1 = tempstrlen ; tempstr1 = strtok(tempstrst,"\r\n"); // nlang[0] = atoi(tempstr1) ; szTag[0]= tempstr1 ; int nCount = 0 ; int nCountChar = 0 ; while((tempstrst != NULL) && (nCount != 4)) { tempstr1 = strtok(NULL,"\r\n"); szTag[nCount + 1]= tempstr1 ; nCount++ ; } int i = 0; char* txt = NULL ; for(i = 0 ;i < 5 ; i++ ); { txt = strtok(szTag[i],","); nlang[0] = atoi(txt) ; while(szTag!=NULL && i>=0) { txt = strtok(szTag[i],","); nlang[i] = atoi(txt); i++ ; } i++ ; } please go through that and tell me where I m going wrong.
AbhiHcl wrote:
its nothing like that I hv not tried, but some where I stucked...
Then use the debugger to find out where.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius