Thread: char arrays, "cutting", resizing etc.

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    111

    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?

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    How about this

    Code:
    if( sscanf( buffer, "%[^:]:%[^:]:%[^:]:%[^:]:%s", part1, part2, part3, part4, part5 ) == 5 )
    {  ... }
    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  4. #4
    Registered User
    Join Date
    Nov 2009
    Posts
    111
    Fordi: Thanks, but C != C++

    ssharinsh: Hmm....exactly what does that do?

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    strtok() is part of the C++ standard library because it was inherited from the C standard library.

    Incidentally, C != C++ will always be false in C and C++ (for all basic types for which the expression compiles).
    Last edited by grumpy; 06-20-2011 at 05:33 AM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    Registered User
    Join Date
    Nov 2009
    Posts
    111
    Yah, of course you're right. And I've looked at strtok. The problems remain, however, as strtok just gets the substrings. I can do that manually as well. But:

    1. How to resize a char array of size 16, which only has 5 characters, to size 5.
    2. How to remove a newline character from a char array when you don't know at which index it is?

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by cnewbie1 View Post
    Yah, of course you're right. And I've looked at strtok. The problems remain, however, as strtok just gets the substrings. I can do that manually as well. But:

    1. How to resize a char array of size 16, which only has 5 characters, to size 5.
    2. How to remove a newline character from a char array when you don't know at which index it is?
    You're missing the point... strtok() splits a string into its sub-parts by inserting nulls at the tokens and returning the start address of the first non-token character after that. In essence it has cut the string down and no part is longer than it needs to be. If you want to copy the strings out of your original buffer --to put them in structs or arrays for example-- you can use malloc() and strcpy() or strdup().

  8. #8
    Registered User
    Join Date
    Nov 2009
    Posts
    111
    I'm running this string through strtok():

    00:name:2:morename:2204a3741a4f4c636acb71cca5da1b0 041446e554d9fb1a2dceec29428491a17

    Code:
    - calling function - reads the above line from a text file and sends it to the split function
    csflsplit_b(line, sizeof(line)/sizeof(char), ":");
    
    void split_b (char line[], int len, char delims[]) {
    	char *result = NULL;						
    	result = strtok(line, delims);
    
    	while (result != NULL) {
    		printf("%s\n", result);
    		result = strtok(NULL, delims);
            }
    }
    Now, this prints out the following:
    Code:
    bob@host ~/stuff $ ./test 
    00
    Neuni
    2
    neuni
    2204a3741a4f4c636acb71cca5da1b0041446e554d9fb1a2dceec29428491a17
    
    bob@host ~/Working-copies/cprojects/nettconf-cli $
    This shows that the last part - 2204a3741a4f4c636acb71cca5da1b0041446e554d9fb1a2dc eec29428491a17 - must have a line-break which strtok includes, doesn't it?

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You can have more than one delimiter in strtok... in your case you could use ":\n\r\t" to remove tabs and line endings.

  10. #10
    Registered User
    Join Date
    Nov 2009
    Posts
    111
    Ah, of course. Should've read the specification more closely. Frightningly simple. Thanks.

  11. #11
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Quote Originally Posted by cnewbie1
    This shows that the last part - 2204a3741a4f4c636acb71cca5da1b0041446e554d9fb1a2dc eec29428491a17 - must have a line-break which strtok includes, doesn't it?
    small update to reflect the OP's statment

    Code:
    if( sscanf( buffer, "%[^:]:%[^:]:%[^:]:%[^:]:%[^\n]", part1, part2, part3, part4, part5 ) == 5 )
    { ... }
    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by ssharish2005 View Post
    small update to reflect the OP's statment

    Code:
    if( sscanf( buffer, "%[^:]:%[^:]:%[^:]:%[^:]:%[^\n]", part1, part2, part3, part4, part5 ) == 5 )
    { ... }
    ssharish
    Or he could have used ... sscanf(str,"%s:%s:%s:%s:%s"...
    It will do pattern matching with that as well.

  13. #13
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Building on ssharish2005's code:
    Code:
    if( fscanf( file, "%2[^:]:%16[^:]:%2[^:]:%16[^:]:%64[^\n]", part1, part2, part3, part4, part5 ) == 5 )
    { ... }
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  14. #14
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Quote Originally Posted by CommonTater
    Or he could have used ... sscanf(str,"%s:%s:%s:%s:%s"...
    It will do pattern matching with that as well.
    Why do you think this code will work? I say it wont and shall leave it to you to workout why?

    ssharish
    Last edited by ssharish2005; 06-20-2011 at 04:03 PM.
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by ssharish2005 View Post
    Why do you think this could will work? I say it wont and shall leave it to you to workout why?
    ssharish
    Don't be changing the colons to semi-colons like that...
    Editing quoted text is some serious bad juju.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. "Switch", "int" and "char"!
    By luckyluke in forum C Programming
    Replies: 6
    Last Post: 07-29-2010, 10:42 AM
  2. scanf "%as" (dynamic char arrays)
    By brightmatter in forum C Programming
    Replies: 10
    Last Post: 03-06-2010, 12:05 AM
  3. Char Help! "Packing " bits to a signle unsigned char
    By xxrexdartxx in forum C Programming
    Replies: 7
    Last Post: 10-11-2009, 04:45 AM
  4. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM