No special meaning to '\0': just like any other character

Discussion in 'C++' started by nagrik, Jul 21, 2006.

  1. nagrik

    nagrik Guest

    Hello group,

    I am reading an 'mpeg file' from the socket. In read socket I specify
    a 'char* buffer' to read the file. However, the content of actual data
    contain '\0' characters at various places.

    When I read the full content I copy the char* buffer into a string type
    variable. The code looks
    like

    int len;
    char * buf[256];
    int size = 256;
    string content;

    len = read(sockFd, buf, size);

    content = buf;

    Here if I print buf anything after the '\0' is not printed.

    When I copy the buf to content anything after '\0' wipes out and is not
    copied. I want full
    buffer to be copied to content and later on down the line I want to
    save it to a file including
    any '\0' characters.

    I also want to print the full buffer on the stdout including anything
    after '\0' character.

    Folks! any suggestion. This bug is stopping my program to proceed.

    Thx.

    arun
    nagrik, Jul 21, 2006
    #1
    1. Advertising

  2. * nagrik:
    > Hello group,
    >
    > I am reading an 'mpeg file' from the socket. In read socket I specify
    > a 'char* buffer' to read the file. However, the content of actual data
    > contain '\0' characters at various places.
    >
    > When I read the full content I copy the char* buffer into a string type
    > variable. The code looks
    > like
    >
    > int len;
    > char * buf[256];
    > int size = 256;
    > string content;
    >
    > len = read(sockFd, buf, size);
    >
    > content = buf;


    string content const( buf, buf + len );


    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Jul 21, 2006
    #2
    1. Advertising

  3. * Alf P. Steinbach:
    > * nagrik:
    >> Hello group,
    >>
    >> I am reading an 'mpeg file' from the socket. In read socket I specify
    >> a 'char* buffer' to read the file. However, the content of actual data
    >> contain '\0' characters at various places.
    >>
    >> When I read the full content I copy the char* buffer into a string type
    >> variable. The code looks
    >> like
    >>
    >> int len;
    >> char * buf[256];
    >> int size = 256;
    >> string content;
    >>
    >> len = read(sockFd, buf, size);
    >>
    >> content = buf;

    >
    > string content const( buf, buf + len );


    Sorry, transposition errors are usually at the letter level, but somehow
    here at the word level.

    Should be

    string const content( buf, buf + len );

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Jul 21, 2006
    #3
  4. nagrik

    red floyd Guest

    nagrik wrote:
    > Hello group,
    >
    > I am reading an 'mpeg file' from the socket. In read socket I specify
    > a 'char* buffer' to read the file. However, the content of actual data
    > contain '\0' characters at various places.
    >
    > When I read the full content I copy the char* buffer into a string type
    > variable. The code looks
    > like
    >
    > int len;
    > char * buf[256];
    > int size = 256;
    > string content;
    >
    > len = read(sockFd, buf, size);
    >
    > content = buf;
    >
    > Here if I print buf anything after the '\0' is not printed.
    >
    > When I copy the buf to content anything after '\0' wipes out and is not
    > copied. I want full
    > buffer to be copied to content and later on down the line I want to
    > save it to a file including
    > any '\0' characters.
    >
    > I also want to print the full buffer on the stdout including anything
    > after '\0' character.
    >
    > Folks! any suggestion. This bug is stopping my program to proceed.
    >


    You don't actually treat the data as a char* type string. The "char*"
    is legacy from BSD. Treat it like a void*.
    red floyd, Jul 22, 2006
    #4
  5. nagrik

    nagrik Guest

    Alf P. Steinbach wrote:
    > * Alf P. Steinbach:
    > > * nagrik:
    > >> Hello group,
    > >>
    > >> I am reading an 'mpeg file' from the socket. In read socket I specify
    > >> a 'char* buffer' to read the file. However, the content of actual data
    > >> contain '\0' characters at various places.
    > >>
    > >> When I read the full content I copy the char* buffer into a string type
    > >> variable. The code looks
    > >> like
    > >>
    > >> int len;
    > >> char * buf[256];
    > >> int size = 256;
    > >> string content;
    > >>
    > >> len = read(sockFd, buf, size);
    > >>
    > >> content = buf;

    > >
    > > string content const( buf, buf + len );

    >
    > Sorry, transposition errors are usually at the letter level, but somehow
    > here at the word level.
    >
    > Should be
    >
    > string const content( buf, buf + len );
    >


    Alf,

    My content variable is already constructed so after I have buf with me,
    the only operation
    I am allowed is assignment.

    something like

    content += buf.

    'buf' contains a '\0' character and there will be many more 'buf's I
    would like to append to
    content. Like

    while ( there are more bufs available)
    content += buf;

    With my implementation, the assignment only copy till the end of '\0'
    and later appends
    append only after '\0' character and not after the whole buffer, which
    I desire. Remember
    the '\0' character can occur anywhere in buf and in any buf. I want
    content to have full
    range of values, including '\0' s.

    Too bad C++ does not have a byte type. Any thoughts.

    nagrik

    > --
    > A: Because it messes up the order in which people normally read text.
    > Q: Why is it such a bad thing?
    > A: Top-posting.
    > Q: What is the most annoying thing on usenet and in e-mail?
    nagrik, Jul 22, 2006
    #5
  6. * nagrik:
    > Alf P. Steinbach wrote:
    >> * Alf P. Steinbach:
    >>> * nagrik:
    >>>>
    >>>> I am reading an 'mpeg file' from the socket. In read socket I specify
    >>>> a 'char* buffer' to read the file. However, the content of actual data
    >>>> contain '\0' characters at various places.
    >>>>
    >>>> When I read the full content I copy the char* buffer into a string type
    >>>> variable. The code looks
    >>>> like
    >>>>
    >>>> int len;
    >>>> char * buf[256];
    >>>> int size = 256;
    >>>> string content;
    >>>>
    >>>> len = read(sockFd, buf, size);
    >>>>
    >>>> content = buf;
    >>> string content const( buf, buf + len );

    >> Sorry, transposition errors are usually at the letter level, but somehow
    >> here at the word level.
    >>
    >> Should be
    >>
    >> string const content( buf, buf + len );

    >
    > My content variable is already constructed so after I have buf with me,
    > the only operation
    > I am allowed is assignment.


    Why don't you just move the declaration?

    Or invent a new name?

    Or if neither of these options feel right, use the named 'assign' member
    function?

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Jul 22, 2006
    #6
  7. In message <j8dwg.133411$>, red
    floyd <> writes
    >nagrik wrote:
    >> Hello group,
    >> I am reading an 'mpeg file' from the socket. In read socket I
    >>specify
    >> a 'char* buffer' to read the file. However, the content of actual data
    >> contain '\0' characters at various places.
    >> When I read the full content I copy the char* buffer into a string
    >>type
    >> variable. The code looks
    >> like
    >> int len;
    >> char * buf[256];
    >> int size = 256;
    >> string content;
    >> len = read(sockFd, buf, size);
    >> content = buf;
    >> Here if I print buf anything after the '\0' is not printed.
    >> When I copy the buf to content anything after '\0' wipes out and is
    >>not
    >> copied. I want full
    >> buffer to be copied to content and later on down the line I want to
    >> save it to a file including
    >> any '\0' characters.
    >> I also want to print the full buffer on the stdout including
    >>anything
    >> after '\0' character.
    >> Folks! any suggestion. This bug is stopping my program to proceed.
    >>

    >
    >You don't actually treat the data as a char* type string. The "char*"
    >is legacy from BSD. Treat it like a void*.
    >

    He's copying it into a std::string. That takes a char*.

    The real problem is that std::string has an assignment operator which
    takes a const char * argument, but it stops copying at the first '\0'.
    It also has a constructor which takes the same argument and has the same
    problem.

    The solution is that it also has constructors which take _two_
    arguments, either two input iterators or a pointer and a length. Either
    of these will copy the appropriate sequence without stopping at '\0':

    string content(buf, size); // pointer and count
    or
    string content(buf, buf+size); // two input iterators

    --
    Richard Herring
    Richard Herring, Jul 24, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Martin Maney
    Replies:
    1
    Views:
    287
    Gabriel Genellina
    Apr 11, 2007
  2. Replies:
    4
    Views:
    728
  3. Dennis.Zeng
    Replies:
    2
    Views:
    635
    Jerry Coffin
    Sep 14, 2009
  4. mosar
    Replies:
    8
    Views:
    157
    Glen Holcomb
    Jul 22, 2008
  5. Sebastian
    Replies:
    17
    Views:
    331
    Gene Wirchenko
    Feb 4, 2013
Loading...

Share This Page