/* * File: lecture.cpp * ------------------ * Snippets from the live coding part of Wed 1/16 lecture * showing some simple use of fstream. */ #include "genlib.h" #include <iostream> #include <fstream> #include "simpio.h" int CountLines(ifstream &in) { int count = 0; while (true) { string line; getline(in, line); if (in.fail()) break; count++; } return count; } int main() { ifstream in; while (true) { cout << "Enter name: "; string s = GetLine(); in.open(s.c_str()); if (!in.fail()) break; cout << "Couldn't open file, try again!" << endl; in.clear(); } cout << "Num lines = " << CountLines(in) << endl; return 0; }
m_r_k