473,386 Members | 1,791 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,386 software developers and data experts.

std::string and \0

Hello,

I guess this might be a very dummy question, but I couldn't find an
answer in the group's archive.

I am reading DICOM files, and to store the string read I use
std::string. Unfortunately there is case where the string can be '\0\0'
Is this a defined bahavior when you do this kind of operation :

const char *s = "\0\0";
std::string a = s; //how many character will be copied

Is there a work around other than subclassing std::string to handle
properly \0 ? Knowing that I can carry the 'real' lenght around, my main
concern is how do I fill a std::string with x number of \0 ?

Thanks
Mathieu

Jul 22 '05 #1
3 3801
"Mathieu Malaterre" <mm**********@Mnycap.rr.com> wrote in message
news:%F*********************@twister.nyroc.rr.com. ..
Hello,

I guess this might be a very dummy question, but I couldn't find an answer
in the group's archive.

I am reading DICOM files, and to store the string read I use std::string.
Unfortunately there is case where the string can be '\0\0'
Is this a defined bahavior when you do this kind of operation :

const char *s = "\0\0"; NB: This will actually point to 3 times '\0'. std::string a = s; //how many character will be copied None.
Is there a work around other than subclassing std::string to handle
properly \0 ? Knowing that I can carry the 'real' lenght around, my main
concern is how do I fill a std::string with x number of \0 ?


Use the std::string constructor that takes an iterator (or pointer)
range:
const char *s = "\0\0";
std::string a(s,s+2); // will copy 2 '\0'

Note that a.size() will return 2, but if you call a.c_str() instead
of a.data(), you will be guaranteed to have a third '\0' included
at the end of the returned buffer, to match C string conventions.
hth,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com
Jul 22 '05 #2
Ivan Vecerina wrote:
"Mathieu Malaterre" <mm**********@Mnycap.rr.com> wrote in message
news:%F*********************@twister.nyroc.rr.com. ..
Hello,

I guess this might be a very dummy question, but I couldn't find an answer
in the group's archive.

I am reading DICOM files, and to store the string read I use std::string.
Unfortunately there is case where the string can be '\0\0'
Is this a defined bahavior when you do this kind of operation :

const char *s = "\0\0";


NB: This will actually point to 3 times '\0'.
std::string a = s; //how many character will be copied


None.

Is there a work around other than subclassing std::string to handle
properly \0 ? Knowing that I can carry the 'real' lenght around, my main
concern is how do I fill a std::string with x number of \0 ?

Use the std::string constructor that takes an iterator (or pointer)
range:
const char *s = "\0\0";
std::string a(s,s+2); // will copy 2 '\0'

Note that a.size() will return 2, but if you call a.c_str() instead
of a.data(), you will be guaranteed to have a third '\0' included
at the end of the returned buffer, to match C string conventions.


You rocks, I was -sadly- thinking I should use a vector<char>
But your advice look a lot more easy to implement/patch in my current
project.

Thanks
Mathieu

Jul 22 '05 #3
"Mathieu Malaterre" <mm**********@Mnycap.rr.com> wrote in message
news:%F*********************@twister.nyroc.rr.com. ..
Hello,

I guess this might be a very dummy question, but I couldn't find an
answer in the group's archive.

I am reading DICOM files, and to store the string read I use
std::string. Unfortunately there is case where the string can be '\0\0'
Is this a defined bahavior when you do this kind of operation :
Yes.

const char *s = "\0\0";
std::string a = s; //how many character will be copied
Zero. I.e. 'a.size()' will return zero, and 'a.empty()'
will return 'true'. Any characters following the first
zero-value character at or after the address contained by
's' are not part of the 'C string' to which it points.

Is there a work around

What do you want to 'work around'?
other than subclassing std::string to handle
properly \0 ?
'std::string' already handles it properly.
Knowing that I can carry the 'real' lenght around,
Then length of a 'C-style' string is the sum of the number
of characters from its beginning up to the one immediately
preceding the first with a value of zero. This length
does not include the zero-valued character.

char s1[] = "\0";
char s2[] = "\0\0";
char s3[] = "\0\0\0";
char s4[] = "\0abc";

Are all strings with length of zero. 'strlen()'
will report the same.
my main
concern is how do I fill a std::string with x number of \0 ?


Use a different constructor:

std::string zeros(x, 0);

-Mike
Jul 22 '05 #4

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

Similar topics

10
by: Angus Leeming | last post by:
Hello, Could someone explain to me why the Standard conveners chose to typedef std::string rather than derive it from std::basic_string<char, ...>? The result of course is that it is...
11
by: Christopher Benson-Manica | last post by:
Let's say I have a std::string, and I want to replace all the ',' characters with " or ", i.e. "A,B,C" -> "A or B or C". Is the following the best way to do it? int idx; while(...
22
by: Jason Heyes | last post by:
Does this function need to call eof after the while-loop to be correct? bool read_file(std::string name, std::string &s) { std::ifstream in(name.c_str()); if (!in.is_open()) return false; ...
19
by: Erik Wikström | last post by:
First of all, forgive me if this is the wrong place to ask this question, if it's a stupid question (it's my second week with C++), or if this is answered some place else (I've searched but not...
8
by: Patrick Kowalzick | last post by:
Dear NG, I would like to change the allocator of e.g. all std::strings, without changing my code. Is there a portable solution to achieve this? The only nice solution I can think of, would be...
6
by: Nemok | last post by:
Hi, I am new to STD so I have some questions about std::string because I want use it in one of my projects instead of CString. 1. Is memory set dinamicaly (like CString), can I define for...
2
by: FBergemann | last post by:
if i compile following sample: #include <iostream> #include <string> int main(int argc, char **argv) { std::string test = "hallo9811111z"; std::string::size_type ret;
84
by: Peter Olcott | last post by:
Is there anyway of doing this besides making my own string from scratch? union AnyType { std::string String; double Number; };
11
by: Jacek Dziedzic | last post by:
Hi! I need a routine like: std::string nth_word(const std::string &s, unsigned int n) { // return n-th word from the string, n is 0-based // if 's' contains too few words, return "" //...
11
by: Christopher Pisz | last post by:
Is std::string::npos always going to be less than any std::string 's size()? I am trying to handle a replacement of all occurances of a substr, in which the replacement also contains the substr....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.