September 26th, 2003, 06:59 AM
mini strings padded
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
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:
Unfortunately, I have too many problems to know where to start.
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);
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
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,
September 26th, 2003, 08:28 AM
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.