Thread: how can i truncate this

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    136

    how can i truncate this

    hi all
    i have an output like this
    Code:
    ID=123:one=a:[email protected]:three=789:four=:five=123 ID=123:one=aa:[email protected]:three=:four=1111111111:five=
    :/
    i would like to truncate this write in a file (file.txt) as
    Code:
    //begin
    a
    [email protected]
    789
    
    123
    //begin
    aa
    [email protected]
    
    11111111
    
    //begin
    how can do this

    can you please help em
    thank you in advance

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by munna_dude View Post
    hi all
    i have an output like this
    Code:
    ID=123:one=a:[email protected]:three=789:four=:five=123 ID=123:one=aa:[email protected]:three=:four=1111111111:five=
    :/
    i would like to truncate this write in a file (file.txt) as
    Code:
    //begin
    a
    [email protected]
    789
    
    123
    //begin
    aa
    [email protected]
    
    11111111
    
    //begin
    how can do this

    can you please help em
    thank you in advance
    You could put this string into a char array and then scan through each char. The equal sign precedes the info you want written out to a file. So when the letter you're looking at is an equal sign, you know that you need to write out the next several char's, until you reach the colon, which marks the end of the field.

    What part are you stuck on? Show me your code to handle this, and explain your specific troubles, please.

    Adak

  3. #3
    Registered User
    Join Date
    Dec 2006
    Posts
    136

    thank you for quick replay

    Quote Originally Posted by Adak View Post
    You could put this string into a char array and then scan through each char. The equal sign precedes the info you want written out to a file. So when the letter you're looking at is an equal sign, you know that you need to write out the next several char's, until you reach the colon, which marks the end of the field.

    What part are you stuck on? Show me your code to handle this, and explain your specific troubles, please.

    Adak
    Code:
    #include <netdb.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <linux/sockios.h>
    #include <string.h>
    #include <math.h>
    #include <dirent.h>
    
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <errno.h>
    #include <unistd.h>
    #include <syslog.h>
    #include <stdio.h>
    
    void write_data();
    
    
    void write_data()
    {
    
    char *response="ID=123:one=a:[email protected]:three=:four=456:five=\nID=123:one=aa:[email protected]:three=22222222:four=:five=0000000000\n:/";
    
                
            FILE *appenddatabase;
            char appendfile[256];
         
     
    char tempfile[256] = "/root/Desktop/1234/MyFriends.txt";
    
            appenddatabase = fopen(tempfile, "r");
             	if (appenddatabase == NULL)
            appenddatabase = fopen(tempfile, "w");               
    
          
                char *delims="\n";
                char *result = NULL;
                result = strtok(response,"\n");
                printf( "refresh Contact response %s\n",response);
                int count=0; 
    
    
    
                while(result!= NULL ) {
                 
                 count=count+1;
    
                   if(strstr(result,":/")!=NULL) {result=NULL;continue;}
    
                if(count>0)
                {     	
                  char **ref;
                  int j;
    	      ref=g_strsplit(result,":",-1);
                  
    char *one="",*two="",*three="",*four="",*five="";
                  for(j=0;j<7;j++)
                  {
    
    
                   if(strstr(ref[j],"one")!=NULL)
    		{
                    char **split1;
    		split1=g_strsplit(ref[j],"=",2);
                    one=split1[1];
    		printf("%s\n",split1[1]);
                    
    		}
                    else if(strstr(ref[j],"two")!=NULL)
    		{
                    char **split2;
    		split2=g_strsplit(ref[j],"=",2);
    		printf("%s\n",split2[1]);
                    two=split2[1];
                    
    		} 
                    else if (strstr(ref[j],"three")!=NULL)
    		{
                    char **split5;
    		split5=g_strsplit(ref[j],"=",2);
    		printf("%s\n",split5[1]);
                    three=split5[1];
    		}  
                    else if(strstr(ref[j],"four")!=NULL)
    		{
                    char **split4;
    		split4=g_strsplit(ref[j],"=",2);
    		printf("%s\n",split4[1]);
                    four=split4[1];
    		} 
                    else if(strstr(ref[j],"five")!=NULL)
    		{
                    char **split3;
    		split3=g_strsplit(ref[j],"=",2);
    		printf("%s\n",split3[1]);
                    five=split3[1];
    		}
                    else {} 
                   
                  }
                   fprintf(appenddatabase, "__BEGIN_\n"); 
                   fprintf(appenddatabase, "%s\n", one);
                   fprintf(appenddatabase, "%s\n", two);
                   fprintf(appenddatabase, "%s\n", three);
                   fprintf(appenddatabase, "%s\n", four);
                   fprintf(appenddatabase, "%s\n", five); 
                } 
                result = strtok( NULL, delims );
    
                }
    
    
             fclose(appenddatabase);
    
    
    }
    
    int
    main(int argc, char *argv[])
    {
      gtk_set_locale ();
      gtk_init (&argc, &argv);
    
    write_data();
    
       gtk_main ();
       return 0;
    }
    save it as one.c
    and compile
    gcc one.c -o one `pkg-config gtk+-2.0 --cflags --libs`

    i used some gtk functions here.

    i know now you are clear about this

    please help me

    thank you in advance

  4. #4
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    You're already using strtok() but unfortunately only with the '\n' delimiter. Instead, why don't you use it a little more advanced:

    1. split your input using the delimiter ':', this will give you a sequence of "option=value" items (as long as neither option nor value contains the ':').

    2. split your sequence of "option=value" with the delimiter '=' which will give you the 2 items option and value

    3. print the value corresponding to your options.

    The whole thing shouldn't take more than .... 10 lines max.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Wow! That's a boatload of code!!

    I don't mean to be unkind, but it seems to be suffering from the all too familiar "Rube Goldberg" effect.


    Let's look at the problem again:
    ID=123ne=a:[email protected]:three=789:four=:five=123 ID=123ne=aa:[email protected]:three=:four=1111111111:five =
    :/

    i would like to truncate this write in a file (file.txt) as
    Code:
    //begin
    a
    [email protected]
    789
    
    123
    //begin
    aa
    [email protected]
    
    11111111
    //begin
    What I was thinking of was just two while loops:

    Code:
    While (there are more char's to be looked at in the string)  {
       ch = next char.
       if (ch == '=')
             while (1)  {  
                  (ch2 = next char in the string 
                  if ((ch2 != ':') && (ch2 != NULL))
                      write ch2 to the file;
                  else 
                      break;
             }
    }
    The loops could be for loops working from 0 to strlen(string) - 1.

    I'm sure the half-code above is incomplete, but I hope it conveys my thoughts on this. I don't think you want "one" "two" --> "seven", etc. Make it something generic so it will handle any number of entries, and at the same time, make it as simple, small and clear as possible.

    Adak

  6. #6
    Registered User
    Join Date
    Dec 2006
    Posts
    136

    thank you for quick replay

    Quote Originally Posted by KONI View Post
    You're already using strtok() but unfortunately only with the '\n' delimiter. Instead, why don't you use it a little more advanced:

    1. split your input using the delimiter ':', this will give you a sequence of "option=value" items (as long as neither option nor value contains the ':').

    2. split your sequence of "option=value" with the delimiter '=' which will give you the 2 items option and value

    3. print the value corresponding to your options.

    The whole thing shouldn't take more than .... 10 lines max.
    no am unable to write the code in 10 lines

    can anybudy rewrite the code please

    thank you in advance

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    No, I couldn't write the program, I don't use that compiler and don't have those header files. More importantly, you get better at programming if you practice - like anything else.

    I know it's time consuming and it can be frustrating, but I believe you have two ways of doing this program, laid out before you.

    Adak

  8. #8
    Registered User
    Join Date
    Dec 2006
    Posts
    136

    thank you for quick replay

    Quote Originally Posted by Adak View Post
    No, I couldn't write the program, I don't use that compiler and don't have those header files. More importantly, you get better at programming if you practice - like anything else.

    I know it's time consuming and it can be frustrating, but I believe you have two ways of doing this program, laid out before you.

    Adak
    thank you for boosting.

    i succeeded.
    but still having a problem
    it will not printing
    fist "five"(i.e. 123),and it is printing remaining "five".

    whats the problem.

    can you please help me .

    thank you in advance

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Could it have anything to do with the odd numbering in the code?

    Code:
    if(strstr(ref[j],"one")!=NULL)
    		{
                    char **split1;
    		split1=g_strsplit(ref[j],"=",2);
                    one=split1[1];
    		printf("%s\n",split1[1]);
                    
    		}
                    else if(strstr(ref[j],"two")!=NULL)
    		{
                    char **split2;
    		split2=g_strsplit(ref[j],"=",2);
    		printf("%s\n",split2[1]);
                    two=split2[1];
                    
    		} 
                    else if (strstr(ref[j],"three")!=NULL)
    		{
                    char **split5;
    		split5=g_strsplit(ref[j],"=",2);
    		printf("%s\n",split5[1]);
                    three=split5[1];
    		}  
                    else if(strstr(ref[j],"four")!=NULL)
    		{
                    char **split4;
    		split4=g_strsplit(ref[j],"=",2);
    		printf("%s\n",split4[1]);
                    four=split4[1];
    		} 
                    else if(strstr(ref[j],"five")!=NULL)
    		{
                    char **split3;
    		split3=g_strsplit(ref[j],"=",2);
    		printf("%s\n",split3[1]);
                    five=split3[1];
    		}
                    else {} 
                   
                  }
    I can't run your program, so you'll have to settle for my idea's on this, only.

    Adak

  10. #10
    Registered User
    Join Date
    Dec 2006
    Posts
    136

    thank you for quick replay

    sorry.
    i forgot some code.
    actually this is the response
    Code:
    char *response="LIST=SUCCESS:ID=123:one=a:[email protected]:three=:four=456:five=666\nID=123:one=aa:[email protected]:three=22222222:four=:five=0000000000\n:/";
    there is an extra ":" in first line so,
    the first "five"(i.e. 666) is not printing.

    can you please help me in this way

    thank you in advance

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by munna_dude View Post
    sorry.
    i forgot some code.
    actually this is the response
    Code:
    char *response="LIST=SUCCESS:ID=123:one=a:[email protected]:three=:four=456:five=666\nID=123:one=aa:[email protected]:three=22222222:four=:five=0000000000\n:/";
    there is an extra ":" in first line so,
    the first "five"(i.e. 666) is not printing.

    can you please help me in this way

    thank you in advance
    Above the loop that has you checking each char, put a while loop into your code. Conceptually, something like this:

    Code:
    /* n is an int, and you may need to use the ASCII value of '=', instead of '=' (which is 61) */
    while ((n = getchar()) != '=');  /* gets char's and converts to int's until it reaches an equal sign. */
    Note the double (( before the n, and a single extra ) after the getchar, as well as the ; at the end of the compact while statement.

    This is not the best answer to your needs, but it may be ONE answer, in this case.

    Adak

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    /* n is an int, and you may need to use the ASCII value of '=', instead of '=' (which is 61) */
    No, you never have to. In C, character literals are actually int values anyway, but even in C++, where they are chars, you don't have to, because it's casted implicitly.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. truncate help
    By munna_dude in forum Linux Programming
    Replies: 3
    Last Post: 10-18-2007, 06:04 AM
  2. is there a way to truncate an array
    By panfilero in forum C Programming
    Replies: 1
    Last Post: 11-03-2005, 04:07 AM
  3. MYSQL++ truncate
    By LTLhawk in forum C++ Programming
    Replies: 2
    Last Post: 08-30-2003, 06:56 PM
  4. truncate
    By hoodiemama in forum C++ Programming
    Replies: 4
    Last Post: 11-02-2002, 06:37 PM
  5. How to truncate URL string to filename?
    By theLastSlacker in forum C++ Programming
    Replies: 6
    Last Post: 09-11-2002, 12:00 AM