Input redirection

Sep 5, 2014 at 3:53am
Hello everybody.
I need help with input redirection. I'm using it to read in a file full of
integer values which I'm putting into array. After sorting it , I'm prompting
a user to enter a value to search for in the array but my program skips
the scanf command. My guess this happens because this input redirection
thingy turns on once I typed in '<' at the prompt and it remains on the whole time.
If it is in fact so, how do I turn it off?
Sep 5, 2014 at 4:12am
Input redirection isn't an on or off thing. Your program has one standard input stream.

Instead of using input redirection, prompt the user for the file name and read the file yourself.
Sep 5, 2014 at 4:16am
I have to use it.
Any idea why a 'scanf' could be skipped?
Sep 5, 2014 at 4:17am
It's not getting skipped, your theory was correct - it is reading from the file.

Why do you have to use input redirection? There is no way to do what you want.
Last edited on Sep 5, 2014 at 4:18am
Sep 5, 2014 at 4:37am
Dang!
My Proff wants me to read in a file using input redirection,
put the values in the array, copy it, sort one of the copies and than use
linear and binary searches on both copies of the array and print out the
position and number of comparisons it took to find a value in all four cases.
The value to search for should too be obtained from user using standard input.
Sep 5, 2014 at 4:39am
Can't I 'close' the file somehow?
I will probably need to look argument vector for that, right?
Sep 5, 2014 at 4:39am
You should contact your professor - maybe there is a misunderstanding? It is not possible to change the standard input while the program is running. Once a program starts, whatever it is inputting from is what it will always be inputting from.

You can, however, pass the number to search for as an argument:

myprogram.exe 42 < input.txt
1
2
3
4
int main(int num_arguments, char const *const *arguments)
{
    //Note: the first argument (argument 0) is always the name of your program file
}


EDIT: If you close the file, that's closing the standard input stream. AKA, no more input, ever.
Last edited on Sep 5, 2014 at 4:41am
Sep 5, 2014 at 4:59am
I got the idea. Thank you.
Just not completely clear what is going on in this line
 
int main(int num_arguments, char const *const *arguments)


char const *const *arguments in particular.

why const* twice?
Last edited on Sep 5, 2014 at 5:00am
Sep 5, 2014 at 5:12am
The const keyword affects the thing to its left.

The first const means you cannot change the individual characters.

The second const means you cannot change the pointers to the characters.

You can change the pointer to the pointers to the characters. Adding a third const would mean you could not:

char const *const *const arguments
Last edited on Sep 5, 2014 at 5:13am
Sep 5, 2014 at 5:15am
Thank you.

:)
Topic archived. No new replies allowed.