Click here to Skip to main content
Licence 
First Posted 12 Oct 2005
Views 66,514
Bookmarked 9 times

CString To char Conversion method (Warning Free)

By | 12 Oct 2005 | Article
An article on converting CStrings to chars without data loss.

Introduction

When I started coding in Visual Studio, I had a difficult time finding clear information on how to convert CStrings to chars. Many of my applications create multiple files for configuration storage based on the information provided by the user. I prefer to use the old C style file handling as apposed to the MFC CFile but to do this you have to convert any CString to a char for the file name. At first I wrote a function to perform the task else Visual Studio gives the infamous "Conversion to char possible loss of data" warning. I prefer my code to be warning free. A small quirk I know. Any way here is a HowTo. Granted this may seem trivial but it frustrated the heck out of me trying to gain an answer.

Using the code

Using the code is simple. Either cut and paste the following code into your application and change any variables to suit or just apply the method as needed.

//
// Convert CString to Char By Quintin Immelman.
//
CString DummyString;
// Size Can be anything, just adjust the 100 to suit. 
const size_t StringSize = 100;
// The number of characters in the string can be
// less than String Size. Null terminating character added at end.
size_t CharactersConverted = 0;
 
char DummyToChar[StringSize];
 
wcstombs_s(&CharactersConverted, DummyToChar, 
           DummyString.GetLength()+1, DummyString, 
           _TRUNCATE);
//Always Enter the length as 1 greater else 
//the last character is Truncated

Points of Interest

Easy as that. Good clean "warning" free code at last.

History

  • Last updated: 07 October 2005

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

ImmelmanQ

Engineer
AK Stoneguards
South Africa South Africa

Member

Industrial Engineer, Currently busy with my MSc Engineering.
 
Interested in hardware, PCI, USB, Serial and Parallel along with the software to accompany.
 
Working aswell as studying at AK Stoneguards doing all their plant automation. (Automated quality logging systems, robotics ect...)


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Questionmaybe something else? Pinmemberveelck9:40 28 Mar '07  
AnswerRe: maybe something else? Pinmembermarcdominic1:42 24 Nov '08  
QuestionThink there was a "built-in" way? PinmemberPhilip Patrick11:03 12 Oct '05  
AnswerRe: Think there was a "built-in" way? PinmemberImmelmanQ11:17 12 Oct '05  
GeneralRe: Think there was a "built-in" way? PinmemberPhilip Patrick11:37 12 Oct '05  
AnswerRe: Think there was a "built-in" way? PinmemberPJ Arends11:18 12 Oct '05  
GeneralRe: Think there was a "built-in" way? PinmemberPhilip Patrick11:30 12 Oct '05  
GeneralRe: Think there was a "built-in" way? PinmemberImmelmanQ11:52 12 Oct '05  
GeneralRe: Think there was a "built-in" way? PinmemberPhilip Patrick12:11 12 Oct '05  
GeneralRe: Think there was a "built-in" way? PinmemberPJ Arends12:02 12 Oct '05  
GeneralRe: Think there was a "built-in" way? PinmemberPhilip Patrick12:10 12 Oct '05  
GeneralRe: Think there was a "built-in" way? PinmemberJohann Gerell20:52 12 Oct '05  
GeneralRe: Think there was a "built-in" way? PinmemberPJ Arends16:16 13 Oct '05  

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

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120517.1 | Last Updated 12 Oct 2005
Article Copyright 2005 by ImmelmanQ
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid