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;
- //Fall sem opnar skránna
- void OpenFile(ifstream& infile);
- //Fall sem telur oðrin og skilar okkur heiltölu
- int count_words (ifstream& infile);
- //Skila fjölda tákna í heiltölu.
- int symbols(ifstream& infile);
- //Fall sem skilar línufjölda í heiltölu.
- int count_lines (ifstream& infile);
- //Prentar út niðurstöður og fjölda lína, tákan og orða.
- void print_out(ofstream& outfile);
- string thefilename;
- int counter3;
- int counter1;
- int counter2;
- int main()
- {
- ifstream infile;
- ofstream outfile;
- cout << "Write the name of the file: ";
- cin >> thefilename;
- OpenFile(infile);
- counter3 = count_lines(infile);
- infile.close();
- OpenFile(infile);
- counter1 = count_words(infile);
- infile.close();
- OpenFile(infile);
- counter2 = symbols(infile);
- infile.close();
- print_out(outfile);
- return 0;
- }
- //opnar skránna, ef ekki tekst að opna fer fallið í exit og lokar terminal glugganum.
- 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(ofstream& outfile ){
- cout << "Lines: " << counter3 << endl;
- cout << "Words: " << counter1 << endl ;
- cout << "Chars: " << counter2 << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement