cstring

关于用strtok_s处理过的CString会导致之前的CString之间的赋值被同化的问题的解决方案

老子叫甜甜 提交于 2019-11-29 13:36:43
问题现象: 实验一: 在MFC中,如果实现 CString a, b; a = "12/24"; b = a; char * next, *item; item = strtok_s((LPSTR)(LPCTSTR)a, "/", &next); 会发现这时a和b的值相同,都是12,而不是b是12/24,a是12。 然而通常情况下,b=a操作是值传递,之后对a做修改是不会影响到b的,这里却影响到了。 *********************** 实验二: CString a, b; a = "12/24"; b = a; a="12"; 这时候, b是12/24,a是12。一切正常。 为了使实验一a不影响到b,解决方法: ********************** 实验三: CString a, b; a = "12/24"; b = ""; b += a; char * next, *item; item = strtok_s((LPSTR)(LPCTSTR)a, "/", &next); 之后在对a重新赋值就不会对b产生影响了。 暂时还不知道为什么会产生这种变化,感觉好像a和b内存共享了!!! 来源: CSDN 作者: jiayinhaoran 链接: https://blog.csdn.net/jiayinhaoran/article/details/51657471

Objective-C - Converting NSString to a C string [duplicate]

限于喜欢 提交于 2019-11-29 12:26:49
Possible Duplicate: objc warning: “discard qualifiers from pointer target type” I'm having a bit of trouble converting an NSString to a C string. const char *input_image = [[[NSBundle mainBundle] pathForResource:@"iphone" ofType:@"png"] UTF8String]; const char *output_image = [[[NSBundle mainBundle] pathForResource:@"iphone_resized" ofType:@"png"] UTF8String]; const char *argv[] = { "convert", input_image, "-resize", "100x100", output_image, NULL }; // ConvertImageCommand(ImageInfo *, int, char **, char **, MagickExceptionInfo *); // I get a warning: Passing argument 3 'ConvertImageCommand'

MFC- 网络编程

匆匆过客 提交于 2019-11-29 08:30:38
一、MFC网络编程 a)CAsyncSocket用于异步非阻塞类,用UDP通信; b)CAsyncSocket的子类(派生类);Csocket同步阻塞类,用于TCP通信; c)通信前,必须调用AfxSocketInit()进行初始化 二、CAsyncSocket类UDP通信 2.1 服务器端通信步骤: a)在.h文件中创建CAsyncSocket类对象; b)创建套接字CAsyncSocket::Create(); c)发送数据CAsyncSocket::SentTo(); d)关闭套接字CAsyncSocket::Close(); 2.2 MFC界面 2.3 代码实例: a)01_UDPServerDlg.h中添加CAsyncSocket对象 1 // 01_UDPServerDlg.h : 头文件 2 // 3 4 #pragma once 5 #include "afxcmn.h" 6 #include "afxwin.h" 7 8 9 // CMy01_UDPServerDlg 对话框 10 class CMy01_UDPServerDlg : public CDialogEx 11 { 12 // 构造 13 public: 14 CMy01_UDPServerDlg(CWnd* pParent = NULL); // 标准构造函数 15 16 // 对话框数据 17

How can I hash a string to an int using c++?

