using "cin" for input from file?
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?
íts more like
ifstream file("filename");
file>>buffer;
not using cin but the fstream-object.
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 ]
What if I wanted to overload the cin function to read from a file rather than from the terminal?
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 :
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]
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]
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.
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.
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 ]
Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
or!
do something like this (from the command prompt):
on *nix it would be:
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)
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)
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement