Advertisement
helenrut

Untitled

Sep 11th, 2015
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.14 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 <string>
  8. #include <cstdlib> //Fyrir exit (1).
  9.  
  10. using namespace std;
  11.  
  12. string myfile ;
  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. string thefilename;
  31. ifstream infile;
  32.  
  33. cout << "Write the name of the file: ";
  34. cin >> thefilename;
  35. OpenFile(infile, thefilename);
  36.  
  37. int count_lines (ifstream& thefilename);
  38. int counter3 ;
  39. cout << "Lines: " << counter3 << endl;
  40.  
  41. int count_words (ifstream& thefilename);
  42. int counter1 ;
  43. cout << "Words : " << counter1 << endl ;
  44.  
  45. int symbols (ifstream& thefilename);
  46. int counter2 ;
  47. cout << "Chars : " << counter2 << endl;
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54. return 0;
  55. }
  56.  
  57.  
  58.  
  59. void OpenFile(ifstream& infile, string thefilename){
  60. infile.open(thefilename.c_str());
  61.  
  62. if(infile.fail() ) {
  63. cout << "Can't open file: " << thefilename << endl;
  64. exit(1);
  65. }
  66. }
  67.  
  68.  
  69. int count_lines (ifstream& thefilename)
  70. {
  71. char (y);
  72. int counter3 = 0 ;
  73.  
  74. while (thefilename.get(y)){
  75. if (y == '\n'){
  76. counter3 ++ ;
  77. }
  78.  
  79. }
  80. if (y != '\n'){
  81. counter3 ++ ;
  82. }
  83.  
  84. return counter3;
  85.  
  86. }
  87.  
  88. int count_words (ifstream& thefilename)
  89. {
  90.  
  91. string words_counter;
  92. int counter1 = 0;
  93. while (thefilename >> words_counter)
  94. counter1 ++ ;
  95.  
  96. return counter1;
  97.  
  98. }
  99.  
  100. int symbols(ifstream& thefilename)
  101. {
  102. char x ;
  103.  
  104. int counter2 = 0;
  105. thefilename.get(x);
  106.  
  107. while (!thefilename.eof()) {
  108.  
  109. thefilename.get(x);
  110. counter2 + 1 ;
  111.  
  112. }
  113. return counter2;
  114. }
  115.  
  116.  
  117. //void print_out (ifstream& thefilename)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement