Advertisement
helenrut

Untitled

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