Advertisement
helenrut

Untitled

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