| |

VerySource

 Forgot password?
 Register
Search
Author: lion502502

How to convert a CString string to a char for writing? ?

[Copy link]

0

Threads

1

Posts

23.00

Credits

Newbie

Rank: 1

Credits
23.00

 China

Post time: 2020-4-1 15:33:22
| Show all posts
A CString variable cstr
A char * variable ch
How to convert cstr to ch?

1. A const char * (LPCTSTR) pointer passed to unallocated memory.
CString cstr (asdd);
Const char * ch = (LPCTSTR) cstr;
The address pointed to by ch is the same as cstr. But because const is used to guarantee that ch will not be modified, it is safe.
2.A pointer passed to unallocated memory.
CString cstr = "ASDDSD";
Char * ch = cstr.GetBuffer (cstr.GetLength () + 1);
Cstr.ReleaseBuffer ();
// Modifying the value pointed to by ch is equal to modifying the value in cstr.
// PS: After using ch, do not delete ch, because this will destroy the internal space of cstr and easily cause the program to crash.
3. The second usage. Assign the CString value to the char * of the allocated memory.
CString cstr1 = "ASDDSD";
Int strLength = cstr1.GetLength () + 1;
Char * pValue = new char [strLength];
Strncpy (pValue, cstr1, strLength);
4. The third usage. Assign the CString value to the char [] array of allocated memory.
CString cstr2 = "ASDDSD";
Int strLength2 = cstr2.GetLength () + 1;
Char chArray [100];
Memset (chArray, 0, sizeof (bool) * 100); // Empty the garbage content of the array.

Strncpy (chArray, cstr2, strLength2);

Zh

1.String to CString
     CString.format ("% s", string.c_str ());
Char * to CString
    CString.format ("% s", char *);
3.char * to string
    string s (char *);
4.String to char *
     char * p = string.c_str ();
5.CString to string
     string s (CString.GetBuffer (CString.GetLength ()));
6.CString to char *
    charpoint = strtest.GetBuffer (strtest.GetLength ());
    It is not recommended to use (LPCTSTR) for forced type conversion, so an error will occur when the size of strtest changes.
7. CString to char [100]
      char a [100];
      CString str ("aaaaaa");
      strncpy (a, (LPCTSTR) str, sizeof (a));
Attachment: Cstring related member functions: http://www.cnblogs.com/Caiqinghua/archive/2009/02/16/1391190.html

Microsoft's summary of this: http://msdn.microsoft.com/zh-cn/library/vstudio/ms235631.aspx
————————————————
Copyright statement: This article is an original article by the CSDN blogger "DayThinking", which follows the CC 4.0 BY-SA copyright agreement. Please reprint the original source link and this statement.
Original link: https://blog.csdn.net/sszgg2006/java/article/details/8667945
Reply

Use magic Report

1

Threads

6

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

 Author| Post time: 2020-4-1 16:15:01
| Show all posts
This is in C ++, the CString string is converted to char for writing to a file! !!
Reply

Use magic Report

0

Threads

5

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 Korea, Republic of

Post time: 2020-4-4 22:45:01
| Show all posts
Brother, isn't it already converted into char *?
Can't you convert char * into a string?
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-4-14 03:00:01
| Show all posts
CString path;
char * buff;

buff = new char [path.GetLength () + 1]; // +1 = + null
wcstombs (buff, path, path.GetLength ()); // UNICODE-> Multi byte
buff [path.GetLength ()] = '\0';

cout << buff;
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list