Hirdetés

Új hozzászólás Aktív témák

  • Zulfaim

    csendes tag

    Befejeztem.
    Ez lett a vége:

    #include <cstring>
    #include <iostream>
    #include <fstream>

    using namespace std;

    class buffer {
    char* buff;
    int size;
    public:
    buffer():buff(0),size(0){}
    buffer(const buffer&);
    buffer& operator=(const buffer&);
    buffer& operator+=(const buffer&);
    bool operator==(const buffer&);
    buffer& buffer:: operator+=( const char*);
    buffer& buffer:: operator=(const char*);
    bool buffer:: operator==(const char*);
    friend void beolvas(buffer &);
    friend void kiir(buffer &);
    friend ostream& operator<<(ostream& s, const buffer b);
    friend istream& operator>>(istream& s, const buffer b);
    ~buffer(){ delete[] buff; }

    };

    buffer::buffer(const buffer& e)
    {
    buff=new char[(size=e.size)+1];
    if (e.buff)
    strcpy(buff,e.buff);
    }

    buffer& buffer:: operator+=( const char* value)
    {
    char* temp=new char[size = strlen(buff)+ strlen(value) +1];
    strcpy(temp,buff);
    strcat(temp, value);
    delete[] buff;
    buff = temp;
    return *this;
    }

    buffer& buffer:: operator+=(const buffer& e)
    {
    char* temp=new char[size+=(e.size+1)];
    strcpy(temp,buff);
    strcat(temp,e.buff);
    delete[] buff;
    buff=temp;
    return *this;
    }

    buffer& buffer:: operator=(const char* value)
    {
    delete[] buff;
    if( size=strlen(value))
    {
    buff=new char[size+1];
    strcpy(buff,value);
    }
    return *this;
    }


    buffer& buffer:: operator=(const buffer& e)
    {
    if( this!= &e)
    {
    delete[] buff;
    buff=new char[size=e.size];
    strcpy(buff,e.buff);
    }
    return *this;
    }

    bool buffer:: operator==(const char* value)
    {
    if(size==strlen(value)) return true;

    else return false;
    }


    bool buffer:: operator==(const buffer& e)
    {
    if(size==e.size) return true;

    else return false;
    }

    ostream& operator<<(ostream& s, const buffer b)
    {
    for(int i=0;i<b.size;++i)
    {
    s<<b.buff;
    }
    return (s);

    }

    int karakterek_szama()
    {
    fstream fp(''c:\\txt.txt'',ios::in);
    char c;
    int k=0;
    while(!fp.eof())
    {
    fp.get(c);
    ++k;
    }
    fp.close();
    return k;
    }

    istream& operator>>(istream& s, buffer b)
    {
    int k=karakterek_szama();
    delete[] b.buff;
    b.buff=new char[(b.size=k)+1];
    for(int i=0;i<b.size;++i)
    {
    s>>b.buff
    ;
    }
    return (s);

    }

    void beolvas(buffer& b)
    {
    int k=karakterek_szama();
    delete[] b.buff;
    b.buff=new char[(b.size=k)+1];
    fstream fp(''c:\\txt.txt'',ios::in);
    while(!fp.eof())
    {
    fp.getline(b.buff,b.size);
    }
    fp.close();
    }

    void kiir(buffer& b)
    {
    fstream fp(''c:\\txt.txt'',ios::out);
    fp<<b.buff;
    fp.close();
    }

    int main()
    {
    buffer b;
    buffer a;
    beolvas(b);
    char s[7]=''+FUZES'';
    char s2[11]=''ASDFWERGWE'';
    a=s2;
    cout<<(a==b);
    cout<<endl;
    b+=s;
    cout<<b;
    cout<<endl;
    b+=a;
    cout<<b;
    kiir(b);
    cin.get();
    return 0;
    }


    [Szerkesztve]

Új hozzászólás Aktív témák