$cc a.c
$./a.out < inpfilename
I want to print inpfilename on stdout. How do I do that ? Thanks for the help in advance...
$cc a.c
$./a.out < inpfilename
I want to print inpfilename on stdout. How do I do that ? Thanks for the help in advance...
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:
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.
I don't think it's possible, since
<
just reads the contents ofinpfilename
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.
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