Click here to Skip to main content
15,889,281 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: char data type problem Pin
pjdriverdude27-Sep-10 9:04
pjdriverdude27-Sep-10 9:04 
QuestionRe: char data type problem Pin
David Crow27-Sep-10 9:05
David Crow27-Sep-10 9:05 
AnswerRe: char data type problem Pin
pjdriverdude27-Sep-10 9:33
pjdriverdude27-Sep-10 9:33 
GeneralRe: char data type problem Pin
David Crow27-Sep-10 9:35
David Crow27-Sep-10 9:35 
AnswerRe: char data type problem Pin
Aescleal27-Sep-10 8:33
Aescleal27-Sep-10 8:33 
GeneralRe: char data type problem Pin
pjdriverdude27-Sep-10 8:57
pjdriverdude27-Sep-10 8:57 
GeneralRe: char data type problem Pin
Luc Pattyn27-Sep-10 10:19
sitebuilderLuc Pattyn27-Sep-10 10:19 
AnswerRe: char data type problem Pin
Stefan_Lang29-Sep-10 6:51
Stefan_Lang29-Sep-10 6:51 
I see a number of conceptual mistakes here, and some of them might be part of the problem you perceive:

1. You are using statically defined buffers for storing a string of dynamic length. You cannot do that, since the behaviour of your function(s) will be undefined if for whatever reason the total string length exceeds the length of your buffer. Just using an arbitrary 'large' size is exactly the kind of thinking that caused the year 2000 bug histeria. The better solution is to calculate the size you need and dynamically allocate a buffer large enough for your purposes.

2. The third parameter of strncat is the maximum number of characters being appended. If you want to make sure that you don't write past the end of your buffer, you first need to calculate the length of the string currently stored within that buffer, using e. g. the strlen() function, and then calculate the difference.

However, that point is moot if you do what I stated in 1. since you will already know for sure that your buffer is sufficiently large - you made it so!

3. Beyond what I stated in 2., while you tried to avoid writing past the end of the buffer, you didn't care to check whether that actually causes a truncation of the concatenated string. Depending on what you want to do with that string, truncating it might render it unusable, and your function's behaviour becomes unpredictable.

4. Using strncat() or strncpy() instead of strcat and strcopy will truncate the terminating 0-character if the concatenated string is too long. As a result, your buffer will contain a string of undefined length, a length that will be equal or greater than your buffer size. Any followup calls to strncat() or strcat() will inevitably write past your reserved buffer. Not to mention that your buffer, when interpreted as a 0-terminated string, will be invalid for your purposes.


Summary:
(a) Do not use strncat() or strncpy! At all! The strings you are working with must not be truncated. A truncated directory name will cause errors as will truncated file names. There is no point at all in using functions that might truncate your strings without producing any indication that they did!

(b) Always use precise length for your string literals and string buffers! For string literals, just don't supply the length, define them like this instead:
char template_file_path[] = "c:\\Dragon\\Speaker\\" ;

And for dynamically sized buffers, calculate the exact length that you need:
char *temporary_file_path = new char[strlen(template_file_path)+strlen(voice_template_g)+1];

(and don't forget the delete [] temporary_file_path; later)

(or use malloc and free if you must)

(c) Use strcpy and strcat to make sure your path and filenames are not truncated, and your final string will be 0-terminated.


If you have a C++ compiler, it would of course be much easier to just use std:string instead of char* ...
GeneralRe: char data type problem Pin
pjdriverdude1-Oct-10 5:31
pjdriverdude1-Oct-10 5:31 
GeneralRe: char data type problem Pin
Stefan_Lang3-Oct-10 22:50
Stefan_Lang3-Oct-10 22:50 
Questionis mfc coding diff [Moved] Pin
prithaa27-Sep-10 3:35
prithaa27-Sep-10 3:35 
AnswerRe: is mfc coding diff Pin
bob1697227-Sep-10 4:16
bob1697227-Sep-10 4:16 
GeneralRe: is mfc coding diff Pin
prithaa27-Sep-10 6:27
prithaa27-Sep-10 6:27 
QuestionCustomizing the Help button in CPrintDialog Pin
rayneesh27-Sep-10 3:04
rayneesh27-Sep-10 3:04 
AnswerRe: Customizing the Help button in CPrintDialog Pin
Cool_Dev27-Sep-10 3:57
Cool_Dev27-Sep-10 3:57 
QuestionHow to add LibTiff in Win32 project? Pin
002comp27-Sep-10 2:34
002comp27-Sep-10 2:34 
AnswerRe: How to add LibTiff in Win32 project? Pin
CPallini27-Sep-10 3:23
mveCPallini27-Sep-10 3:23 
QuestionVertical Scrollbar in list control Pin
learningvisualc27-Sep-10 1:30
learningvisualc27-Sep-10 1:30 
AnswerRe: Vertical Scrollbar in list control Pin
Niklas L27-Sep-10 2:01
Niklas L27-Sep-10 2:01 
AnswerRe: Vertical Scrollbar in list control Pin
Cool_Dev27-Sep-10 2:42
Cool_Dev27-Sep-10 2:42 
QuestionUnions, bitfields and masking unmasking Pin
federico.strati26-Sep-10 21:45
federico.strati26-Sep-10 21:45 
AnswerRe: Unions, bitfields and masking unmasking Pin
Niklas L26-Sep-10 21:57
Niklas L26-Sep-10 21:57 
AnswerRe: Unions, bitfields and masking unmasking Pin
Sauro Viti26-Sep-10 22:04
professionalSauro Viti26-Sep-10 22:04 
AnswerRe: Unions, bitfields and masking unmasking Pin
federico.strati26-Sep-10 23:57
federico.strati26-Sep-10 23:57 
QuestionTab Order Pin
Fedrer26-Sep-10 20:05
Fedrer26-Sep-10 20:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.