Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > No special meaning to '\0': just like any other character

Reply
Thread Tools

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

 
 
nagrik
Guest
Posts: n/a
 
      07-21-2006
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

 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      07-21-2006
* 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?
 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      07-21-2006
* 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?
 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      07-21-2006
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*.

 
Reply With Quote
 
nagrik
Guest
Posts: n/a
 
      07-22-2006

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?


 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      07-22-2006
* 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?
 
Reply With Quote
 
Richard Herring
Guest
Posts: n/a
 
      07-24-2006
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Regex: Any character in character class Sebastian Java 17 02-04-2013 10:26 PM
Understanding search queries, semantics, and "Meaning" ...aren't weall looking for meaning? 5lvqbwl02@sneakemail.com Python 4 01-14-2009 02:28 PM
Use of special character like @ in irb mosar Ruby 8 07-22-2008 04:01 PM
501 PIX "deny any any" "allow any any" Any Anybody? Networking Student Cisco 4 11-16-2006 10:40 PM
Is the Stand By Me Deluxe Editon still mono audio just like the Special Edition? unclejr DVD Video 2 03-25-2005 01:34 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57