簡體   English   中英

如何在 Linux 中使用 popen() 在 C 中讀取包含轉義字符的字符串?

[英]How to read string including escape characters in C using popen() in Linux?

我必須使用 C 中的 popen() 顯示 Linux(更具體地說,Ubuntu 19.10)目錄中的所有文件和子目錄。相關代碼如下。 我調試這段代碼時的問題是,“list”變量最多只包含第一個“\\n”轉義字符,即“.:\\n”。 如何繞道以便 popen() 輸出包括轉義序列字符在內的所有字符串?

#include <stdio.h>

int main()
{
    FILE *read_file;
    char list[1000];

    read_file = popen("ls -R","r");
    fgets(list, 1000, read_file);
    pclose(read_file);

    printf("%s", list);

    return(0);
}

@M 哦。 我試着像你評論的那樣。 但是當我嘗試執行“fgets(list[++i], sizeof(char), read_file);”時,會出現分段錯誤。

#include <stdio.h>

int main()
{
    FILE *read_file;
    char list[1000];
    int i = -1;

    read_file = popen("ls -R","r");
    do
    {
        fgets(list[++i], sizeof(char), read_file);
    }while(list[i] != NULL);
    pclose(read_file);

    printf("%s", list);

    return(0);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM