char arrays, "cutting", resizing etc.
I'm doing this (quite simple) operation:
- Read a line from a semi-colon-separate file:
00:name:22:name2:blablablablablahblahblah
This string has a length of 104, allowing for the these substring lengths (plus the delimiters):
2
16
2
16
64
However, the size of the strings can be shorter, so the entire string will most often be less than the max length of 104.
My problem is that I then need to split the elements in this string (based on the delimiters) into new strings (and a couple of ints), which I've defined with the above sizes.
What I then want to do (which is the actual problem) is:
1: Cut down the size of the arrays. F.ex. the array which includes the "Name" part of the string has a size of 16 characters. But if the actual contents is only 5 characters, I'd like to get rid of the rest. I know I cannot just resize an array, but is there an "elegant" way of solving this?
2: More importantly: The string created by fgets(), and hence the last of the new arrays including the last substring (blablablablablahblahblah in this example) includes a line-break character, placed there by fgets(). This i really need to remove. However, I cannot just remove the last two characters of the string, since it might just as well be only 40 characters in the 64-characer long array, and hence a bunch of empty spaces between the last character and the line-break. Is there a good way to loop through a char array looking for a line break, and then removing it?