cstring

Which is better code for converting BSTR parameters to ANSI in C/C++?

柔情痞子 提交于 2019-12-30 10:35:41
问题 So far I've discovered I can convert incoming BSTRs to ANSI in two (of many?) ways, and I'm curious to know whether one is "better" than the other with respect to speed / efficiency etc. The way I've been using for a while is use the USES_CONVERSION and W2A macros, e.g. BSTR __stdcall F(BSTR p1, BSTR p2 ) { USES_CONVERSION; LPSTR sNum1 = W2A( p1 ); LPSTR sNum2 = W2A( p2 ); Recently, however, I came across another technique: BSTR __stdcall F(BSTR p1, BSTR p2 ) { long amt = wcstombs( NULL, p1,

MFC中CString转成char *

五迷三道 提交于 2019-12-29 20:06:55
转换之前,首先了解几点: 工程的编码属性不同,CString的存储方式也不一样。 a) 比如unicode字符集时,CString会被定义成CStringW,其内部是wchar宽字符。每个英文字母也会占用2个字节,如果不转换直接输出则只能输出第一个字符(第二个字符为0被截断了)。 b) 多字节时,CString被定义为CStringA,其内部是char单字节字符。 CString有一个GetBuffer()函数, 可以获取指向字符缓冲区的非常量指针。 并且会锁定缓冲区长度。如果程序后面还要操作这个CString,且会修改长度。则一定记得要调用ReleaseBuffer()解除锁定。如果只是读取,则不用。当GetBuffer()有参数时,如果输入值小于缓冲区长度,则返回的指针跟前面说的一样。如果大于当前长度,则系统会重新realloc一份大的空间,覆盖原来的位置,实现动态增长。 一些常用的代号。 char - A tchar - T bstr - OLE wchar - w const - C mfc自带的一些宏定义转换。比如CT2A表示从tchar转到char。CW2A表示从宽字符转为单字符。后续发展时,我们只需使用CT2A即可,它兼容包括了w。 下面开始介绍CString转为char *的几种方法,以unicode工程属性为例: 用%S进行宽字符输出或者格式化。 char

Cannot modify C string

送分小仙女□ 提交于 2019-12-28 13:52:37
问题 Consider the following code. int main(void) { char * test = "abcdefghijklmnopqrstuvwxyz"; test[5] = 'x'; printf("%s\n", test); return EXIT_SUCCESS; } In my opinion, this should print abcdexghij. However, it just terminates without printing anything. int main(void) { char * test = "abcdefghijklmnopqrstuvwxyz"; printf("%s\n", test); return EXIT_SUCCESS; } This however, works just fine, so did I misunderstand the concept of manipulating C strings or something? In case it is important, I'm

Why do i first have to strcpy() before strcat()?

你离开我真会死。 提交于 2019-12-27 22:39:09
问题 Why does this code produce runtime issues: char stuff[100]; strcat(stuff,"hi "); strcat(stuff,"there"); but this doesn't? char stuff[100]; strcpy(stuff,"hi "); strcat(stuff,"there"); 回答1: strcat will look for the null-terminator, interpret that as the end of the string, and append the new text there, overwriting the null-terminator in the process, and writing a new null-terminator at the end of the concatenation. char stuff[100]; // 'stuff' is uninitialized Where is the null terminator? stuff

Why do i first have to strcpy() before strcat()?

陌路散爱 提交于 2019-12-27 22:38:18
问题 Why does this code produce runtime issues: char stuff[100]; strcat(stuff,"hi "); strcat(stuff,"there"); but this doesn't? char stuff[100]; strcpy(stuff,"hi "); strcat(stuff,"there"); 回答1: strcat will look for the null-terminator, interpret that as the end of the string, and append the new text there, overwriting the null-terminator in the process, and writing a new null-terminator at the end of the concatenation. char stuff[100]; // 'stuff' is uninitialized Where is the null terminator? stuff

CTreeCtrl 控件使用总结

