Advertisement
helenrut

Untitled

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