using "cin" for input from file?

Started by
18 comments, last by Chaucer 21 years, 6 months ago
Can the "cin" command from iostream be used to get input from a file? If so, how can it be used in such a way?
Thanks!
Advertisement
íts more like
ifstream file("filename");
file>>buffer;

not using cin but the fstream-object.

  #include <fstream> // NEVER <fstream.h> !!!!#include <iostream> // NEVER <iostream.h> !!!!using namespace std;int main(){   ifstream ifs( "input.txt" );   int i;   ifs >> i;   cout << i;};  


Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
What if I wanted to overload the cin function to read from a file rather than from the terminal?
Thanks!
Why would you want to? Why not use fstream?
cin isn''t a function anyway. Its a static instance of a istream(I think its istream...Correct me if I am wrong).
cin is NOT a function. It is a global variable.

If you want to redirect it, you need to use the rdbuf member function :


    #include <fstream> // NEVER <iostream.h> !!!!#include <iostream> // NEVER <iostream.h> !!!!#include <streambuf>using namespace std;int main(){  ifstream ifs( "input.txt" );  // You MUST save the stdin buffer  // and restore it at the end of the program.  streambuf* cin_buffer = cin.rdbuf();  // Assign ifs's ifstreambuf to cin  // You could have used any other istreambuf,  // user-defined or whatever.  cin.rdbuf( ifs.rdbuf() );  // Now cin points to "input.txt"    int read_from_file;  cin >> read_from_file;  // Must restore, otherwise cin won't have  // a valid stream to close after ifs is  // destroyed.  cin.rdbuf( cin_buffer );}    


Watch out what you're doing though.

Note that this wont't work with the old iostream (.h) library.

edit: was writing to an istream ... how embarassing.

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]


[edited by - Fruny on October 2, 2002 11:09:51 AM]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Sorry, I didn''t mean to call cin a function.

Lets just say someone else wrote a bunch of code using cin and I''d like to be able to change the usage of cin to read from a file instead of changing all of the code.
Thanks!
Redirect it by changing the underlying stream buffer as I''ve shown you. And scream after the code''s author for not writing reusably.

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
or!

do something like this (from the command prompt):
>type filename | program_that_uses_cin 


on *nix it would be:
>cat filename | program_that_uses_cin 


type/cat outputs the contents of the file to stdout. What the | thingie does is pipe the left programs stdout (cout) to the right programs stdin (cin)
daerid@gmail.com

This topic is closed to new replies.

Advertisement