Advertisement
helenrut

Untitled

Sep 11th, 2015
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. //Skilaverkefni 1
  2. //Haustönn 2015
  3. //Unnið af : Helen Rut Ástþórsdóttir
  4.  
  5. #include <iostream>
  6. #include <fstream>
  7. #include <cstring>
  8. #include <cstdlib> //Fyrir exit (1).
  9.  
  10. using namespace std;
  11.  
  12. string thefilename ;
  13. // Opnar skránna sem við ætlum að vinna með.
  14. void OpenFile(ifstream& infile, string thefilename);
  15.  
  16. //Telur orðinni í skránni og skilar út fjölda í heiltölu.
  17. int count_words (ifstream& thefilename);
  18.  
  19. //Telur öll táknin sem skráin inniheldur og skilar okkur út fjölda í heiltölu.
  20. int symbols(ifstream& thefilename);
  21.  
  22. // Telur línufjölda í skránni og skilar út heiltölu sem táknar fjölda lína.
  23. int count_lines (ifstream& thefilename);
  24.  
  25.  
  26.  
  27. int main()
  28. {
  29.  
  30.  
  31. ifstream infile;
  32.  
  33. cout << "Write the name of the file: ";
  34. cin >> thefilename;
  35. OpenFile(infile, thefilename);
  36.  
  37.  
  38. int count_lines (ifstream& thefilename);
  39. int counter3 ;
  40. cout << "Lines: " << counter3 << endl;
  41.  
  42.  
  43.  
  44.  
  45. int count_words (ifstream& thefilename);
  46. int counter1 ;
  47. cout << "Words : " << counter1 << endl ;
  48.  
  49. int symbols (ifstream& thefilename);
  50. int counter2 ;
  51. cout << "Chars : " << counter2 << endl;
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58. return 0;
  59. }
  60.  
  61.  
  62.  
  63. void OpenFile(ifstream& infile, string thefilename){
  64. infile.open(thefilename.c_str());
  65.  
  66. if(infile.fail() ) {
  67. cout << "Can't open file: " << thefilename << endl;
  68. exit(1);
  69. }
  70. }
  71.  
  72.  
  73.  
  74. int count_words (ifstream& thefilename)
  75. {
  76.  
  77. string words_counter;
  78. int counter1 = 0;
  79. while (thefilename >> words_counter)
  80. counter1 ++ ;
  81.  
  82. return counter1;
  83.  
  84. }
  85.  
  86. int symbols(ifstream& thefilename)
  87. {
  88. char x ;
  89.  
  90. int counter2 = 0;
  91. thefilename.get(x);
  92.  
  93. while (!thefilename.eof()) {
  94.  
  95. thefilename.get(x);
  96. counter2 + 1 ;
  97.  
  98. }
  99. return counter2;
  100. }
  101.  
  102.  
  103. //void print_out (ifstream& thefilename)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement