Advertisement
Guest User

Untitled

a guest
Mar 1st, 2017
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <limits>
  4. using namespace std;
  5.  
  6. bool newl(ifstream & plik )
  7. {
  8. char cZnak;
  9. for(;; ) //nieskoñczona pêtla
  10. {
  11. plik.clear();
  12. cZnak = plik.peek(); //sprawdzamy jaki kolejny znak zostanie zwrócony przez operacjê odczytu
  13. if( plik.fail() || plik.bad() )
  14. return false; //wyst¹pi³ b³¹d odczytu danych
  15.  
  16. if( !isspace( cZnak ) )
  17. return false; //pobrany znak nie jest bia³ym znakiem
  18.  
  19. plik.get( cZnak ); //odczytujemy bia³y znak z pliku
  20. if( plik.fail() || plik.bad() )
  21. return false; //wyst¹pi³ b³¹d odczytu danych
  22.  
  23. if( cZnak == '\n' )
  24. return true;
  25.  
  26. } //for
  27. }
  28. bool numload(ifstream &file, int &num)
  29. {
  30. file.clear();
  31. file>>num;
  32. if (file.fail())
  33. return false;
  34. else return true;
  35. }
  36.  
  37. bool loadfile(string fname)
  38. {
  39. ifstream file;
  40. file.open(fname.c_str());
  41. if (!file.good())
  42. return false;
  43.  
  44. int num;
  45. int line=1;
  46. int sum=0;
  47.  
  48. while(!file.eof())
  49. {
  50. if(newl(file)||file.eof())
  51. {
  52. cout<<sum<<endl;
  53. sum=0;
  54. line++;
  55. }
  56. if (numload(file,num))
  57. sum+=num;
  58. else
  59. {
  60. cout<<"error in line: "<<line<<endl;
  61. sum=0;
  62. line++;
  63. file.ignore(numeric_limits<streamsize>::max(),'\n');
  64. }
  65. }
  66. file.close();
  67. }
  68.  
  69. int main()
  70. {
  71. if (!loadfile("file2.txt"))
  72. cout<<"error, couldn't read the file";
  73.  
  74. return 0;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement