SLIDE 13 13
Reading from a file
#include <fstream> #include <iostream> #include <cstdlib> int main(int argc, char** argv) { std::ifstream inStream; inStream.open("input.txt"); if( inStream.fail()) { std::cout << "Input file opening failed.\n"; exit(1); } int first, second, third; inStream >> first >> second >> third; std::cout << first << " " << second << " " << third << std::endl; inStream.close(); }
25
Reading from a file
int main(int argc, char** argv) { std::ifstream inStream; inStream.open("input.txt"); if( inStream.fail()) { std::cout << "Input file opening failed.\n"; exit(1); } int first, second, third; inStream >> first >> second >> third; std::cout << first << " " << second << " " << third << std::endl; inStream.close(); }
26
- input.txt now associated with inStream.
- Note: open() only accepts C-style
strings and not string objects. Use c_str() on a string object to get the C- style string