[Solved] Read a file using argv


You can run the program as:

IFS=$'\n'
./sort.exe $(cat file.txt)

Each line of the file will then be an argument to the program (setting IFS to just newline prevents spaces in the file from being argument delimiters).

Then the program can loop over argv (except for argv[0], which contains the program name) to get all the lines to sort.

Note that there’s a limit on the size of the arguments, so this method is not very scalable. Reading from the file is generally preferred.

FYI, $(cat file.txt) can also be written as $(< file.txt)

9

solved Read a file using argv