Click here to Skip to main content
15,867,939 members
Articles / Desktop Programming / MFC
Article

CString To char Conversion method (Warning Free)

Rate me:
Please Sign up or sign in to vote.
3.00/5 (8 votes)
12 Oct 2005 86.4K   9   13
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


Written By
Engineer Deckel Sportslids (PTY) Ltd.
South Africa South Africa
Engineer, Completed a PhD. Mechanical Engineering focusing on Machine Vision Control. Developed algorithms to allow an automated machine to automatically identify and qualify the product loaded. If the product has been correctly loaded then the machine will execute the correct program for that particular component.

Interested in hardware, PCI, USB, Serial and Parallel along with the software to accompany.

Comments and Discussions

 
Questionmaybe something else? Pin
veelck28-Mar-07 9:40
veelck28-Mar-07 9:40 
AnswerRe: maybe something else? Pin
marcdominic24-Nov-08 1:42
professionalmarcdominic24-Nov-08 1:42 
QuestionThink there was a "built-in" way? Pin
Philip Patrick12-Oct-05 11:03
professionalPhilip Patrick12-Oct-05 11:03 
AnswerRe: Think there was a "built-in" way? Pin
ImmelmanQ12-Oct-05 11:17
professionalImmelmanQ12-Oct-05 11:17 
GeneralRe: Think there was a "built-in" way? Pin
Philip Patrick12-Oct-05 11:37
professionalPhilip Patrick12-Oct-05 11:37 
AnswerRe: Think there was a "built-in" way? Pin
PJ Arends12-Oct-05 11:18
professionalPJ Arends12-Oct-05 11:18 
GeneralRe: Think there was a "built-in" way? Pin
Philip Patrick12-Oct-05 11:30
professionalPhilip Patrick12-Oct-05 11:30 
GeneralRe: Think there was a "built-in" way? Pin
ImmelmanQ12-Oct-05 11:52
professionalImmelmanQ12-Oct-05 11:52 
GeneralRe: Think there was a "built-in" way? Pin
Philip Patrick12-Oct-05 12:11
professionalPhilip Patrick12-Oct-05 12:11 
Yep and for me too, had to go and read something new, never was aware of that new set of CRT functions Big Grin | :-D

Philip Patrick
Web-site: www.stpworks.com
"Two beer or not two beer?" Shakesbeer
GeneralRe: Think there was a "built-in" way? Pin
PJ Arends12-Oct-05 12:02
professionalPJ Arends12-Oct-05 12:02 
GeneralRe: Think there was a "built-in" way? Pin
Philip Patrick12-Oct-05 12:10
professionalPhilip Patrick12-Oct-05 12:10 
GeneralRe: Think there was a "built-in" way? Pin
Johann Gerell12-Oct-05 20:52
Johann Gerell12-Oct-05 20:52 
GeneralRe: Think there was a "built-in" way? Pin
PJ Arends13-Oct-05 16:16
professionalPJ Arends13-Oct-05 16:16 

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.