6

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);
}
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • What exactly is the problem you are facing? How to read input like this? Or how to use `strcmp` properly? – UnholySheep Jul 02 '20 at 13:08
  • 1
    I don't think you can do that if you stream the file contents to stdin via bash. However you can pass the filename as a parameter to your program for comparing, you then have to open the file yourself if you want the contents. – perivesta Jul 02 '20 at 13:10
  • You could change to filename as parameter to your program instead of input redirection (which I think makes impossible what you want to do). Would that be an option? – Yunnosch Jul 02 '20 at 13:12
  • Does this answer your question? [Read input.txt file and also output.bmp file from terminal (C-programming)](https://stackoverflow.com/questions/47535120/read-input-txt-file-and-also-output-bmp-file-from-terminal-c-programming) – Yunnosch Jul 02 '20 at 13:14
  • 3
    If you accept input from `stdin`, as would be the case of input from the shell, you do not have control of the input, which may or may not even be a file (e.g., `echo Hi | ./my_program`). If you must know the input file, then require it as a param and don't use `stdin`. If knowing the file allows for certain optimizations or features, you could support both an input file and `stdin` and default to reading `stdin` (and not using those optimizations/features) only when there is no input file given as an argument. – metal Jul 02 '20 at 13:14

2 Answers2

10

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
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • 2
    To be pedantic, there is a way, it's just a bad idea. You can jump through hoops to get the file name, but you shouldn't. – John Kugelman Jul 02 '20 at 13:34
2

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)

P.P
  • 117,907
  • 20
  • 175
  • 238
  • You also need to check if the file descriptor refers to a regular file with `fstat()`. It could be a pipe, a tty, or even a socket. – Andrew Henle Jul 02 '20 at 13:28
  • Indeed. If the input could be different (than the file direction) then `fstat(2)` would help differentiate. – P.P Jul 02 '20 at 13:32