2016-11-16 228 views
2

我正在尝试为我的项目创建过程。我会通过从父母的子进程中获得参数,并且参数会及时更改,所以我想先尝试将1传递给子进程。字符串格式应该是这样的“childname.exe c”,其中c表示随机字符(在这种情况下,仅用于试用)。从字符串数组中获取第一个字符串c

我创建了一个childname数组,并且我想要的是将新的字符串与childname字符串连接起来,并将其复制到另一个字符串数组(lpCommandLine变量)。当我调试下面的代码时,我看到child_name [0](当我等于0时)只返回'C',但我期望它返回“ChildProj1.exe”。有没有一点我错过了或如何在c中完成?

这里有什么,我getin调试器图像:here stored values of in variables

#define NO_OF_PROCESS 3 

char *child_names[]= {"ChildProj1.exe", "ChildProj2.exe", "ChildProj3.exe" }; 
char* lpCommandLine[NO_OF_PROCESS]; 
int i; 

    for (i = 0; i < NO_OF_PROCESS; i++) 
     lpCommandLine[i] = (char *)malloc(sizeof(char) * 16); 


    for (i = 0; i < NO_OF_PROCESS; i++) 
    { 
     strcat_s(child_names[i], strlen(child_names[i]), " 1"); 
     strcpy_s(lpCommandLine[i], strlen(lpCommandLine[i]), child_names[i]); 
    } 
+0

你认为在新的字符串“ 1”将被存储。 strcat_s失败,因为您尝试向缓冲区添加字符 - 请检查srcat_s的返回值 – pm100

+0

@ Y.E.S。目前还不清楚你将在数组lpCommandLine中获得什么。显示其结果内容。 –

+0

我希望它被存储在lpCommandLine中。在child_names数组中,第0个字符串是“ChildProj1.exe”,我希望lpCommandLine [0]是“ChildProj1.exe 1”。那么你是否建议为16个字符的每个child_names索引分配内存,这意味着ChildProj1.exe + 3(对于空白和1和\ 0) –

回答

1

从你的描述可以得出你想要得到的字符串这样

"childname.exe c" 

但是这个循环

for (i = 0; i < NO_OF_PROCESS; i++) 
{ 
    strcat_s(child_names[i], strlen(child_names[i]), " 1"); 
    strcpy_s(lpCommandLine[i], strlen(lpCommandLine[i]), child_names[i]); 
} 

不会做你想要什么。

这个循环是未定义的行为,因为在此声明

strcat_s(child_names[i], strlen(child_names[i]), " 1"); 

有修改字符串文字的一种尝试。您不能在C和C++中都更改字符串文字。

而且在此声明

strcpy_s(lpCommandLine[i], strlen(lpCommandLine[i]), child_names[i]); 

此调用

strlen(lpCommandLine[i]) 

也有不确定的行为,因为阵列指向这个指针lpCommandLine[i]不具有终止零。

没有任何需要使用功能strcat_sstrcpy_s。使用标准功能strcatstrcpy要好得多。

您在本演示程序中显示的内容如下。

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

#define NO_OF_PROCESS 3 

int main(void) 
{ 
    const char * child_names[]= 
    { 
     "ChildProj1.exe", 
     "ChildProj2.exe", 
     "ChildProj3.exe" 
    }; 

    const char *s = " 1"; 
    size_t n = strlen(s); 

    char* lpCommandLine[NO_OF_PROCESS]; 

    for (int i = 0; i < NO_OF_PROCESS; i++) 
    { 
     lpCommandLine[i] = (char *)malloc(strlen(child_names[i]) + n + 1); 
    } 

    for (int i = 0; i < NO_OF_PROCESS; i++) 
    { 
     strcpy(lpCommandLine[i], child_names[i]); 
     strcat(lpCommandLine[i], s); 
    } 

    for (int i = 0; i < NO_OF_PROCESS; i++) puts(lpCommandLine[i]); 

for (int i = 0; i < NO_OF_PROCESS; i++) free(lpCommandLine[i]); 

    return 0; 
} 

程序输出是

ChildProj1.exe 1 
ChildProj2.exe 1 
ChildProj3.exe 1 
0

而不是char * child_names[]你的意思是这样char[][] child_nameschar[] * child_nameschar ** child_names

+0

我想存储字符串在该数组中,已经尝试过char [] [],char **但没有工作。 –

+0

用char * child_names [],我试图打印每个元素,并用此代码printf(“%s”,child_names [i]);'正确打印。它像这样打印每个字符串,但是当我想要将事情连接起来时会变得疯狂。 –

0

做字符串连接做

size_t sz = strlen(child_names[i]) + 3; // space, '1' and \0 
char *buff = malloc(sz); 
strcat_s(buff,sz,child_names[i]); 
strcat_s(buff,sz," 1"); 
+0

关于childname [0] ='C'的问题是红鲱鱼。 YOu只需要在调试器中将其显示为字符串而不是字符 – pm100

+0

我刚刚尝试了它,并且在buff中只有“\ 0”它表示。如果你有时间可以用你的编译器来试试它,我想知道现在编译器是否有问题。 –

相关问题