Witam, mam na zajęcia napisać nieco uproszczoną wersję grepa - treśc zadania podana w komantarzach. Już się cieszę, że skończyłem, a tu błąd kompilacji. Zaznaczyłem komentarzem gdzie. Nie mam zielonego pojęcia co jest źle. Może ktoś pomóc? Ewentualnie jakieś komentarze odnośnie stylu itp? Dopiero zaczynam zagłębiać się w programowanie, więc myślę, że to najlepsza pora na wyrobienie sobie dobrych nawyków ;)
błędy kompilacji: (g++, Ubuntu)
In file included from /usr/include/c++/4.5/ios:39:0,
from /usr/include/c++/4.5/ostream:40,
from /usr/include/c++/4.5/iostream:40,
from grep.cpp:4:
/usr/include/c++/4.5/bits/ios_base.h: In copy constructor ‘std::basic_ios<char>::basic_ios(const std::basic_ios<char>&)’:
/usr/include/c++/4.5/bits/ios_base.h:785:5: error: ‘std::ios_base::ios_base(const std::ios_base&)’ is private
/usr/include/c++/4.5/iosfwd:77:11: error: within this context
/usr/include/c++/4.5/iosfwd: In copy constructor ‘std::basic_fstream<char>::basic_fstream(const std::basic_fstream<char>&)’:
/usr/include/c++/4.5/iosfwd:117:11: note: synthesized method ‘std::basic_ios<char>::basic_ios(const std::basic_ios<char>&)’ first required here
/usr/include/c++/4.5/streambuf: In copy constructor ‘std::basic_filebuf<char>::basic_filebuf(const std::basic_filebuf<char>&)’:
/usr/include/c++/4.5/streambuf:773:7: error: ‘std::basic_streambuf<_CharT, _Traits>::basic_streambuf(const std::basic_streambuf<_CharT, _Traits>::__streambuf_type&) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_streambuf<_CharT, _Traits>::__streambuf_type = std::basic_streambuf<char>]’ is private
/usr/include/c++/4.5/iosfwd:108:11: error: within this context
/usr/include/c++/4.5/iosfwd: In copy constructor ‘std::basic_fstream<char>::basic_fstream(const std::basic_fstream<char>&)’:
/usr/include/c++/4.5/iosfwd:117:11: note: synthesized method ‘std::basic_filebuf<char>::basic_filebuf(const std::basic_filebuf<char>&)’ first required here
grep.cpp: In function ‘int main(int, char**)’:
grep.cpp:120:78: note: synthesized method ‘std::basic_fstream<char>::basic_fstream(const std::basic_fstream<char>&)’ first required here
grep.cpp:120:78: error: initializing argument 1 of ‘void Find(std::fstream, std::string, std::string, bool, bool)’
/*Napisz program, który będzie uproszczoną wersją grep-a. Jako argument otrzymuje wzorzec (zwykły napis, bez wyrażeń regularnych) do wyszukania, a następnie listę plików (co najmniej jeden). Program powinien wyświetlić wszystkie linie z podanych plików, które zawierają ten wzorzec (w postaci nazwa_pliku:linia).
Opcja "-i" powinna ignorować wielkość liter we wzorcu/liniach.
Opcja "-n" powinna wypisywać także numer linii zawierających wzorzec.
Program powinien być możliwie odporny na wszelkie napotkane błędy, wyświetlając odpowiednie komunikaty.*/
#include<iostream>
#include<fstream>
#include<cstring>
using namespace std;
struct data
{
bool IgnoreHeigh; //czy ignorowac wielkosc liter -i
bool ShowLiNr; //czy wyswietlac numer linii -n
int N; // ile plikow
string *files;
string key;
};
string tolower(string str)
{
string nstr = str;
for(int i=0; i<str.length(); i++)
if(static_cast<int>(str[i]) <= 90 && static_cast<int>(str[i]) >= 65) nstr[i] += 32;
return nstr;
}
int LoadPrefs(int arg, char *tab[], struct data &opt)
{
opt.IgnoreHeigh = false;
opt.ShowLiNr = false;
opt.N = 0;
if(arg < 3)
{
cout << "Za malo argumentow\n";
return 1;
}
opt.key = tab[1];
for(int i=2; i<arg; i++)
if(tab[i][0] != '-') opt.N++;
if(opt.N == 0)
{
cout << "Nie przekazano poprawnie plikow\n";
return 2;
}
opt.files = new string [opt.N];
for(int i=0; i<opt.N; i++)
opt.files[i] = tab[i+2];
for(int i=opt.N+2; i<arg; i++)
{
if(static_cast<string>(tab[i]) == "-i") opt.IgnoreHeigh = true;
else if (static_cast<string>(tab[i]) == "-n") opt.ShowLiNr = true;
else
{
cout << "Zle przekazane opcje\n";
return 3;
}
}
return 0;
}
bool OpenFiles(fstream *files, int N, string *_files)
{
bool ok = true;
for(int i=0; i<N; i++)
files[i].open(_files[i].c_str(), ios::in);
for(int i=0; i<N; i++)
if(!files[i].is_open())
{
ok = false;
cout << "Blad podczas otwierania " << _files[i] << endl;
}
if(ok) return true;
else return false;
}
void Find(fstream file, string filename, string key, bool IgnoreHeigh, bool ShowLiNr)
{
bool ok = false;
if(IgnoreHeigh) key = tolower(key);
int line = 1; //obecna linia podczas przegladania
string CurrentLine; //obecna linia przechowywana w stringu
while(!file.eof())
{
getline(file, CurrentLine);
if(IgnoreHeigh) CurrentLine = tolower(CurrentLine);
if(strstr(CurrentLine.c_str(), key.c_str()) != NULL)
{
cout << filename << ":";
if(ShowLiNr)
cout << line << ":";
cout << CurrentLine << endl;
ok = true;
}
line++;
}
if(!ok) cout << "Brak szukanej frazy w pliku " << filename << endl;
}
int main(int argc, char *argv[])
{
struct data Prefs;
if(LoadPrefs(argc, argv, Prefs) != 0) return 1;
fstream *files = new fstream [Prefs.N];
if(!OpenFiles(files, Prefs.N, Prefs.files)) return 2;
//!!!!!!!!!!!!!!!!!!!!!!!!!!!! Nastepne dwie linijki błąd!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! tylko gdzie?
for(int i=0; i<Prefs.N; i++)
Find(files[i], Prefs.files[i], Prefs.key, Prefs.IgnoreHeigh, Prefs.ShowLiNr);
return 0;
}