Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Fill array

Fill array

Scheduled Pinned Locked Moved C / C++ / MFC
data-structuresjsonhelptutorial
14 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AbhiHcl
    wrote on last edited by
    #1

    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

    L _ S 3 Replies Last reply
    0
    • A AbhiHcl

      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

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      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...

      A 1 Reply Last reply
      0
      • A AbhiHcl

        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

        _ Offline
        _ Offline
        _AnsHUMAN_
        wrote on last edited by
        #3

        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>

        A 1 Reply Last reply
        0
        • L Lost User

          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...

          A Offline
          A Offline
          AbhiHcl
          wrote on last edited by
          #4

          Thanx for ur reply, Can u please gv me some ssample code as I m new in c++.

          L 2 Replies Last reply
          0
          • _ _AnsHUMAN_

            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>

            A Offline
            A Offline
            AbhiHcl
            wrote on last edited by
            #5

            can u please send me some sample code .

            1 Reply Last reply
            0
            • A AbhiHcl

              Thanx for ur reply, Can u please gv me some ssample code as I m new in c++.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              NO.

              ...byte till it megahertz...

              1 Reply Last reply
              0
              • A AbhiHcl

                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

                S Offline
                S Offline
                Sauro Viti
                wrote on last edited by
                #7

                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)[^]

                A 1 Reply Last reply
                0
                • S Sauro Viti

                  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)[^]

                  A Offline
                  A Offline
                  AbhiHcl
                  wrote on last edited by
                  #8

                  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.

                  S D 2 Replies Last reply
                  0
                  • A AbhiHcl

                    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.

                    S Offline
                    S Offline
                    Sauro Viti
                    wrote on last edited by
                    #9

                    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

                    L 1 Reply Last reply
                    0
                    • S Sauro Viti

                      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

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #10

                      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...

                      S 2 Replies Last reply
                      0
                      • A AbhiHcl

                        Thanx for ur reply, Can u please gv me some ssample code as I m new in c++.

                        L Offline
                        L Offline
                        Lost User
                        wrote on last edited by
                        #11

                        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...

                        1 Reply Last reply
                        0
                        • L Lost User

                          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...

                          S Offline
                          S Offline
                          Sauro Viti
                          wrote on last edited by
                          #12

                          :-O It was lunch time and my stomach got the control over my brain! :laugh:

                          1 Reply Last reply
                          0
                          • L Lost User

                            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...

                            S Offline
                            S Offline
                            Sauro Viti
                            wrote on last edited by
                            #13

                            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);
                            }
                            }

                            1 Reply Last reply
                            0
                            • A AbhiHcl

                              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.

                              D Offline
                              D Offline
                              David Crow
                              wrote on last edited by
                              #14

                              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

                              1 Reply Last reply
                              0
                              Reply
                              • Reply as topic
                              Log in to reply
                              • Oldest to Newest
                              • Newest to Oldest
                              • Most Votes


                              • Login

                              • Don't have an account? Register

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • World
                              • Users
                              • Groups