Advertisement
helenrut

Untitled

Sep 11th, 2015
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cstring>
  4. #include <cstdlib> //Fyrir exit (1).
  5.  
  6. using namespace std;
  7.  
  8.  
  9. //Fall sem opnar skránna
  10. void OpenFile(ifstream& infile);
  11. //Fall sem telur oðrin og skilar okkur heiltölu
  12. int count_words (ifstream& infile);
  13. //Skila fjölda tákna í heiltölu.
  14. int symbols(ifstream& infile);
  15. //Fall sem skilar línufjölda í heiltölu.
  16. int count_lines (ifstream& infile);
  17. //Prentar út niðurstöður og fjölda lína, tákan og orða.
  18. void print_out(ofstream& outfile);
  19.  
  20. string thefilename;
  21. int counter3;
  22. int counter1;
  23. int counter2;
  24.  
  25.  
  26. int main()
  27. {
  28.  
  29. ifstream infile;
  30. ofstream outfile;
  31.  
  32. cout << "Write the name of the file: ";
  33. cin >> thefilename;
  34.  
  35.  
  36.  
  37. OpenFile(infile);
  38. counter3 = count_lines(infile);
  39. infile.close();
  40.  
  41. OpenFile(infile);
  42. counter1 = count_words(infile);
  43. infile.close();
  44.  
  45. OpenFile(infile);
  46. counter2 = symbols(infile);
  47. infile.close();
  48.  
  49.  
  50. print_out(outfile);
  51.  
  52.  
  53.  
  54. return 0;
  55. }
  56.  
  57.  
  58. //opnar skránna, ef ekki tekst að opna fer fallið í exit og lokar terminal glugganum.
  59. void OpenFile(ifstream& infile){
  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.  
  70. int count_words (ifstream& infile)
  71. {
  72. string words_counter;
  73. int counter1 = 0;
  74. while (infile >> words_counter){
  75. counter1 ++ ;
  76. }
  77. return counter1;
  78.  
  79. }
  80.  
  81. int count_lines (ifstream& infile)
  82. {
  83. char y;
  84. int counter3 = 0 ;
  85.  
  86. while (infile.get(y)){
  87. if (y == '\n'){
  88. counter3 ++ ;
  89. }
  90.  
  91. }
  92. if (y != '\n'){
  93. counter3 ++ ;
  94. }
  95.  
  96. return counter3;
  97.  
  98. }
  99.  
  100. int symbols(ifstream& infile)
  101. {
  102. char x ;
  103.  
  104. int counter2 = 0;
  105. infile.get(x);
  106.  
  107. while (!infile.eof()) {
  108.  
  109. infile.get(x);
  110. counter2 ++ ;
  111.  
  112. }
  113. return counter2;
  114. }
  115.  
  116.  
  117.  
  118.  
  119.  
  120. void print_out(ofstream& outfile ){
  121.  
  122. cout << "Lines: " << counter3 << endl;
  123. cout << "Words: " << counter1 << endl ;
  124. cout << "Chars: " << counter2 << endl;
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement