/ Published in: C++
Use to get console input from the user for a file name (in the current directory) and read in from that file.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
#include <iostream> #include <fstream> #include <string> using namespace std; int main() { ifstream inputFile; string line; string input; cout << "Enter a file name to scan" << endl; cin >> input; cout << endl; cout << "Your file was: " << input << endl; try { inputFile.open(input.data(), ios::in); } catch (...) { cout << "File open error" << endl; } if ( !inputFile.is_open() ) { return -1; } while (! inputFile.eof() ) { getline(inputFile, line, ' '); cout << "__________ " << line << endl; } inputFile.close(); cin >> input; // Just used to keep the console open in Visual C++ return 0; }