简体   繁体   中英

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. 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. 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. 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.

Manually entered Example) linux_shell$./agilent_bin_reader des_trace1.bin trace1.csv

Saves the binary information from des_trace1.bin into trace1.csv perfectly.

My problem is when I run this c program using my script.sh I get the error that the file basically failed to open. Its a problem with this part of the c code:

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

because the if never evaluates to true. the else that follows basically kicks me out of the code.

Heres my script.sh that Ive created:

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. 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. 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.

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:

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

My names2.txt file looks like:

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 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. See this website for a detailed explanation: 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM