creating a stream object with stdin

Nov 29, 2011 at 5:41pm
Hello all. I am stuck on a project I am working on, and need some help. I'm still kind of a beginner with C++, but it seemed like this topic wouldn't be good for the beginners forum.
I need to get some input from the stdin, but I don't know how to create a stream object and give it stdin. Any suggestions?
Nov 29, 2011 at 9:40pm
std::cin and std::wcin are the stream objects associated with stdin.

They already exist when the program starts up, there is no need to create them at run time, but if you must, std::istream mycin(std::cin.rdbuf());

What is this for?
Nov 29, 2011 at 10:36pm
It is a project for a class where we are learning to program in Unix/Linux. The goal is to simulate the process scheduler, using a few of the different algorithms like round robin, fcfs, ect. We get information about the processes from the user from stdin by either directing a file to stdin, or letting the user enter information. for example, at run-time I would enter this at the command line:
./project8 < in.txt
or else just use stdin to let the user enter the necessary info manually.

So, if I'm understanding correctly, I don't need to create a stream object at all, I can just say something like this:
1
2
3
4
while (!(EOF))
{
        cin >> array[index];
}


Nov 29, 2011 at 10:37pm
Yeah. By piping the text file in like that, cin will read from that file.
Nov 29, 2011 at 10:50pm
Or you could just do

1
2
while(cin >> array[index])
// ... 

If the input is a file, it will read until the end. If it's the console, it will read until a EOF (Ctrl-D).
Nov 29, 2011 at 10:54pm
Thanks guys, you were a huge help!
Topic archived. No new replies allowed.