Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <iostream>
- #include <cstdlib>
- #include <string>
- int main()
- {
- char file_name[16];
- std::ifstream in_stream;
- int count = 0;
- char ch;
- bool lastWasWhite = true;
- std::string next;
- // Ask the user for the file name
- std::cout << "What is the name of the file? ";
- std::cin >> file_name;
- in_stream.open(file_name);
- // See if we can open the file
- if (!in_stream.is_open()) {
- std::cout << "Something went wrong when opening your file.";
- exit(1);
- }
- // Read the file, one character at a time
- while (in_stream >> next) {
- do {
- std::cin.get(ch);
- }
- while (ch != ' ' && ch != '\n' && ch != '\t');
- ++count;
- }
- /* program works mostly properly. It only completes if you use CTRL+C to end the terminal. I cannot find a way to
- quit the program before that, so I think it loops forever. */
- in_stream.close();
- std::cout << "The file " << file_name << " contains " << count << " words." << std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement