Archived

This topic is now archived and is closed to further replies.

Chaucer

using "cin" for input from file?

Recommended Posts

Fruny    1658

  
#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 ]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
cin isn''t a function anyway. Its a static instance of a istream(I think its istream...Correct me if I am wrong).

Share this post


Link to post
Share on other sites
Fruny    1658
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]

Share this post


Link to post
Share on other sites
Chaucer    122
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.

Share this post


Link to post
Share on other sites
daerid    354
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)

Share this post


Link to post
Share on other sites
Chaucer    122
hmm is there any way to change an istream object to an ifstream object?

cin is the istream object so if I could change it to ifstream, then I could easily read the file.

Share this post


Link to post
Share on other sites
Taulin    100
Speaking of which,
Is there a way to set a stream object with NULL terminated string?

Like...

  
char testIn[] = "3.2\n1.2, 3.4, 5.6\0";

<some stream type???> newIn( testIn );

float version = 0.0f;

newIn >> version;


Thanks!

Share this post


Link to post
Share on other sites
Oluseyi    2112
quote:
Original post by Chaucer
hmm is there any way to change an istream object to an ifstream object?

You can''t change an object from one type to another. Since an ifstream is an istream, you can make an istream "point at" an ifstream by manipulating it''s stream buffer. This has been stated in this thread.

quote:

cin is the istream object so if I could change it to ifstream, then I could easily read the file.

You can''t. Get over it.

Taulin:
You can fill a stream object''s stream buffer with the contents of a null-terminated string. In fact, that''s what the insertion operator does. The solution to what you want to do (convert text to floating-point number), however, is to use a stringstream:

#include <sstream>
...
 
std::stringstream ss;
ss << "3.2\n1.2, 3.4, 5.6"; // string literals are automatically null-terminated
float v0, v1, v2, v3;
ss >> v0 >> v1 >> v2 >> v3;

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
just fix the masses of code that are using cin when they shouldn''t be. Ok if your boss says not to then yeah do whatever, but I suspect that this is just a personal project. If you are going to copy code (either yours or someone elses) you should at least tidy it up. Kids these days... I remember when comps were made out of straw and all it would take was a huffing and puffing wolf...

Share this post


Link to post
Share on other sites
Chaucer    122
error C2660: 'rdbuf' : function does not take 1 parameters

edit:


  

class myClass
{
public:
ifstream in("file.txt");
myClass();
friend istream & operator >>(istream &is, char &a)
{

streambuf* cin_buffer = is.rdbuf();
is.rdbuf(in.rdbuf());
is.rdbuf(cin_buffer);
};
};


[edited by - Chaucer on October 2, 2002 10:15:55 PM]

Share this post


Link to post
Share on other sites
Fruny    1658
quote:
Original post by Chaucer
hmmm, can istream open files? What if all I have is a reference to an istream object. How would I use that to open a file?



I gave you the code.

quote:
Original post by Chaucer
error C2660: ''rdbuf'' : function does not take 1 parameters



Make sure you''re using <iostream> and <fstream> and not <iostream.h> and <fstream.h>.


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

Share this post


Link to post
Share on other sites
Fruny    1658
quote:
Original post by Magmai Kai Holmlor
cin reads from Standard In, which is redirectable - it CAN read from a file.

Hmm... is cin directly using stdin/fd-0 or does it hold a duplicate ?
i.e. do you think redirecting stdin after cin is created would work ?

Have to test ...

edit: seems to work with cout and a freopen() on stdout.

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:15:33 PM]

Share this post


Link to post
Share on other sites