两盒软妹~` 提交于 2019-12-27 10:32:10
一 基础操作 1 插入节点 1)插入根节点 [cpp] view plain copy //插入根节点 HTREEITEM hRoot; CString str=L "ROOT" hRoot=nTreeCtrl.InsertItem(str); //相当于 hRoot=nTreeCtrl.InsertItem(str,TVI_ROOT,TVI_LAST); 2)插入孩子节点 [cpp] view plain copy //加入hRoot节点的孩子节点,而且被加入的节点位于hRoot全部孩子节点的末尾 HTREEITEM hChild=nTreeCtrl.InsertItem(str,hRoot); //相当于 HTREEITEM hChild=nTreeCtrl.InsertItem(str,hRoot,TVI_LAST); 2 获得节点句柄 [cpp] view plain copy //获得根节点 HTREEITEM hRootItem; hRootItem=nTreeCtrl.GetRootItem(); //获得当前节点 HTREEITEM hCurrentItem; hCurrentItem=nTreeCtrl.GetSelectedItem(); //获得hItem的前一个节点 HTREEITEM hPreItem; hPreItem=nTreeCtrl

getting console input for Cstrings

Deadly 提交于 2019-12-24 09:14:48
问题 note: this is in C++ but using C-style strings hello SO, I'm working on an assignment and I need to get input from the console and save it to a cstring. Everything compiles fine, but when the program runs, it just skips over getting input from the user. So it will output: "Enter string to be inserted: " then skip the cin.getline function, then execute the next command. Here's my header files, the declaration of the cstring, and the line of code I'm having trouble with. #include "stdafx.h"

strncat & strncpy help c++

偶尔善良 提交于 2019-12-24 03:57:11
问题 So my assignment is: Using the strncpy and strncat functions in #include<cstring> , implement a function void concat(const char a[ ], const char b[ ], char result[ ], int result_maxlength) that concatenates the strings a and b to the buffer result . Be sure not to overrun the result. It can hold result_maxlength characters, not counting the \0 terminator. (That is, the buffer has buffer_maxlength + 1 bytes available.) Be sure to provide a ‘\0’ terminator. My solution (thus far) is below but I

背包问题

狂风中的少年 提交于 2019-12-24 00:25:20
检验代码 背包九讲 0-1背包 有 N 件物品和一个容量是 V 的背包。每件物品只能使用一次。 第 i 件物品的体积是 vi,价值是 wi。 求解将哪些物品装入背包,可使这些物品的总体积不超过背包容量,且总价值最大。 输出最大价值。 输入格式 第一行两个整数,N,V用空格隔开,分别表示物品数量和背包容积。 接下来有 N 行,每行两个整数 vi,wi,用空格隔开,分别表示第 i 件物品的体积和价值。 输出格式 输出一个整数,表示最大价值。 数据范围 0<N,V≤1000 0<vi,wi≤1000 输入样例 4 5 1 2 2 4 3 4 4 5 输出样例: 8 CODE: # include <iostream> # include <algorithm> # include <cstring> using namespace std ; const int N = 1010 ; int f [ N ] [ N ] ; int v [ N ] , w [ N ] ; int main ( ) { int n , m ; cin >> n >> m ; int i , j ; for ( i = 1 ; i <= n ; i ++ ) cin >> v [ i ] >> w [ i ] ; for ( i = 1 ; i <= n ; i ++ ) { for ( j = 0 ; j

Pointers and cstring length

梦想的初衷 提交于 2019-12-23 17:46:06
问题 I am setting pointers here one to point to name and one to point to name again but get the lenth. How come when i use cout << strlen(tail); it keeps telling me the lenth is 3? Even if i enter something that is 12? #include <iostream> #include <stdio.h> #include <string.h> using namespace std; int main() { char name[0]; cout << "Please enter your name: "; cin.getline(name, 256); cout << "Your name: " << name << endl; char* head = name; cout << head[6] << endl; char* tail = name; cout << strlen