问题
Currently the code is reading the file and sort the records, as shown below,
#include"fileIO/file.h"
#define MAX_RECORD_SIZE 256 // Bad style
typedef struct{
int age;
char *lastName;
char *firstName;
}Person;
.....
int main(int argc, char *argv[]){
FILE *pFile = fopen("file.txt", "r");
....
callInsertionSort(getItemList(cache), getSize(getItemList(cache)), less);
}
where, file.txt
is,
Age,LastName,FirstName
50,B,A
30,A,B
20,X,D
10,F,A
10,A,B
90,V,E
60,N,M
Execution:
$./sort.exe Before sorting Age,LastName,FirstName 50 B A 30 A B 20 X D 10 F A 10 A B 90 V E 60 N M After sorting Age,LastName,FirstName 10 F A 10 A B 20 X D 30 A B 50 B A 60 N M 90 V E
Question:
Without using fopen()
, is there a way to get the shell to pass the contents of file.txt
as command-line arguments, one argument per line in the file, (through argv
by passing shell command (sort.exe
) parameters)?
回答1:
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)
回答2:
If the objective is to pass each line of the file as a separate command line argument to the sort.exe
program, then you will need Bash (which is in the tags) and you can use:
IFS=$'\n'; sort.exe $(cat file)
To demonstrate, take an alternative command, printf
, and a data file like this, using ⧫
to represent a blank:
verbiage
spaced⧫out
⧫leading⧫and⧫trailing⧫
⧫⧫multiple⧫⧫spaces⧫⧫everywhere⧫⧫
Running the command yields:
$ ( IFS=$'\n'; printf '[%s]\n' $(cat file) )
[verbiage]
[spaced out]
[ leading and trailing ]
[ multiple spaces everywhere ]
$
This uses the outer ( … )
to run the command in a sub-shell so that my interactive IFS
is not messed up. They're unnecessary in some other contexts.
Note that there's an upper-bound on how long the command line arguments (plus environment variables) can be — it is sometimes as much as 256 KiB, and seldom larger. This means you can only afford to do this with small enough files. In most circumstances, you'd be better off following the Unix (POSIX) sort
command and reading the data direct from the filename specified as an argument to the sort
command.
来源:https://stackoverflow.com/questions/41687782/read-a-file-using-argv