Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <cstring>
- #include <cstdlib> //Fyrir exit (1).
- using namespace std;
- void OpenFile(ifstream& infile);
- int count_words (ifstream& infile);
- int symbols(ifstream& infile);
- int count_lines (ifstream& infile);
- void print_out(ofstream& outfile);
- string thefilename;
- int counter3;
- int counter1;
- int counter2;
- int main()
- {
- ifstream infile;
- ofstream outfile; // ("svor.txt", std::ofstream::out);
- cout << "Write the name of the file: ";
- cin >> thefilename;
- //OpenFile(infile, thefilename);
- OpenFile(infile);
- counter3 = count_lines(infile);
- infile.close();
- OpenFile(infile);
- counter1 = count_words(infile);
- infile.close();
- OpenFile(infile);
- counter2 = symbols(infile);
- infile.close();
- //int count_lines (ifstream& thefilename);
- //int counter3 ;
- //cout << "Lines: " << counter3 << endl;
- //int count_words (ifstream& thefilename);
- //int counter1 ;
- //cout << "Words : " << counter1 << endl ;
- //int symbols (ifstream& thefilename);
- //int counter2 ;
- //cout << "Chars : " << counter2 << endl;
- print_out(outfile);
- return 0;
- }
- void OpenFile(ifstream& infile){
- infile.open(thefilename.c_str());
- if(infile.fail() ) {
- cout << "Can't open file: " << thefilename << endl;
- exit(1);
- }
- }
- int count_words (ifstream& infile)
- {
- string words_counter;
- int counter1 = 0;
- while (infile >> words_counter){
- counter1 ++ ;
- }
- return counter1;
- }
- int count_lines (ifstream& infile)
- {
- char y;
- int counter3 = 0 ;
- while (infile.get(y)){
- if (y == '\n'){
- counter3 ++ ;
- }
- }
- if (y != '\n'){
- counter3 ++ ;
- }
- return counter3;
- }
- int symbols(ifstream& infile)
- {
- char x ;
- int counter2 = 0;
- infile.get(x);
- while (!infile.eof()) {
- infile.get(x);
- counter2 ++ ;
- }
- return counter2;
- }
- void print_out (ifstream& infile ){
- cout << "Lines: " << counter3 << endl;
- cout << "Words: " << counter1 << endl ;
- cout << "Chars: " << counter2 << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement