Tag: string

_T()宏更改UNICODE字符数据

我有UNICODE应用程序,我们使用_T(x),其定义如下。 #if defined(_UNICODE) #define _T(x) L ##x #else #define _T(x) x #endif 我知道L被定义为wchar_t,在任何平台上都是4个字节。 如果我错了,请纠正我。 我的要求是我需要L为2个字节。 因此编译器hack我开始使用-fshort-wchar gcc标志。 但是现在我需要将我的应用程序移动到zSeries,在那里我无法看到-fshort-wchar标志在该平台中的效果。 为了让我能够在zSeries上移植我的应用程序,我需要以这样的方式修改_T()宏,即使在使用L ## x并且不使用-fshort-wchar标志之后,我需要获得2byte宽字符data.Can有人告诉我如何更改L的定义,以便我可以在我的应用程序中将L定义为2个字节。

C – strtok上的意外分段错误(…)

我正在使用库的strtok(…),它似乎工作正常,直到结束条件,它导致分段错误和程序崩溃。 API声称strtok(…)将在没有更多令牌被找到时输出NULL,这意味着,我想,你必须捕获这个NULL才能终止你使用strtok运行的任何循环( …)。 我需要做什么才能捕获此NULL以防止程序崩溃? 我想象允许使用NULL作为终止条件。 我准备了一个SSCCE供你观察这种行为。 我需要strtok(…)来处理我正在编写的更大的软件,并且我得到完全相同的分段行为。 命令行的输出显示在此代码插图下方(是的,我知道您使用来封装库,但是我很难获得此post来显示代码库)。 我在Windows 8操作系统上使用gcc 4.5.3版本,下面显示了两种不同的方式,我想象一个人可以尝试在循环中捕获NULL。 #include #include #include #include #include #include #include main(){ char* from = “12.34.56.78”; char * ch = “.”; char * token = strtok(from, ch); printf(“%s\n”,token); while(token != NULL){ token = strtok(NULL, ch); printf(“%s\n”, token); } printf(“Broke out of loop!”); while(strcmp(token, 0) != 0){ printf(“%s\n”,token); token […]

C编程:只从fgets打印int

看到这个main : int main(void) { int i; int ch; char str[512]; fgets(str, sizeof str, stdin); for (i = 0; i <= (strlen(str)); i++) { if (str[i] != '\0' && str[i] != '\n') { int num = atoi(&str[i]); printf("%d\n", num); } } return 0; } 我希望得到用户的数字,并获得所有数字,没有任何spaces或tabs 。 例如: 输入1 2 3 。 但在这种情况下这输出: 1 2 2 […]

使用C风格的字符串有哪些缺点?

我知道缓冲区溢出是使用C风格字符串(char数组)的一个潜在危险。 如果我知道我的数据适合我的缓冲区,是否可以使用它们? 我还需要注意C风格字符串固有的其他缺点吗? 编辑:这是一个接近我正在做的事情的例子: char buffer[1024]; char * line = NULL; while ((line = fgets(fp)) != NULL) { // this won’t compile, but that’s not the issue // parse one line of command output here. } 此代码从使用popen(“df”)命令创建的FILE指针获取数据。 我正在尝试运行Linux命令并解析其输出以获取有关操作系统的信息。 以这种方式将缓冲区设置为任意大小是否有任何错误(或危险)?

char *和wchar_t之间的区别*

我是MFC的新手。 我正在尝试做简单的mfc应用程序,我在某些地方变得混乱。 例如, SetWindowText有两个api, SetWindowTextA , SetWindowTextW一个api取char *而另一个接受wchar_t * 。 char *和wchar_t *什么用?

如何使此函数采用任意字符串?

所以基本上,现在,这个函数只能用9个单词,每个10个字符。 如何制作它以便它可以采用arbitrary数量的words和characters并按字母顺序对其进行相应的排序? int sortText(){ char name[10][9], tname[10][9], temp[10]; int i, j, n; printf(“Enter the amount of words you want to sort (max 9):”); scanf(“%d”, &n); printf(“Enter %d words: “,n); for (i = 0; i < n; i++) { scanf("%s", name[i]); strcpy(tname[i], name[i]); } for (i = 0; i < n – 1 ; i++){ for […]

将字符串转换为C中的双变量

我编写了以下代码….它应该将像“88”这样的字符串转换为double值88并打印出来 void convertType(char* value) { int i = 0; char ch; double ret = 0; while((ch = value[i] )!= ‘\0’) { ret = ret*10 +(ch – ‘0’); ++i; } printf(“%d”,ret);//or %f..what is the control string for double? } //input string :88 但它总是打印0 …但是当我将ret的类型更改为int …它工作正常…当类型为float或double时,它打印为零…所以为什么我得到这个模棱两可的结果?

如何只打印字符串的某些部分?

我有一个字符串const char[15] ,我想像这样打印它: 标签一: 字符[0,13) 标签二: 字符[13,15] 如何只打印字符串的某些部分?

使用ctypes将python字符串对象转换为c char *

我试图使用ctypes从Python(3.2)向C发送2个字符串。 这是我的Raspberry Pi上项目的一小部分。 为了测试C函数是否正确接收到字符串,我将其中一个放在文本文件中。 Python代码 string1 = “my string 1” string2 = “my string 2” # create byte objects from the strings b_string1 = string1.encode(‘utf-8’) b_string2 = string2.encode(‘utf-8’) # send strings to c function my_c_function(ctypes.create_string_buffer(b_string1), ctypes.create_string_buffer(b_string2)) C代码 void my_c_function(const char* str1, const char* str2) { // Test if string is correct FILE *fp = fopen(“//home//pi//Desktop//out.txt”, […]

实现strnstr

我试图将一个strnstr函数实现为C(strstr,但它检查长度),由于某种原因它不起作用(输出总是没有): #include char *searchingFor = “stackdummy”; char *in = “la da\ndoo a da\nnow here comes the stack\nok there it was.\n”; char *strnstr(char *s1, char *s2, int length) { if(s1 == NULL || s2 == NULL) return NULL; printf(“searching \n\n\”%s\”\n for %.*s\n”, s1, length, s2); char *ss1 = malloc(strlen(s1) + 1); strcpy(ss1, s1); char *ss2 = […]