0

I have several arguments to my program and an input file being redirected. i am parsing the arguments (using argc and argv) and the file (using cin). However, I am not able to display the name of the redirected file. For instance, the arguments are-

./procsim -r 2 -f 4 -j 3 -k 2 -l 1 < gcc.100k.trace

and my code to parse the arguments is -

for (int i = 1; i < argc; ++i)
    {
    if (argv[i][0] == '-')
        {
        if (!strcmp(argv[i], "-r"))
        {
            if (i < argc - 1)
            {
                pipeline_parameters[count++] = atoi(argv[i+1]);
                R = atoi(argv[i+1]);
                i = i+1;
            }
        }

        else if (!strcmp(argv[i], "-f"))
        {
            if (i < argc - 1)
            {
                pipeline_parameters[count++] = atoi(argv[i+1]);
                F = atoi(argv[i+1]);
                i = i+1;
            }
        }

and to parse the file, I use cin -

 string line;

while (getline( cin, line))
{
address_arr.push_back(line);
}

I want to read the fie name, which is gcc.100k.trace here. how do i do that? P.S. - I have tried parsing through the arguments, but I'm not able to find it!

Edit: address_arr is a vector of int. pipeline_parameters is an array of int of length 5, to store the arguments. R and F are global variables which store the arguments as well.

2
  • Welcome to stackoverflow! Please try to create a Minimal, Complete, and Verifiable example as there is information missing from your example, e.g., what is pipeline_parameters or address_arr? Commented Mar 18, 2017 at 5:56
  • 2
    I don't think you can retrieve the name of the file whose contents were redirected to stdin. Take a look at this answer to another SO post. It may lead you to find an answer to your question. Commented Mar 18, 2017 at 6:11

1 Answer 1

0

You certainly cannot do it using argc and argv. If there is any hope at all, it may be to call getppid() to get your shell's PID, then read /proc/PPID/fd or proc/PPID/fdinfo and see what files are open there.

This will be a huge nasty hack, and has no place in a production application. There's probably not a good enough reason why you'd need to do this, and no other program in the wild does it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.