please explain istream in simple terms

closed account (ypfz3TCk)
Can somebody please explain
a) what istream is and
b) example how it is used?

I understand istream is for console input - but why? what does this do that cin does not?

if i do: cin >> some_text - why should i want to do istream >> some_text? I am confused because the books that i read either dont explain this very well or skip it altogether (C++ primer, you can program in c++, accelerated c++).

if istream is a class why wont this compile?

#include <iostream>


using namespace std;

int main()
{
istream << myfile;
return 0;
}
istream is a class that represents and input stream. You can read values (ints, strings, your own classes) from these input streams.

cin is a specific istream object that represents standard input to a program. Standard input (stdin in C) is frequently console input, but it can also be piped input from programs running in batch mode or other streams, depending on the context in which the program is running.

Other frequently-used istream subclasses include ifstream (for reading from a file) and istringstream (for reading from a string in memory).

Your example doesn't make sense because istream is not an object, you are trying to use the insertion operator rather than an extraction operator on an istream, and myfile is undefined.

If you are trying to read from a file, you would use something like this:

1
2
3
4
5
6
7
8
9
#include <fstream>
using std::ifstream

int main()
{
    ifstream fileStream("myFile");
    int a;
    fileStream >> a;
}


This will read an integer from the file "myfile".
closed account (ypfz3TCk)
Thanks doug - i have to admit that i was surprised to find that this area is so complex -presumably that's why a lot of introductory books don't go into too much depth. I think this is one of those areas that will make more sense to me once i have studied classes in my book!
Topic archived. No new replies allowed.