簡體   English   中英

將多個 arguments(它們是來自多個 txt 文件的文件名)傳遞給在 linux 終端中運行的 c 程序

[英]passing multiple arguments that are file names from multiple txt files to a c program ran in linux terminal

我有一個項目需要將 .bin 文件的大列表轉換為等效的 .csv 文件的大列表。 轉換器腳本已經由一家名為 Agilent 的公司編寫在 c 中,catch 22 是該腳本一次僅轉換一個.bin 文件。 但一旦轉換,它將是我項目的正確格式

當我手動輸入 arguments 時,c 程序(稱為 agilent_bin_reader.c)工作正常,但是當 arguments 從 a.txt 文件打開時,文件打開器將失敗。 我想自動化該過程以數千次運行 c 程序,因此我創建了一個 script.sh 重復該過程直到任一 txt 文件的 EOF。

手動輸入 示例) linux_shell$./agilent_bin_reader des_trace1.bin trace1.csv

將 des_trace1.bin 中的二進制信息完美保存到 trace1.csv 中。

我的問題是當我使用我的 script.sh 運行這個 c 程序時,我收到文件基本上無法打開的錯誤。 c 代碼的這一部分存在問題:

inputFile = fopen(argv[1], "rb");
if (inputFile)

因為 if 永遠不會評估為 true。 后面的 else 基本上把我踢出了代碼。

這是我創建的 script.sh:

while 
  IFS= read -r a1 <&3 &&
  IFS= read -r a2 <&4
do
  ./agilent_bin_reader "$a1" "$a2" 3<&- 4<&-
done 3< names1.txt 4< names2.txt

可以看到,這里的 arguments 是names1.txtnames2.txt ,因為它們分別傳入 argv[1] 和 argv[2]。 但即使 arguments 已完美設置,它幾乎就像 fopen 認為它應該在 names1.txt 文件中查找 .bin 文件,但實際上 .bin 文件與 agilent_bin_reader.c,names1 位於同一文件夾中。 txt 和 names2.txt。 目錄不應該有任何問題,除非我的 script.sh 使fopen只查看 argv[] 進入的 .txt 文件。

我希望代碼基本上做到這一點:

linux_shell$ ./agilent_bin_reader names1.txt(1st line) names2.txt(1st line)
linux_shell$ ./agilent_bin_reader names1.txt(2nd line) names2.txt(2nd line)
linux_shell$ ./agilent_bin_reader names1.txt(3rd line) names2.txt(3rd line)
....
....
linux_shell$ ./agilent_bin_reader names1.txt(81,569th line) names2.txt(81,569th line)
*EOF of both .txt files, iteration complete

我的 names1.txt 文件如下所示:

des_trace1.bin
des_trace2.bin
des_trace3.bin
...
des_trace81569.bin

我的 names2.txt 文件如下所示:

trace1.csv
trace2.csv
trace3.csv
...
trace81569.csv

Here is the converter code supplied to me (Is found at http://www.dpacontest.org/agilent_bin_reader.c ) {I named this program agilent_bin_reader.c just like the company who created it did}

Shawn 在評論部分發現我的問題是我的 txt 文件中有隱藏的字符。運行 dos2unix,我的問題立即消失了。 詳細說明見本網站: https://www.liquidweb.com/kb/dos2unix-removing-hidden-windows-characters-from-files/

這是我運行的命令:

linux_shell$ sudo apt install dos2unix
linux_shell$ dos2unix names1.txt
linux_shell$ dos2unix names2.txt

暫無
暫無

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

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