Hey all,
In Xcode, do you have to put the text file somewhere special for it to be read? My file opening code runs but is always .fail().
Also how can I make an ifstream from cin?
Thanks for any help
Hey all,
In Xcode, do you have to put the text file somewhere special for it to be read? My file opening code runs but is always .fail().
Also how can I make an ifstream from cin?
Thanks for any help
I don't know about Xcode. Check to see where the executable file is being placed. Some (most) IDEs run the compiled program in a different environment than that in which the executable file is, but if you can find the file, you can run it wherever you like.
As for your second question, you can't make an ifstream from cin because ifstream is derived from istream, which is what cin is. I think it is more likely that you mean something else entirely, so please restate your question.
"If you tell the truth, you don't have to remember anything"
-Mark Twain
Well if there is no file specified in argv[] I want to read from std input instead. How can that be implemented?
Exactly what is your command that you run?
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Ok the program is a search program which searches a file for the first command argument, then prints out any line with that in it. If there's only one argument, it uses cin.
salami testdata.txt
is the argument line I'm trying to do now, and I've no idea how I would implement it if it was only
salami
thanks for looking
Use a function which takes a reference to an ifstream, then choose where that ifstream comes from.Code:void myInputFunction ( ifstream &in ); int main ( int argc, char *argv ) { if ( argc < 2 ) { myInputFunction( cin ); } else { ifstream myfile(argv[1]); myInputFunction( myfile ); } }
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Also, you can use istream::read(char*, size_t) and get the length of the read from istream::gcount( ).
Code:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }
Alright so cin is a reference to an ifstream? Would I have to open the file before I pass it to myInputFunction? because if the open command was in myInputFunction, would that work with cin?
>> Alright so cin is a reference to an ifstream?
I didn't spot that earlier, but no, cin is actually an instance of an object that is derived from an istream (not ifstream).
>> Would I have to open the file before I pass it to myInputFunction?
Yes. Just be sure to change the parameter to a reference to an istream, of course.
>> because if the open command was in myInputFunction, would that work with cin?
Nope.
Code:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }