Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <sstream>
- int countLines(const std::string& filename) {
- std::ifstream file(filename);
- int lines = 0;
- std::string line;
- while (std::getline(file, line)) {
- ++lines;
- }
- return lines;
- }
- int countWords(const std::string& filename) {
- std::ifstream file(filename);
- int words = 0;
- std::string word;
- while (file >> word) {
- ++words;
- }
- return words;
- }
- int countCharacters(const std::string& filename) {
- std::ifstream file(filename);
- int characters = 0;
- char ch;
- while (file.get(ch)) {
- ++characters;
- }
- return characters;
- }
- void printResults(int lines, int words, int characters) {
- std::cout << "Lines: " << lines << "\n";
- std::cout << "Words: " << words << "\n";
- std::cout << "Characters: " << characters << "\n";
- }
- int main() {
- std::string filename = "example.txt";
- int lines = countLines(filename);
- int words = countWords(filename);
- int characters = countCharacters(filename);
- printResults(lines, words, characters);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement