Lets say I get something like this in Linux / Bash:
./my_program <input_file.in
is there a way in my code to check the name of my input file?
something like this?
if (strcmp(in,"desired_input_file_name.in")) {
printf("success!!"\n);
}
Lets say I get something like this in Linux / Bash:
./my_program <input_file.in
is there a way in my code to check the name of my input file?
something like this?
if (strcmp(in,"desired_input_file_name.in")) {
printf("success!!"\n);
}
There is no portable simple way. Piping via <
will redirect the contents of input_file.in
to the standard input of my_program
. Its the same as if you had typed the contents of the file. If you want to know the filename then you need to pass that, eg as command line argument:
./my_program input_file.in
There's a non-portable solution. Linux has proc filesystem which can be used to get the info. You can get the file descriptor of stdin
and then get the filename from it - using fileno(3)
and readlink(3)
calls.
#include<stdio.h>
#include<unistd.h>
int main(void)
{
char buf[512], file[512] = {0};
snprintf(buf, sizeof buf, "/proc/self/fd/%d", fileno(stdin));
readlink(buf, file, sizeof file - 1);
printf("File name: %s\n", file);
}
(error checks omitted for brevity)