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

N

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;

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
 
A

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 );
 
A

Alf P. Steinbach

* 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 );
 
R

red floyd

nagrik said:
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*.
 
N

nagrik

Alf said:
* 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

Alf P. Steinbach

* nagrik:
Alf said:
* 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?
 
R

Richard Herring

red said:
nagrik said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top