#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    107
    Rep Power
    11

    mini strings padded


    Hi,

    I would like to reformat a string. The string contains standard characters, and is replicating data from a record, delimited by semi-colons, an example of original string:

    X25;1116;1293567729;BT;P; ; ;0;19.2;;05/03/1998;;;GALILEO UK;QQE;LGW1; ;13.58;21.7;0;

    I have a structure of what I would like the new string to be, i.e,

    field1 C 20
    field2 C 15
    field3 N 80
    field4 C 20
    etc....

    FIelds with chars to be right-padded, field nums to be left-padded and fields with dates to be left unchanged.

    The original record might be 387, or 409 or whatever and the new record will always be 990.

    I thought the function could accept the string and return the string. It would loop though the string finding the semi_colons and padding that particular mini_string with either left or right like so:

    Code:
    char * foo(char * line)
    {
       /* get sizeof of line
        * start loop up to end of line
        * at each semi-colon take mini-sting and sent to lpad or rpad
        * put modified string into new line
        * at end send back new line
        */
    
    char * result;
    char * mini;
    int letter, count, loop:
    
       count = sizeof(line);
       for(loop = 0;, loop < count; loop++)
       {
          letter = strchr(line, ";")
          mini[loop] = letter[loop];
          result += rpad(mini);
       }
    
       return result;
    }
    Unfortunately, I have too many problems to know where to start.

    I dont have an lpad or rpad function,
    I can only call one of them in the above loop, whereas there are three possible outcomes
    1) lpad
    2) rpad
    3) leave alone

    and how to clear mini between each field.

    If any one has done this before, I would be grateful of some ideas,

    many thanks,

    Paul
  2. #2
  3. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244
    You need to allocate memory for your new 'string' (keep in mind that it is only a character array!). Alternatively, you can use the C++ 'string' class, much slower (CPU wise, maybe not human wise), but much less book keeping.

    Obviously you are from another language! C and to a lesser extent C++ doesn't hold your hand like a lot of other languages, you have to do the dirty work yourself. I am sure there are libraries out there that could help you, but then you have to find, compile and link to use.

    If you want to put your data into a structure (it sort of looks like that to me), you will have to either define a class (C++) or a structure (either), then copy the bits into the structure. You can have numeric as well as string (and even date) data in a structure. If you create a class, you can have a 'print' method to dump the data back out.

    This is non-trivial development in C, perhaps you should consider why you want to use C instead of another language.

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

IMN logo majestic logo threadwatch logo seochat tools logo