2016-11-16 232 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」它表示。如果你有時間可以用你的編譯器來試試它,我想知道現在編譯器是否有問題。 –

相關問題