简体   繁体   English

将多个 arguments(它们是来自多个 txt 文件的文件名)传递给在 linux 终端中运行的 c 程序

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

I have a project that requires converting a big list of.bin files to an equivalent big list of.csv files.我有一个项目需要将 .bin 文件的大列表转换为等效的 .csv 文件的大列表。 The converter script is already written in c by a company called agilent, the catch 22 is the script only converts one.bin file at a time.转换器脚本已经由一家名为 Agilent 的公司编写在 c 中,catch 22 是该脚本一次仅转换一个.bin 文件。 but once converted, it will be in the correct format for my project但一旦转换,它将是我项目的正确格式

The c program (called agilent_bin_reader.c) works fine when I type the arguments manually, but when the arguments come from a.txt file then the file opener fails to open the file.当我手动输入 arguments 时,c 程序(称为 agilent_bin_reader.c)工作正常,但是当 arguments 从 a.txt 文件打开时,文件打开器将失败。 I want to automate the process to run the c program thousands of time so I created a script.sh that repeats the process until EOF of either one of the txt files.我想自动化该过程以数千次运行 c 程序,因此我创建了一个 script.sh 重复该过程直到任一 txt 文件的 EOF。

Manually entered Example) linux_shell$./agilent_bin_reader des_trace1.bin trace1.csv手动输入 示例) linux_shell$./agilent_bin_reader des_trace1.bin trace1.csv

Saves the binary information from des_trace1.bin into trace1.csv perfectly.将 des_trace1.bin 中的二进制信息完美保存到 trace1.csv 中。

My problem is when I run this c program using my script.sh I get the error that the file basically failed to open.我的问题是当我使用我的 script.sh 运行这个 c 程序时,我收到文件基本上无法打开的错误。 Its a problem with this part of the c code: c 代码的这一部分存在问题:

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

because the if never evaluates to true.因为 if 永远不会评估为 true。 the else that follows basically kicks me out of the code.后面的 else 基本上把我踢出了代码。

Heres my script.sh that Ive created:这是我创建的 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

As one can see, the arguments here are names1.txt and names2.txt as they pass into argv[1] and argv[2] respectively.可以看到,这里的 arguments 是names1.txtnames2.txt ,因为它们分别传入 argv[1] 和 argv[2]。 But even though the arguments are perfectly set up, its almost like the fopen thinks it should be looking in the names1.txt file for the.bin file but really the.bin files are sitting in the same folder as agilent_bin_reader.c, names1.txt, and names2.txt.但即使 arguments 已完美设置,它几乎就像 fopen 认为它应该在 names1.txt 文件中查找 .bin 文件,但实际上 .bin 文件与 agilent_bin_reader.c,names1 位于同一文件夹中。 txt 和 names2.txt。 There shouldnt be any issues with directories unless my script.sh makes the fopen only look at the.txt file that the argv[] goes into.目录不应该有任何问题,除非我的 script.sh 使fopen只查看 argv[] 进入的 .txt 文件。

I want the code to do this basically:我希望代码基本上做到这一点:

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

My names1.txt file looks like:我的 names1.txt 文件如下所示:

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

My names2.txt file looks like:我的 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} 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 in the comments section found that my issue was my txt files had hidden characters in them.. Ran dos2unix and my problem instantly went away. Shawn 在评论部分发现我的问题是我的 txt 文件中有隐藏的字符。运行 dos2unix,我的问题立即消失了。 See this website for a detailed explanation: https://www.liquidweb.com/kb/dos2unix-removing-hidden-windows-characters-from-files/详细说明见本网站: https://www.liquidweb.com/kb/dos2unix-removing-hidden-windows-characters-from-files/

Here are the commands I ran:这是我运行的命令:

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