Find the answer to your Linux question:
Results 1 to 4 of 4
Hello everyone, i have a problem in my program. The program is taking input from file i.e (./prog < input) program is taking input from "input" file. The problem is ...
  1. #1
    Just Joined!
    Join Date
    Mar 2010
    Posts
    4

    problem!!

    Hello everyone, i have a problem in my program. The program is taking input from file i.e (./prog < input) program is taking input from "input" file. The problem is that after doing this, i cant do any reading throughout the prgram. The program just neglects all the "cin>>" operators.Is there any way to close the input redirection so that i can take input according to my wish. Another problem is that i want to detect "ENTER" key, and this is also a problem to detect. Whats the procedure to detect it?.Thanks in advance

  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    When you start a program with input redirected from a file or other device, stdin/cin inside your program is no longer looking for input from the terminal, but is looking for input from the file or other device. If you have redirected from a file or other device, then you can look for end-of-file on that stream and reopen it on the tty your program was executed on. Then you can take input from the terminal again. I've done this in the past, but it isn't necessarily simple to do. You need to understand how all this stuff works first - I'm not going to give you a "cookbook" for it.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  3. #3
    Just Joined!
    Join Date
    Mar 2008
    Location
    Armidale, NSW, Australia
    Posts
    4
    I know this doesn't solve the actual problem presented (regarding input redirection), but rather than redirecting input could you not take the file name as a command line parameter instead and have the program read the file using an ifstream?

  4. #4
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    Quote Originally Posted by cospengle View Post
    I know this doesn't solve the actual problem presented (regarding input redirection), but rather than redirecting input could you not take the file name as a command line parameter instead and have the program read the file using an ifstream?
    This is actually the preferable method, as opposed to redirecting stdin. That eliminates the cruft of reopening stdin after the program has finished processing the file. My previous post was to show that it is possible to redirect stdin from a file, yet continue after processing it with normal keyboard input. This was necessary for an application I worked on in the past (about 20 years ago).
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...