473,322 Members | 1,540 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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

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

Jul 21 '06 #1
6 1963
* 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?
Jul 21 '06 #2
* 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?
Jul 21 '06 #3
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*.

Jul 21 '06 #4

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?
Jul 22 '06 #5
* 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?
Jul 22 '06 #6
In message <j8********************@newssvr21.news.prodigy.com >, red
floyd <no*****@here.dudewrites
>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
Jul 24 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Pikkel | last post by:
i'm looking for a way to replace special characters with characters without accents, cedilles, etc.
1
by: quickcur | last post by:
I am rewrite some of my excel sheets to xml document. In my excel, there is a lot of special characters like "/", " " (space), "#", "+'. I am using a java program based on JDom to to create xml....
13
by: Peter Monsson | last post by:
Hi all, I'm sitting with a problem where I have some special characters which have been "stringified" meaning that instead of having f. x. a '\n' or '\x20' I have "\n" and "\x20" and I have to...
5
by: M Kinnear | last post by:
I want to check a string only contains a-z 0-9 ( ) . and # I've used ereg("^*)$"),$instr) which parses a string correctly until I try and parse a !,",£,$ ... or any of the other...
3
KevinADC
by: KevinADC | last post by:
Purpose The purpose of this article is to discuss the difference between characters inside a character class and outside a character class and some special characters inside a character class....
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.