cin after reditected file?

S

skinnay

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 :)
 
D

Dietmar Kuehl

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?

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,055
Members
47,659
Latest member
salragu

Latest Threads

Top