Advertisement
helenrut

Untitled

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