Program napisany w C++. Program jest już skończony. Liczy wyrazy wpisane przez użytkownika i zapisuje je w pliku testowym, następnie pokazuję całą zawartość pliku i liczy ile jest słów w całym pliku. Muszę zmienić zapis i odczyt na binarny by dalej liczył słowa i nie wiem jak to zrobić.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class InputReader
{
private:
int number;
int words_1;
string text;
int v_length;
public:
string word;
InputReader()
{
number = 1;
words_1 = 0;
};
~InputReader() {};
friend void save(InputReader &object);
void type();
virtual void read_from_file();
};
void InputReader::type()
{
cout<<"Prosze wpisac swoj tekst: "<<endl;
getline(cin, text);
int v_length=text.length();
for (int i=0; i<v_length; i++)
{
if(text[i]==' ')
number++;
}
if(text[v_length-1]==' ')
number--;
if(text[0]==' ')
number=0;
else if (text[0] == NULL)
number=0;
cout<<endl;
cout<<"Liczba wyrazow w Twoim tekscie wynosi: "<<number<<endl;
cout<<"---------------------------------------------------------"<<endl;
cout<<endl;
}
void save(InputReader &object)
{
ofstream plik("Nazwa.txt",ios::app);
plik<<object.text<< endl;
}
void InputReader::read_from_file()
{
cout<<"Caly tekst z pliku: "<<endl;
ifstream file ("Nazwa.txt");
for( std::string line; getline( file, line ); )
{
cout<<line<<endl;
}
ifstream file2 ("Nazwa.txt");
while (file2>>word)
{
++words_1;
}
cout<<endl;
cout<<"Liczba slow: "<<words_1<<endl;
cout<<"---------------------------------------------------------"<<endl;
cout<<endl;
}
using namespace std;
int main()
{
InputReader objekt;
objekt.type();
save(objekt);
objekt.read_from_file();
return 0;
}