繁体   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