Advertisement
helenrut

Untitled

Sep 11th, 2015
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 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.  
  10. void OpenFile(ifstream& infile);
  11. int count_words (ifstream& infile);
  12. int symbols(ifstream& infile);
  13. int count_lines (ifstream& infile);
  14. void print_out(ofstream& outfile);
  15.  
  16. string thefilename;
  17. int counter3;
  18. int counter1;
  19. int counter2;
  20.  
  21.  
  22. int main()
  23. {
  24.  
  25.  
  26.  
  27. ifstream infile;
  28. ofstream outfile; // ("svor.txt", std::ofstream::out);
  29.  
  30. cout << "Write the name of the file: ";
  31. cin >> thefilename;
  32. //OpenFile(infile, thefilename);
  33.  
  34.  
  35. OpenFile(infile);
  36. counter3 = count_lines(infile);
  37. infile.close();
  38.  
  39. OpenFile(infile);
  40. counter1 = count_words(infile);
  41. infile.close();
  42.  
  43. OpenFile(infile);
  44. counter2 = symbols(infile);
  45. infile.close();
  46. //int count_lines (ifstream& thefilename);
  47. //int counter3 ;
  48. //cout << "Lines: " << counter3 << endl;
  49.  
  50.  
  51. //int count_words (ifstream& thefilename);
  52. //int counter1 ;
  53. //cout << "Words : " << counter1 << endl ;
  54.  
  55.  
  56. //int symbols (ifstream& thefilename);
  57. //int counter2 ;
  58. //cout << "Chars : " << counter2 << endl;
  59.  
  60. print_out(outfile);
  61.  
  62.  
  63.  
  64. return 0;
  65. }
  66.  
  67.  
  68.  
  69. void OpenFile(ifstream& infile){
  70. infile.open(thefilename.c_str());
  71.  
  72. if(infile.fail() ) {
  73. cout << "Can't open file: " << thefilename << endl;
  74. exit(1);
  75. }
  76. }
  77.  
  78.  
  79.  
  80. int count_words (ifstream& infile)
  81. {
  82. string words_counter;
  83. int counter1 = 0;
  84. while (infile >> words_counter){
  85. counter1 ++ ;
  86. }
  87. return counter1;
  88.  
  89. }
  90.  
  91. int count_lines (ifstream& infile)
  92. {
  93. char y;
  94. int counter3 = 0 ;
  95.  
  96. while (infile.get(y)){
  97. if (y == '\n'){
  98. counter3 ++ ;
  99. }
  100.  
  101. }
  102. if (y != '\n'){
  103. counter3 ++ ;
  104. }
  105.  
  106. return counter3;
  107.  
  108. }
  109.  
  110. int symbols(ifstream& infile)
  111. {
  112. char x ;
  113.  
  114. int counter2 = 0;
  115. infile.get(x);
  116.  
  117. while (!infile.eof()) {
  118.  
  119. infile.get(x);
  120. counter2 ++ ;
  121.  
  122. }
  123. return counter2;
  124. }
  125.  
  126.  
  127. void print_out (ifstream& infile ){
  128.  
  129.  
  130. cout << "Lines: " << counter3 << endl;
  131. cout << "Words: " << counter1 << endl ;
  132. cout << "Chars: " << counter2 << endl;
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement