Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <limits>
- using namespace std;
- bool newl(ifstream & plik )
- {
- char cZnak;
- for(;; ) //nieskoñczona pêtla
- {
- plik.clear();
- cZnak = plik.peek(); //sprawdzamy jaki kolejny znak zostanie zwrócony przez operacjê odczytu
- if( plik.fail() || plik.bad() )
- return false; //wyst¹pi³ b³¹d odczytu danych
- if( !isspace( cZnak ) )
- return false; //pobrany znak nie jest bia³ym znakiem
- plik.get( cZnak ); //odczytujemy bia³y znak z pliku
- if( plik.fail() || plik.bad() )
- return false; //wyst¹pi³ b³¹d odczytu danych
- if( cZnak == '\n' )
- return true;
- } //for
- }
- bool numload(ifstream &file, int &num)
- {
- file.clear();
- file>>num;
- if (file.fail())
- return false;
- else return true;
- }
- bool loadfile(string fname)
- {
- ifstream file;
- file.open(fname.c_str());
- if (!file.good())
- return false;
- int num;
- int line=1;
- int sum=0;
- while(!file.eof())
- {
- if(newl(file)||file.eof())
- {
- cout<<sum<<endl;
- sum=0;
- line++;
- }
- if (numload(file,num))
- sum+=num;
- else
- {
- cout<<"error in line: "<<line<<endl;
- sum=0;
- line++;
- file.ignore(numeric_limits<streamsize>::max(),'\n');
- }
- }
- file.close();
- }
- int main()
- {
- if (!loadfile("file2.txt"))
- cout<<"error, couldn't read the file";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement