In C, how do I print the name of a file redirected as a shell entry

$cc ac $./a.out < inpfilename 

I want to print inpfilename on stdout. How can I do it? Thanks for the help in advance ...

+7
c command-line shell printf io-redirection
source share
8 answers

You cannot get a file name in the same way as input; the shell will process all the redirect data without telling you.

In the case of direct redirection of < file you can get the file path associated with stdin with fstat to get the inode number for it, going through the file hierarchy like find / -inum to get the path that matches This. (This may be several of these file paths due to links.)

But you will never need to do this. As others have said, if you need to know the file names, you should accept the file names as arguments.

+11
source share

Why would you want to do that? Your whole a.out program is passed from the shell, it is an open file descriptor, stdin.

The user can also do this:

 cat inpfilename | ./a.out 

and now you have absolutely no file name to use (other than / dev / stdin).

If a.out needs to work with file names, why not take the file as a command line argument?

+4
source share

Only the parent shell knows about it. A program, a.out will always consider it as stdin.

+2
source share

Your operating system will provide your program with input from this file. This is transparent to your program, and therefore you cannot see the file name. In fact, in some circumstances, you will be given an input that does not come from a file, for example:

 ls | ./a.out 

What you need is very system dependent. Probably the best solution is to pass the file name as a parameter. This way you get the file name and you can open it to read the contents.

+1
source share

As you expressed, the process executing a.out does not have the concept of a file name that provides its “standard input”.

The call should be:

 $ ./a.out inputfilename 

and parse argv into int main( int argc, char* argv[] ) { ... }

or

 $ ./a.out <<< "inputfilename" 

And get the file name from stdin.

Then in ac you need to fopen this file to read its contents.

0
source share

I do not think this is possible, since < just reads the contents of inpfilename in STDIN.

If you want inpfilename to be available to your program, but you also want to be able to accept data from STDIN, configure your program to accept the file name argument and fopen for FILE. If no argument is given, assign STDIN to your file. Then your input reader uses functions like fscanf, not scanf, and the FILE you pass in is either a link to the fopened file or STDIN.

0
source share

An fstat(0,sb) (0 is the descriptor of the stdin file) will provide you with information about the input file, size, permissions (called mode) and the inode of the device on which it is located.

In any case, you cannot tell your path: since unix inodes have no idea which path they belong to, and technically (see ln ) they can belong to several paths.

0
source share

G'day

As indicated in the answer above, all you see is the handle to the open stdin file.

If you really want to do this, you can specify that the first line of the input file should be the name of the file itself.

NTN

0
source share

All Articles