我有一個可執行程序,它只接受兩個參數「文件的打開命令./file和另一個參數」。我想要的是知道如何設置一個命令,讓文件讀取文件而不將其作爲參數在linux上傳遞。我沒有該程序的源代碼,但我在互聯網上發現了這個C代碼,我認爲它可能是相似的。讀取文件而不是將其作爲參數傳遞
#include <stdio.h>
int main (int argc, char *argv[])
{
if (argc != 2) /* argc should be 2 for correct execution */
{
/* We print argv[0] assuming it is the program name */
printf("usage: %s filename", argv[0]);
}
else
{
// We assume argv[1] is a filename to open
FILE *file = fopen(argv[1], "r");
/* fopen returns 0, the NULL pointer, on failure */
if (file == 0)
{
printf("Could not open file\n");
}
else
{
int x;
/* read one character at a time from file, stopping at EOF, which
indicates the end of the file. Note that the idiom of "assign
to a variable, check the value" used below works because
the assignment statement evaluates to the value assigned. */
while ((x = fgetc(file)) != EOF)
{
printf("%c", x);
}
fclose(file);
}
}
}
'$ alias executable ='executable hidden_file'' – pmg
'$ ./my_program.out