Connecting Tech Pros Worldwide Forums | Help | Site Map

cin after reditected file?

skinnay@gmail.com
Guest
 
Posts: n/a
#1: Jul 23 '05
i have a program that has two parts, the first part populates a list
with a loop and cin.getline(). the second part is a command prompt to
manipulate the list. i have the first part done, and can use a
redirected file as input. (./main < data.txt) but after the first loop
i want it to drop to a command prompt. is this possible?

the program works fine without redirecting a file, but cin will not
wait for user input in the second loop when redirecting a file.


main()
{
// 1st loop
while(!end of file flag)
{
cin.getline(str);
list.append(str);
}


// command promt loop
while(!exit)
{
cout << "$ " ;

// this will not wait for input when supplied with
// redirected file :(
cin >> buf;

...
/* handle commands - manipulate list */
...

}

}

thanks :)


Dietmar Kuehl
Guest
 
Posts: n/a
#2: Jul 23 '05

re: cin after reditected file?


skinnay@gmail.com wrote:[color=blue]
> i have a program that has two parts, the first part populates a list
> with a loop and cin.getline(). the second part is a command prompt to
> manipulate the list. i have the first part done, and can use a
> redirected file as input. (./main < data.txt) but after the first[/color]
loop[color=blue]
> i want it to drop to a command prompt. is this possible?[/color]

No, it is not: there is only one input stream and once you redirected
it, it is, well, redirected. On some platforms you can explicitly
open the input channel which is normally connected with standard input
(e.g. on POSIX systems /dev/tty should do the trick) but this is
platform specific. The easier and portable approach would be the use
of an input stream ('std::ifstream') for the first part and a stadard
input stream which is not redirected as command prompt.
--
<mailto:dietmar_kuehl@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting

Closed Thread