核能气质少年 提交于 2019-11-29 02:55:02
问题 I have to write my own hash function. If I wanted to just make the simple hash function that maps each letter in the string to a numerical value (i.e. a=1, b=2, c=3, ...), is there a way I can perform this hash on a string without having to first convert it to a c-string to look at each individual char? Is there a more efficient way of hashing strings? 回答1: Re the first question, sure, e.g, something like: int hash = 0; int offset = 'a' - 1; for(string::const_iterator it=s.begin(); it!=s.end(

Simply encrypt a string in C

冷暖自知 提交于 2019-11-29 02:22:41
I'm trying to encrypt a query string on a game I'm making when opening a url. It doesn't have to be complicated, in fact since I'm working from a game engine it needs to be as simple as possible. It tends to fuss if I get too low level. I've already created the query string, I just need to take each char of it and subtract 15 from the char to lightly encrypt it. I'm just wanting to make a simple encryption that will deter most users. I wish I could give a code example but I'm not too experienced in C, and I'm not even sure where to begin. The game engine's api usually makes everything simple

Working with C strings in Swift, or: How to convert UnsafePointer<CChar> to CString

泄露秘密 提交于 2019-11-29 01:07:48
While playing with Standard C Library functions in Swift, I came across problems when passing C strings around. As a simple example (just to demonstrate the problem), the Standard C Library function char * strdup(const char *s1); is exposed to Swift as func strdup(_: CString) -> UnsafePointer<CChar> which means that the return value of strdup() cannot be passed to another strdup() call: let s1 : CString = "abc" let s2 = strdup(s1) // OK, s2 is a UnsafePointer<CChar> let s3 = strdup(s2) // error: could not find an overload for '__conversion' that accepts the supplied arguments My question is:

VC++中的CString、char、int类型转换

断了今生、忘了曾经 提交于 2019-11-28 20:30:52
1、如何将CString类型的变量赋给char*类型的变量 方法一:GetBuffer函数 使用CString::GetBuffer函数。 char *p; CString str="hello"; p=str.GetBuffer(str.GetLength()); str.ReleaseBuffer(); 将CString转换成char * 时 CString str("aaaaaaa"); strcpy(str.GetBuffer(10),"aa"); str.ReleaseBuffer(); 当我们需要字符数组时调用GetBuffer(int n),其中n为我们需要的字符数组的长度.使用完成后一定要马上调用ReleaseBuffer(); 还有很重要的一点就是,在能使用const char *的地方,就不要使用char * 方法二:memcpy CString mCS=_T("cxl"); char mch[20]; memcpy(mch,mCS,20); 2、CString类向const char *转换 char a[100]; CString str("aaaaaa"); strncpy(a,(LPCTSTR)str,sizeof(a)); 或者如下: strncpy(a,str,sizeof(a)); 以上两种用法都是正确地.

MFC常用函数总结

佐手、 提交于 2019-11-28 18:16:56
1、MFC编辑框、静态文本框相关的常用函数 《1》GetDlgItemText(ID ,str) 作用:从对话框中获取文本 第一个参数为要获取的编辑框(或者静态文本框、单选按钮等可以显示内容的控件)的ID,第二个参数为字符串(Cstring 类型)的变量,获取的文本存储在str中。 《2》SetDlgItemText(ID,str) 作用:将字符串显示在控件中 第一个参数为要显示的编辑框(或者静态文本框、单选按钮、组合框等可以显示内容的控件)的ID,第二个参数为字符串(Cstring 类型)的变量,显示的文本存储在str中。如果要显示的变量的类型不是Cstring,则通过Format函数强制转换。 通常还要加一个UpDateData(FALSE)。 《3》UINT nID=GetCheckedRadioButton(IDC1, IDC2); 作用:获取单选框的选项的ID 第一个参数为该组合框中第一个单选按钮的ID,第二个参数为该组合框中最后一个按钮的ID。 《4》CheckRadioButton(IDC1, IDC2, IDC3); 作用:初始化单选按钮 第一个参数为该组合框中第一个单选按钮的ID,第二个参数为该组合框中最后一个按钮的ID,第三个参数为为缺省选项的ID。 《5》m_scrollBar.SetScrollRange(0, 500) ; 作用:设置水平滚动条的取值范围

GetCurrentTime() and CFile option

早过忘川 提交于 2019-11-28 15:30:14
获取当前时间 CTime tm = CTime::GetCurrentTime(); 将当前时间转换成sctring类型+.txt 1 CString str2; 2 str2.Format(_T("%d%d%d_%d%d%d.txt"),tm.GetYear(),tm.GetMonth(),tm.GetDay(),tm.GetHour(),tm.GetMinute(),tm.GetSecond()); 将字符串定义为文件名称,并创建该文件,将编辑框内容写入文件内: 1 CFile c_flie; 2 if(c_flie.Open(str2,CFile::modeWrite|CFile::modeCreate)) 3 { 4 CString m_str; 5 CEdit *pEdit = (CEdit *)this->GetDlgItem(IDC_EDIT15); 6 pEdit->GetWindowText(m_str); 7 c_flie.Write(m_str,m_str.GetLength()); 8 c_flie.Close(); 9 } End thanks. 来源: https://www.cnblogs.com/lumao1122-Milolu/p/11412705.html

在CTreeCtrl控件点击事件中获取点击的项

守給你的承諾、 提交于 2019-11-28 14:59:24
网上搜了一下,有两种方法: 1、使用GetSelectedItem() HTREEITEM hItem = m_treeCtrl.GetSelectedItem(); CString strText = m_treeCtrl.GetItemText(hItem); MessageBox(strText); 2、使用HitTest() CPoint pt; GetCursorPos(&pt); m_treeCtrl.ScreenToClient(&pt); UINT uFlags; HTREEITEM hItem = m_treeCtrl.HitTest(pt, &uFlags); CString strText = m_treeCtrl.GetItemText(hItem); MessageBox(strText); 总结:方法没有达到要求,因为在点击事件使用GetSelectedItem()获取的项是CTreeCtrl控件选中的项,并不一定是点击的项,因为它只会返回上次点击的项,因为上次点击的项在这次事件中是出于选中状态的,因此使用方法2. 来源: http://www.cnblogs.com/lit10050528/p/4141852.html