Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
CString is not accepting the NULL values in between? I am reading the image file, while reading am storing it in char and while copying the value to CString, It accepts the characters till the first NULL characters, rest values are ignored. Help me........
Posted
Comments
CPallini 19-May-11 7:54am    
Why do you need that (i.e. there might be better ways to achieve your final target)?

In a C-style string, the NULL character means "end of string". The CString object will display all characters untill the NULL character is found. But it doesn't mean the next characters are not stored or lost.

For example:
C++
CString foo;
foo += 'a';
foo += 'b';
foo += '\0';
foo += 'c';
foo += 'd';
//will contain 'c'
char c = foo[3];
//will contain 'd'
char d = foo[4];
//l equals 5.
int l = foo.GetLength();

In the above code, the debugger will display "ab" for the foo variable, however, c will contain 'c' and d will contain 'd' as expected.

But if you want to store a block of memory somewhere, I suggest not to use CString class. Use a byte or char array instead (CByteArray for example, or regular arrays).
 
Share this answer
 
v3
Comments
Nithin Sundar 16-May-11 6:35am    
This certainly doesn't deserve a downvote! Not when the solution poster has taken valuable time to explain a concept which is well within the scope of this question.
Olivier Levrey 16-May-11 6:39am    
The down-voter probably voted on the first revision of my answer while I was changing it:
my first answer was not so detailed and could be misunderstood.
Thank you Nithin.
Stefan_Lang 16-May-11 10:14am    
Upvoted for balance ;) This is definitely a good answer.

My advice would have been to use a different type altogether, such as CByteArray - but you already suggested that as well. Good work!
Olivier Levrey 16-May-11 10:26am    
Thank you very much Stefan.
Albert Holguin 16-May-11 10:36am    
right +5
I don't think this is true. All non-idiotic string type perfectly accept null characters.

(A null-terminated string is arguably idiotic; this is one of the most idiotic inventions of human kind of all times and peoples. At the same time, this type is one of the most used. Not good for us. All non-idiotic string types are length-prefixed in persistent presentation or just contain a length descriptor.)

Now, the problem is not the type but some function you call which may rely on null character. There are millions of such functions, I don't know which one caused the problem. Read the help pages on each one you use or see it under the debugger.

—SA
 
Share this answer
 
Comments
CPallini 19-May-11 7:49am    
Well, that is your opinion. The terminating NULL char has its drawbacks, I agree. But the lenght-prefix (aka 'Pascal' style) has (and above all had) its drawbacks too. For instance, what should be the size of the prefix? Would such string be portable to microcontrollers?
Sergey Alexandrovich Kryukov 20-May-11 15:17pm    
Is this is a problem, but not such idiotic as null terminated.
This is exactly the same kind of problem as the size of integer type on different systems. Could be separate types on different systems, big deal. Null character is
Your question about micro-controllers is irrelevant. Is the null-terminated idea was never used, it was not used anywhere, not in micro-controllers, nowhere.

No, this is not a matter of my taste or something like that. I'm talking objective criteria. If you think that real-life technological culture is rational, think again. It's full of misconceptions and even idiotic decisions as this one (this is not the only one).

Now, if we discussed just my answer about CString, it is also irrelevant. This type accept null string in the middle. Want to test it? My answer is correct anyway.

Thank you.
--SA
CPallini 20-May-11 16:20pm    
Nope. Microcontroller question is relevant. There isn't a portable (meaningful) way to define length prefixed strings for both a PC and, say, a 8 bit MCU. That said, what are the HUGE advantages of lenght-prefixed strings to mark the other choice 'idiotic'? I see only some conveniences, nothing more (please remember OOP isn't the way, is just a way of programming).
CPallini 20-May-11 16:21pm    
Nope. NULL-terminating is a portable way to handles strings. And microcontroller question is relevant. How could you port the PC length-prefixed string to an 8 bit MCU, in a meaningful way?
And what are the HUGE advantages of length-prefixed strings? I see some conveniences, nothing more.
Sergey Alexandrovich Kryukov 21-May-11 0:03am    
In brief, you're too preoccupied with null-termination. It's only portable in the framework of currently established practice, not more. If you try to explain this is portable you will not give an argument. The benefit is not convenience. It's as fundamental as direction of time if you will. Direction of reading data. Null-termination is huge performance hit and also a risk. Further discussion will need considering algorithms. You can consider them yourself.
--SA
Check this link and convert accordingly.
->[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900