[問題] 從參數傳 separator 進 C 程式

看板C_and_CPP (C/C++)作者 (Neisseria)時間7年前 (2018/05/16 22:20), 7年前編輯推噓3(307)
留言10則, 3人參與, 7年前最新討論串1/1
開發平台(Platform): (Ex: Win10, Linux, ...) 目前是 Mac,但類 Unix 系統應該都可以 使用 sh-compatible shell,Bash 或 Zsh 編譯器(Ex: GCC, clang, VC++...)+目標環境(跟開發平台不同的話需列出) GCC 或 Clang 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) 問題(Question): 這有點偏特定系統問題,但牽涉到 C 實作,故在此板問 想要把 separator 傳入 C 程式中 [Update on 2018/05/17] 後來就造了一個輪子,自已解析 separator,效果如下: $ ./sep_parse ' ' --> <-- $ ./sep_parse 'n' -->n<-- $ ./sep_parse '\t' --> <-- $ ./sep_parse '\n' --> <-- $ ./sep_parse '\n\t' --> <-- 只用到標準函式庫的功能,這輪子至少堪用 餵入的資料(Input): 見下例 預期的正確結果(Expected Output): $ ./prog -s "\t" a b c a b c 錯誤結果(Wrong Output): $ ./prog -s "\t" a b c a\tb\tc $ ./prog -s '\t' a b c a\tb\tc 這樣會正確: $ ./prog -s $'\t' a b c a b c 但儘量不要用這個方法,至少大部分 GNU/Linux 程式不會這樣傳參數 程式碼(Code):(請善用置底文網頁, 記得排版,禁止使用圖檔) 僅供參考 https://pastebin.com/bA2ZDEYv 補充說明(Supplement): C 和 C++ 板惠我良多 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 112.104.89.132 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1526480414.A.E88.html

05/17 00:40, 7年前 , 1F
$' ... '
05/17 00:40, 1F

05/17 00:40, 7年前 , 2F
Quoted string expansion.衮his construct expands si
05/17 00:40, 2F

05/17 00:40, 7年前 , 3F
ngle or multiple escaped octal or hex values into
05/17 00:40, 3F

05/17 00:40, 7年前 , 4F
ASCII衞3]覔r袪nicodecharacters.
05/17 00:40, 4F

05/17 00:40, 7年前 , 5F
05/17 00:40, 5F

05/17 01:35, 7年前 , 6F
這要看你用的是哪個shell
05/17 01:35, 6F

05/17 01:35, 7年前 , 7F
相容性最高的寫法應該是 "$(printf '\t')"
05/17 01:35, 7F
※ 編輯: Neisseria (112.104.89.132), 05/17/2018 05:09:05

05/17 12:57, 7年前 , 8F
'\t' 或 "\t" 的 \t 在 shell 下改按 CTRL-V TAB 可否?
05/17 12:57, 8F

05/17 13:02, 7年前 , 9F
不過上述的幾種作法都無關 C 實作...
05/17 13:02, 9F

05/17 13:05, 7年前 , 10F
或是你是求解把 -s 後的參數, 若看到 '\\' + 't' 轉成 '\t' ?
05/17 13:05, 10F
※ 編輯: Neisseria (203.71.94.20), 05/17/2018 13:26:55 ※ 編輯: Neisseria (60.251.46.166), 12/24/2018 16:59:52
文章代碼(AID): #1Q_3uUw8 (C_and_CPP)
文章代碼(AID): #1Q_3uUw8 (C_and_CPP)