English 中文(简体)
In C how do I print filename of file that is redirected as input in shell
原标题:
$cc a.c
$./a.out < inpfilename

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

最佳回答

You can t get the filename exactly as input; the shell will handle all that redirection stuff without telling you.

In the case of a direct < file redirection, you can retrieve a filepath associated with stdin by using fstat to get an inode number for it then walking the file hierarchy similarly to find / -inum to get a path that matches it. (There might be more than one such filepath due to links.)

But you shouldn t ever need to do this. As others have said, if you need to know filenames you should be taking filenames as arguments.

问题回答

Why do you want to do this? All your program a.out is passed from the shell, is an open file descriptor, stdin.

The user might as well do this:

cat inpfilename | ./a.out

and now you have absolutely no filename to use (except /dev/stdin).

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

Only the parent shell is going to know that. The program, a.out is always going to see it as stdin.

In fact, it is possible to get filename from procfs, since /proc/*/fd contains symlink to opened files:

char filename[bufsize];
int sz = readlink("/proc/self/fd/0", filename, bufsize-1);
filename[sz] = 0;
puts(filename);

Your operating system will supply your program with input from this file. This is transparent to your program, and as such you don t get to see the name of the file. In fact, under some circumstances you will be fed input which doesn t come from a file, such as this:

ls | ./a.out

What you re after is very system-specific. Probably a better solution is to pass the filename as a parameter. That way you get the filename, and you can open it to read the content.

As you put it the process that runs a.out has no notion of the file name of the file that provides its standard input.

The invocation should be:

$ ./a.out inputfilename

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

or

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

And get the filename from stdin.

Then in a.c you need to fopen that file to read it s content.

I don t think it s possible, since < just reads the contents of inpfilename to STDIN.

If you want inpfilename to be available to your program, but you also want to be able to accept data from STDIN, set up your program to accept a filename argument and fopen that to a FILE. If no argument is given assign STDIN to your FILE. Then your input reading routine uses functions like fscanf rather than scanf, and the FILE that you pass in is either a link to the fopened file or STDIN.

An fstat(0,sb) (0 is stdin file descriptor) will give you details on the input file, size, permissions (called mode) and inode of the device it resides on.

Anyway you won t be able to tell its path: as unix inodes have no idea what path they belong to, and technically (see ln) they could belong to more than one path.

G day,

As pointed out in the above answer all you see is the open file descriptor stdin.

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

HTH





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签