Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Skilaverkefni 1
- //Haustönn 2015
- //Unnið af : Helen Rut Ástþórsdóttir
- #include <iostream>
- #include <fstream>
- #include <cstring>
- #include <cstdlib> //Fyrir exit (1).
- using namespace std;
- void OpenFile(ifstream& infile, string thefilename);
- int count_words (ifstream& thefilename);
- int symbols(ifstream& thefilename);
- int count_lines (ifstream& thefilename);
- string thefilename ;
- int main()
- {
- ifstream infile;
- cout << "Write the name of the file: ";
- cin >> thefilename;
- OpenFile(infile, thefilename);
- 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;
- return 0;
- }
- void OpenFile(ifstream& infile, string thefilename){
- 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& thefilename)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement