String manupulation

This is a discussion on String manupulation within the C Programming forums, part of the General Programming Boards category; Hi to all, I have that string "prog=0. 6415 5436" and I want to manipulate it until it will be ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    161

    Unhappy String manupulation

    Hi to all, I have that string "prog=0.64155436" and I want to manipulate it until it will be like "64.15%".

    I've written a small code that works:

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    
    char *f (char *);
    
    int main (void) {
            char str[] = "prog=0.64155436";
            printf ("%s \n", f (str));
            return (0);
            system("PAUSE");
    }
    
    char *f (char *s) {
            float f;
            char *p = (char *) malloc (sizeof (s));
            if (strstr (s, "prog="))
                            s = s + 5;
            f = atof (s);
            f = f * 100;
            sprintf (p, "%2.2f%%", f);
            return (p);
            
            
    }
    This works but it's not wat I was looking for, it was only a "try".

    The REAL program it is the seguent (it uses the same funcion I created before) :

    Code:
    #include <stdio.h>
    #include <stdio.h>
    #include <string.h>
    
    char *f (char *);
    
    int main(void) {
      char done[100];  
      char *stato;
      FILE *file;  
    
      file = fopen("state.sah", "r"); 
      /* open a text file for reading */
    
      if(file==NULL) {
        printf("Seti@Home ha finito di elaborare la WorkUnit.\n");
        return 1;
      }
      else {
            
        while(fgets(done, 100, file)!=NULL) { 
        
            if(strstr(done,"prog=0.")) { printf("%s",done);
            printf ("%s \n", f (done)); }
            
           }
    
        fclose(file);
        system ("PAUSE");
        return 0;
      }
    }
    
    char *f (char *s) {
            float f;
            char *p = (char *) malloc (sizeof (s));
            if (strstr (s, "prog="))
                            s = s + 5;
            f = atof (s);
            f = f * 100;
            sprintf (p, "%2.2f%%", f);
            return (p);  
            
    }
    It looks in a file for the string I want and trys to modify it BUT it doesnt work because the output is :

    prog=0.64155436
    229339200.00%
    and not

    prog=0.64155436
    64.15%
    Thanx to everyone

  2. #2
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    472
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h> /* required for atof */
    
    void f(char *s) { /* instead of returning now just modifies s */
            float f;        /* notice there is no memory allocation this time */
            char *p;
            if (strstr (s, "prog=")) { /* your old program continued */
                p = s + 5;                  /* processing even when this */
                f = atof(p);                /* condition was false. */
                f = f * 100.0;
                sprintf(p, "%2.2f%%", f);
            }
    }
    
    int main()
    {
        char s[] = "prog=0.64155436";
        
        f(s); /*no return we have to call it on a seperate line */
        printf("%s\n",s);
        return 0;
    }
    try that one on for size. the main is just for testing.

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    161
    if (strstr (s, "prog="))
    seems to fail....if i compile it, it says only press a key to continue..

    anyway I will look at it better later thanx for your help

  4. #4
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    472
    Quote Originally Posted by BianConiglio
    seems to fail....if i compile it, it says only press a key to continue..

    anyway I will look at it better later thanx for your help
    ...okay...did you even read the code?

  5. #5
    /*enjoy*/
    Join Date
    Apr 2004
    Posts
    159
    f(done)
    should be
    & f(done)
    .....

  6. #6
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    I see you are trying to make a meter or something of how many seti units you have done, so I looked at the source code for a seti plugin to gkrellm, a program in linux. they did something like this
    Code:
    if(strcmp(string, "prog=") == 0) {
         sscanf(string, "prog=%lf", &somevar);
    }
    Of course it wasn't exactly like that being they used gtk type variables, strncmp, etc. If you want to look at the full code, you can download it at http://xavier.serpaggi.free.fr/seti/ but keep in mind most of the code deals with gtk.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by enjoy
    f(done)
    should be
    & f(done)
    .....
    No. Just stop. It should not.

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Mar 2004
    Posts
    161
    ok it works, i forgot to include #include <stdlib.h>


    (comments r in italian, sorry about that)

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    /* AGGIUNGENDO L'HEADER "STDLIB.H" FUNZIONA!!!
       Nel file stdlib.h viene dichiarato che la funzione "atof" restituisce un 
       double. Se non includiamo "stdlib.h" la funzione restituisce un double, ma 
       il main lo tratta come intero e quando lo stampo con la printf otteniamo un 
       valore errato */ 
    
    
    int main(void) {
      
      char *tmp;        /* l'ho aggiunto io. Punta alla stringa in questione */
      char done[100];  
      char *stato;
      FILE *file;  
      double valore_perc; /* qui mettiamo il valore convertito in double */
    
      file = fopen("state.sah", "r"); 
      /* open a text file for reading */
    
      if(file==NULL) {
        printf("Seti@Home ha finito di elaborare la WorkUnit.\n");
        printf("Ora verra' scaricata una nuova WorkUnit.\n");
        return 1;
      }
      
      else {
            
        while(fgets(done, 100, file)!=NULL) { 
        
          if(tmp=strstr(done,"prog=")) { 
                      /* adesso tmp punta alla riga cercata */ 
                      
                       tmp=tmp + 5;     /* avanziamo il puntatore di 5 caratteri */
                       valore_perc=(double)atof(tmp); /* convertiamo in double */
                       valore_perc = valore_perc * 100;
                       printf("Percentuale completata : %2.2f%%\n\n",valore_perc); /* stampiamo il double */
                       break; /* usciamo dal while */ 
                      }
            
           }
    
        fclose(file);
        system("PAUSE");
            return 0;
      }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  3. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 10:06 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21