Thread: Dealing with strings containing embedded null characters

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    2

    Dealing with strings containing embedded null characters

    hi,

    how can I get the length of a string that has embedded '\0' ?

    Code:
    char * foo = "abcde\0fgh\0ijkl"; 
    printf ("%s\n", foo);            /* this only prints out: abcde */ 
    printf("%d\n", strlen(foo));  /* this displays: 5 instead of 12 (or is it 14 ?) */
    Thanks
    --Andrew

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You'd have to define your own terminator, say a double null, and write a set of functions to deal with such a thing.

    Code:
    char * foo = "abcde\0fgh\0ijkl\0";
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    There is no such a thing as a string that has an embedded \0. By definition, a string stops at a \0 character.

    What are you actually trying to accomplish?

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    2
    What i'm trying to do is read and display the contents of a file, specifically "/proc/<pid>/cmdline"
    file (file size happens to be 0-bytes) on Linux. The contents of "cmdline" file contain embedded '\0' characters
    and represent the command line arguments for a given process. The '\0' character is the delimiter of each argument.

    Thanks
    --Andrew
    Last edited by mr_c; 10-29-2008 at 01:01 AM.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by mr_c View Post
    What i'm trying to do is read and display the contents of a file, specifically "/proc/<pid>/cmdline"
    file (file size happens to be 0-bytes) on Linux. The contents of "cmdline" file contain embedded '\0' characters
    and represent the command line arguments for a given process. The '\0' character is the delimiter of each argument.

    Thanks
    --Andrew
    I expect that follows the same concept as environment variables: It has a double zero at the end. Use "od -x /proc/<pid>/cmdline" to display it in hex, and I believe you will see the end is two zero bytes.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Tweakable Radar...
    By DoraTehExploda in forum Game Programming
    Replies: 8
    Last Post: 06-07-2005, 10:49 AM
  2. . . . . . . - . . . - -
    By The Brain in forum C++ Programming
    Replies: 17
    Last Post: 05-17-2005, 04:01 AM
  3. Help with yacc/compiler design/seg fault
    By trippeer in forum C Programming
    Replies: 1
    Last Post: 04-08-2005, 03:43 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. BST/Red and Black Tree
    By ghettoman in forum C++ Programming
    Replies: 0
    Last Post: 10-24-2001, 10:45 PM