Advertisement
helenrut

Untitled

Sep 11th, 2015
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 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 <string>
  8. #include <cstdlib> //Fyrir exit (1).
  9.  
  10. using namespace std;
  11.  
  12. string myfile ;
  13. // Opnar skránna sem við ætlum að vinna með.
  14. void OpenFile(ifstream& infile, string thefilename);
  15.  
  16. //Telur orðinni í skránni og skilar út fjölda í heiltölu.
  17. int count_words (ifstream& thefilename);
  18.  
  19. //Telur öll táknin sem skráin inniheldur og skilar okkur út fjölda í heiltölu.
  20. int symbols(ifstream& thefilename);
  21.  
  22. // Telur línufjölda í skránni og skilar út heiltölu sem táknar fjölda lína.
  23. int count_lines (ifstream& thefilename);
  24.  
  25.  
  26.  
  27. int main()
  28. {
  29.  
  30. string thefilename;
  31. ifstream infile;
  32.  
  33. cout << "Write the name of the file: ";
  34. cin >> thefilename;
  35. OpenFile(infile, thefilename);
  36.  
  37.  
  38. int count_words (ifstream& thefilename);
  39.  
  40. int symbols (ifstream& thefilename);
  41.  
  42. int count_lines (ifstream& thefilename);
  43.  
  44.  
  45.  
  46.  
  47. return 0;
  48. }
  49.  
  50.  
  51.  
  52. void OpenFile(ifstream& infile, string thefilename){
  53. infile.open(thefilename.c_str());
  54.  
  55. if(infile.fail() ) {
  56. cout << "Can't open file: " << thefilename << endl;
  57. exit(1);
  58. }
  59. }
  60.  
  61.  
  62. int count_words (ifstream& thefilename)
  63. {
  64.  
  65. string words_counter;
  66. int counter1 = 0;
  67. while (thefilename >> words_counter)
  68. counter1 ++ ;
  69.  
  70. return counter1;
  71.  
  72. }
  73.  
  74. int symbols(ifstream& thefilename)
  75. {
  76. char x ;
  77.  
  78. int counter2 = 0;
  79. thefilename.get(x);
  80.  
  81. while (!thefilename.eof()) {
  82.  
  83. thefilename.get(x);
  84. counter2 + 1 ;
  85.  
  86. }
  87. return counter2;
  88. }
  89.  
  90. int count_lines (ifstream& thefilename)
  91. {
  92. char (y);
  93. int counter3 = 0 ;
  94.  
  95. while (thefilename.get(y)){
  96. if (y == '\n'){
  97. counter3 ++ ;
  98. }
  99.  
  100. }
  101. if (y ! = '\n'){
  102. counter3 ++ ;
  103. }
  104.  
  105. return counter3;
  106.  
  107. }
  108.  
  109. //void print_out (ifstream& thefilename)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement