using "cin" for input from file?

Started by
18 comments, last by Chaucer 21 years, 6 months ago
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?
Thanks!
Advertisement
You want ifstream for reading files.

Helpful links:
How To Ask Questions The Smart Way | Google can help with your question | Search MSDN for help with standard C or Windows functions
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.
Thanks!
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!
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-terminatedfloat v0, v1, v2, v3;ss >> v0 >> v1 >> v2 >> v3; 
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...
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]
Thanks!
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 ]
"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
redirecting input may be what you want to do - daerid mentioned this.

cin reads from Standard In, which is redirectable - it CAN read from a file.
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
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]
"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

This topic is closed to new replies.

Advertisement