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 <string>
- #include <cstdlib> //Fyrir exit (1).
- using namespace std;
- string myfile ;
- // Opnar skránna sem við ætlum að vinna með.
- void OpenFile(ifstream& infile, string thefilename);
- //Telur orðinni í skránni og skilar út fjölda í heiltölu.
- int count_words (ifstream& thefilename);
- //Telur öll táknin sem skráin inniheldur og skilar okkur út fjölda í heiltölu.
- int symbols(ifstream& thefilename);
- // Telur línufjölda í skránni og skilar út heiltölu sem táknar fjölda lína.
- int count_lines (ifstream& thefilename);
- int main()
- {
- string thefilename;
- 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_lines (ifstream& thefilename)
- {
- char (y);
- int counter3 = 0 ;
- while (thefilename.get(y)){
- if (y == '\n'){
- counter3 ++ ;
- }
- }
- if (y != '\n'){
- counter3 ++ ;
- }
- return counter3;
- }
- int count_words (ifstream& thefilename)
- {
- string words_counter;
- int counter1 = 0;
- while (thefilename >> words_counter)
- counter1 ++ ;
- return counter1;
- }
- int symbols(ifstream& thefilename)
- {
- char x ;
- int counter2 = 0;
- thefilename.get(x);
- while (!thefilename.eof()) {
- thefilename.get(x);
- counter2 + 1 ;
- }
- return counter2;
- }
- //void print_out (ifstream& thefilename)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement