我有這些行會中斷并引發錯誤 439 memcpy(&path[0] strlen("ls /proc/"),pid,1);
這是代碼
void get_pid()
{
char line[1000];
FILE *cmd = popen("pidof a.out", "r");
fgets(line, 100, cmd);
pid_t pid = strtoul(line, NULL, 10);
char *path=malloc(sizeof(char)*100);
memset(path,0,100);
memcpy(path,"ls /proc/",strlen("ls /proc/"));
memcpy(&path[0] strlen("ls /proc/"),pid,1);
system(path);
free(path);
return;
}
有沒有什么巧妙的方法來完成我只是按照我嘗試的方式執行 ls 命令/proc/并與我得到的 pid 連接system(pidof)實際上我需要進入ls /proc/my_pid=pid/fd/并找到套接字 fd。/proc/pid/fd/目錄下只有一個socket檔案
如何在 C 中做到這一點
實際上這條線是麻煩制造者
memcpy(&path[0] strlen("ls /proc/"),pid,1);
我只是將第一個字符的地址加上 strlen,然后用 mcmcpy 寫入 1。但顯然上面是錯誤的,因為我的 memcpy 的第二個引數是 int 型別,它是 4 位元組,它期望 char 指標是 8 位元組。我可以投射它可能是但我需要在 C 中以正確的方式進行。任何人都可以幫忙嗎
uj5u.com熱心網友回復:
通常建議您嘗試做什么以及資料是什么型別。
你首先使用popen命令來提取結果pidof a.out。fgets會給你一個以換行符結尾的字串。只需將最后一個字符替換為 null
...
fgets(line, 100, cmd);
line[strlen(line) -1] = '\0';
您稍后想將該值連接到另一個字串......將其轉換為 pid_t 只是將其轉換回字串是沒有用的(除非您想控制它的正確格式)。
此外,memxxx建議使用這些函式處理可能包含空字符的任意字符陣列。在這里,您只處理以空字符結尾的字串,因此您應該使用這些strxxx函式。
最后,除非您想嘗試一下,否則對固定大小的陣列使用動態分配是非慣用的……您想要的是:
char path[100] = "ls /proc/";
這是足以讓100個字符陣列初始化與內容"ls /proc"后面的空字符。
只需連接您的初始字串:
strcat(path, line);
system(path);
無論如何,這仍然是糟糕的代碼,因為你永遠不應該依賴輸入資料,并且應該始終測驗你得到的是你所期望的,但我會保留另一個答案......
uj5u.com熱心網友回復:
有很多方法可以找到當前行程的 pid,然后獲取行程目錄的目錄串列——但是你讓它變得比需要的更難。
大多數編譯提供了 POSIX 函式getpid(),它將檢索當前行程 pid。要確定有多少個字符需要合并"ls /proc/"和pid,只是使用舊snprinf()把戲與緩沖區NULL,并設定為字符的數量0和snprintf()將回傳長度將被要求持有完成字串,如
len = snprintf (NULL, 0, "ls /proc/%d", pid); /* determine len req'd */
使用長度,您可以分配len 1位元組然后用于sprintf()填充字串,例如
if (!(path = malloc (len 1))) { /* allocate len 1 */
perror ("malloc-path");
return;
}
sprintf (path, "ls /proc/%d", pid); /* create path */
剩下的就是system()撥打您的電話。總而言之,您可以這樣做:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
void get_pid ()
{
char *path;
int len;
pid_t pid = getpid(); /* get pid of current proc */
len = snprintf (NULL, 0, "ls /proc/%d", pid); /* determine len req'd */
if (!(path = malloc (len 1))) { /* allocate len 1 */
perror ("malloc-path");
return;
}
sprintf (path, "ls /proc/%d", pid); /* create path */
system (path); /* list contents of process directory */
free (path); /* free path */
}
int main () {
get_pid();
}
(注意:為了您的使用,您可以避免長度檢查和動態分配,只需將其宣告path為固定大小的陣列,例如char path[128];就足夠了,然后您可以簡單地呼叫sprintf()并創建所需的字串)
使用固定大小的版本path簡化為:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
void get_pid ()
{
char path[128]; /* fixed buffer to hold final string */
pid_t pid = getpid(); /* get pid of current proc */
sprintf (path, "ls /proc/%d", pid); /* create path */
system (path); /* list contents of process directory */
}
int main () {
get_pid();
}
無論哪種方式都很好。
示例使用/輸出
運行代碼確實提供了當前行程目錄的串列,例如
$ ./bin/lsprocpid
attr coredump_filter fdinfo make-it-fail mountstats oom_score_adj sched stat timerslack_ns
auxv cpuset gid_map map_files net pagemap schedstat statm uid_map
cgroup cwd io maps ns patch_state sessionid status wchan
clear_refs environ latency mem numa_maps personality setgroups syscall
cmdline exe limits mountinfo oom_adj projid_map smaps task
comm fd loginuid mounts oom_score root stack timers
這可能是更簡潔的方法之一。等有很多方法strcat(),但是當您需要以printf()類似的格式字串混合數字和文本時,使用sprintf()來創建組合字串與您所擁有的一樣簡單。
如果您還有其他問題,請仔細查看并告訴我。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/406068.html
標籤:
上一篇:在二維陣列上使用函式
