Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.

Problem s citanjem i pisanjem File-a

[es] :: C/C++ programiranje :: Problem s citanjem i pisanjem File-a

[ Pregleda: 2726 | Odgovora: 3 ] > FB > Twit

Postavi temu Odgovori

Autor

Pretraga teme: Traži
Markiranje Štampanje RSS

refa
Tuzla

Član broj: 60356
Poruke: 59
85.92.231.*



Profil

icon Problem s citanjem i pisanjem File-a08.01.2006. u 19:26 - pre 192 meseci
ovo je moj kod.

stablo.h
###################
#include<iostream>
using namespace std;
#include <string>
struct tnode{
char *bosanski,*engleski;
struct tnode *left,*right;
};
class BinaryTree
{
private:
struct tnode *root;
struct tnode * addtreework(struct tnode *,char *, char *);
struct tnode * searchtreework(struct tnode *,char *);
int treesizework(struct tnode *);
void treeprintinorderwork(struct tnode *);
void treeprintpreorderwork(struct tnode *);
void treeprintpostorderwork(struct tnode *);
void treedelete(struct tnode *);
public:
BinaryTree()
{
root=NULL;
}
~BinaryTree()
{
treedelete(root);
}
void addtree(char *, char *);
struct tnode* searchtree(char *);
int treesize(void);
void treeprintinorder();
void treeprintpostorder();
void treeprintpreorder();
};

###################################
stablo.cpp
##################################
#include "stablo.h"
#include<iostream>
#include<string>
using namespace std;
struct tnode * BinaryTree::addtreework(struct tnode *p,char *bos, char *eng)
{
int cond;
char *b,*e;
if(p==NULL)
{
p=new struct tnode;
b=new char[strlen(bos)+1];
e=new char[strlen(eng)+1];
strcpy(b,bos);
strcpy(e,eng);
p->bosanski=b;
p->engleski=e;
p->left=NULL;
p->right=NULL;
}
if((cond=strcmp(bos,p->bosanski))==0);
else if(cond<0)
p->left=addtreework(p->left,bos,eng);
else
p->right=addtreework(p->right,bos,eng);
return p;
}
void BinaryTree::addtree(char *bos, char *eng)
{
root=addtreework(root,bos,eng);
}
struct tnode * BinaryTree::searchtreework(struct tnode *p,char *word)
{
int cond;
if(p==NULL);
else if(((cond=strcmp(p->bosanski,word))&&(cond=strcmp(p->engleski,word)))==0);
else if(cond<0)
p=searchtreework(p->left,word);
else
p=searchtreework(p->right,word);
return p;
}
struct tnode * BinaryTree::searchtree(char *word)
{
struct tnode *ptr;
ptr=searchtreework(root,word);
return ptr;
}
void BinaryTree::treedelete(struct tnode *p)
{
if(p!=NULL)
{
treedelete(p->left);
treedelete(p->right);
cout<<" BRISEM:\t "<<p->bosanski<<"\t"<<p->engleski<<endl;
delete p;
}
}
int BinaryTree::treesizework(struct tnode *p)
{
if (p == NULL)
return 0;
else
return (1 + treesizework(p->left) + treesizework(p->right));
}
int BinaryTree::treesize(void)
{
int i;
i = treesizework(root);
return i;
}
void BinaryTree::treeprintinorderwork(struct tnode * p)
{
if( p != NULL)
{
treeprintinorderwork(p->left);
cout<<p->bosanski<<"\t"<<p->engleski<<endl;
treeprintinorderwork(p->right);
}
}

void BinaryTree::treeprintinorder(void)
{
treeprintinorderwork(root);
}


void BinaryTree::treeprintpreorderwork(struct tnode * p)
{
if(p != NULL)
{
cout<<"\n"<<p->bosanski<<"\t"<<p->engleski<<endl;
treeprintpreorderwork(p->left);
treeprintpreorderwork(p->right);
}
}

void BinaryTree::treeprintpreorder(void)
{
treeprintpreorderwork(root);
}


void BinaryTree::treeprintpostorderwork(struct tnode * p)
{
if(p != NULL)
{
treeprintpostorderwork(p->left);
treeprintpostorderwork(p->right);
cout<<"\n"<<p->bosanski<<"\t"<<p->engleski<<endl;
}
}


void BinaryTree::treeprintpostorder(void)
{
treeprintpostorderwork(root);
}
############################
main_programm
###########################
#include "stablo.h"
#include <stdio.h>
#include <string>
#include <iostream>
using namespace std;
int main(int argc,char *argv[])
{
BinaryTree rijecnik;
tnode *ptr;
int izbor=1;
//i varijabla sluzi samo na kraju da ustavi program
rijecnik.addtree("kuca","house");
rijecnik.addtree("magarac","donkey");
rijecnik.addtree("mama","mother");
rijecnik.addtree("tata","dady");
rijecnik.addtree("zadaca","homework");
//unos par rijeci unutar rijecnika da nije prazan
do{
cout<<"::::::::::::::BOSANSKO-ENGLESKI,ENGLESKO-BOSANSKI Rijecnik::::::::::::::"<<endl;
cout<<":::::::::::::: ::::::::::::::"<<endl;
cout<<"::::::::::::::....................MENI....................::::::::::::::"<<endl;
cout<<endl;
cout<<" #1.Bosansko-Engleski Rijecnik"<<endl;
cout<<" #2.Englesko-Bosanski Rijecnik"<<endl;
cout<<" #3.Dodati nove rijeci u rijecnik"<<endl;
cout<<" #4.Izlistaj sve rijeci u rijecniku"<<endl;
cout<<" #5.Quit"<<endl;
cout<<"\n\n";
cout<<"Vas izbor:\t";
cin>>izbor;

switch (izbor)
{

case 1:
{
cout<<"\n\n\n--------------------------------------------------------------------------------";
char *r=new char[25]; //moglo je i sa stringom
cout<<"Prevedi rijec:\t";
cin>>r;
cout<<endl;
ptr=rijecnik.searchtree(r);
delete r;
if(ptr!=NULL) //u slucaju da nema rezultata ptr prima vrijednost NULL i kao takava ne moze isprintat nista sto dovodi do prekida programa
cout<<ptr->bosanski<<"\t"<<ptr->engleski<<endl;
else
cerr<<"nema takva rijec u rijecniku"<<endl;
cout<<"--------------------------------------------------------------------------------\n\n"<<endl;
}break;
case 2:
{
cout<<"\n\n\n--------------------------------------------------------------------------------";
char *r=new char[25];
cout<<"Prevedi rijec:\t";
cin>>r;
cout<<endl;
ptr=rijecnik.searchtree(r);
delete r;
if(ptr!=NULL)
cout<<ptr->engleski<<"\t"<<ptr->bosanski<<endl;
else
cerr<<"nema takva rijec u rijecniku"<<endl;
cout<<"--------------------------------------------------------------------------------\n\n"<<endl;
}break;
case 3:
{
cout<<"\n\n\n--------------------------------------------------------------------------------";
char *rijecbos=new char[25];
char *rijeceng=new char[25];
cout<<"bos:\t";
cin>>rijecbos;
cout<<"\nend:\t";
cin>>rijeceng;
rijecnik.addtree(rijecbos,rijeceng);
delete rijecbos;
delete rijeceng;
cout<<"--------------------------------------------------------------------------------\n\n"<<endl;
}break;
case 4:
{
cout<<"\n\n\n--------------------------------------------------------------------------------";
cout<<"Velicina stabla: "<<rijecnik.treesize()<<endl;
cout<<endl;
rijecnik.treeprintinorder();
cout<<"--------------------------------------------------------------------------------\n\n"<<endl;
}break;
}

}while(izbor!=5);

return 0;
}



####################################
pitanje:
kako da smijestam ove rijeci u neki file na disku
znam da je
file *dat;
fwrite("c:\\dat.bin",sizeof(struct tnode),1,dat);
ono sto mi je potrebno za pisanje
Medjutim ja nemam vishe struktura nego samo pointer na memorijsku lokaciju gdje je alocirana struktura, i da napravim rucno neki ja rijecnik ne znam onda kako cu ucitavati tako da ove funkcije mogu citati iz tog fila i pretrazivati sve to.
pomoc!!
refa
 
Odgovor na temu

Buffy
Stanko Culaja
Sipovo, BiH

Član broj: 45310
Poruke: 312
*.teol.net.



Profil

icon Re: Problem s citanjem i pisanjem File-a09.01.2006. u 00:27 - pre 192 meseci
Kako se ti uopste snadjes u ovom kodu. :)
Ne bih ga mogao procitati ni za karton vinjaka.

Pokusaj rijeci pisati funkciom fprintf()- slicna je funkciji printf.(Pogledaj man).
pozdrav i ljepse pisi kod. :)
 
Odgovor na temu

RedDrake

Član broj: 80474
Poruke: 17
*.adsl.net.t-com.hr.



Profil

icon Re: Problem s citanjem i pisanjem File-a09.01.2006. u 00:59 - pre 192 meseci
Code:
//------------------------------------------------------------------------
//
// Name : Rijecnik.cpp
//
// Desc : Glavni source fajl
//
//------------------------------------------------------------------------
// Include files
//------------------------------------------------------------------------
#include <iostream>
#include <string>
#include <fstream>
#include <map>





//------------------------------------------------------------------------
// Namespaces
//------------------------------------------------------------------------
using namespace std;





//------------------------------------------------------------------------
// Macroes
//------------------------------------------------------------------------
#define ADD_WORD(eng,bos)      fEngleske.insert(pair<string, string>(eng, bos));\
                            fBosanske.insert(pair<string, string>(bos, eng));





//------------------------------------------------------------------------
//
// Name : main
//
// Desc : Pocetna funkcija programa
//
//------------------------------------------------------------------------
int main (void)
{
    map<string, string> fEngleske;
    map<string, string> fBosanske;


    ADD_WORD("house", "kuca");
    ADD_WORD("donkey", "magarac");
    ADD_WORD("mother", "mama");
    ADD_WORD("dady", "tata");
    ADD_WORD("homework", "zadaca");


    cout << "::::::::::::::BOSANSKO-ENGLESKI,ENGLESKO-BOSANSKI Rijecnik::::::::::::::\n";
    cout << ":::::::::::::: ::::::::::::::\n";
    cout << "::::::::::::::....................MENI....................::::::::::::::\n\n";


    int fOption;
    
    do
    {
        cout << "\n#1 Bosansko engleski rijecnik \n#2 Englesko bosanski rijecnik \n"
             << "#3 Dodati nove rijeci u rijecnik \n#4 Izlistati sve rijeci u rijecniku \n"
             << "#5 Spremi u datoteku \n#6 Ucitaj datoteku \n"
             << "#7 Kraj \n";
        
        cin >> fOption;

        string tWord, tTranslated;
        map<string, string>::iterator fIt;
        fstream tFile;

        switch (fOption)
        {
            case 1:
                cout << "Unesi rijec : \n";
                cin >> tWord;

                fIt = fBosanske.find(tWord);

                if (fIt != fBosanske.end())
                    cout << "Rijec znaci : \n" << fIt->second;
                else
                    cout << "Rijec ne postoji u rijecniku ! \n";
                
                break;

            case 2:
                cout << "Unesi rijec : \n";
                cin >> tWord;

                fIt = fEngleske.find(tWord);

                if (fIt != fEngleske.end())
                    cout << "Rijec znaci : \n" << fIt->second;
                else
                    cout << "Rijec ne postoji u rijecniku ! \n";
                
                break;

            case 3:
                cout << "Unesite rijec na engleskom : \n";
                cin >> tWord;
                cout << "Unesite rijec na bosanskom : \n";
                cin >> tTranslated;

                ADD_WORD(tWord, tTranslated);

                break;

            case 4:
                fIt = fBosanske.begin();

                do
                {
                    cout << "Rijec : " << fIt->first << " znaci : " << fIt->second 
                         << " na engleskom. \n";
                    fIt++;
                }while (fIt != fBosanske.end());

                break;

            case 5:
                cout << "Unesite ime fajla : \n";
                cin >> tWord;

                tFile.open(tWord.c_str(), ios_base::out);
                
                if (tFile.is_open() != true)
                {
                    cout << "Greska ! \nNemoguce kreirati fajl imena : " << tWord << "\n";
                    break;
                }
                
                fIt = fBosanske.begin();

                do
                {
                    tFile << fIt->first << " " << fIt->second << "\n";
                    fIt++;
                }while (fIt != fBosanske.end());

                tFile.close();

                break;

            case 6:
                cout << "Unesite ime fajla : \n";
                cin >> tWord;

                tFile.open(tWord.c_str(), ios_base::in);
                
                if (tFile.is_open() != true)
                {
                    cout << "Greska ! \nNemoguce otvoriti fajl imena : " << tWord << "\n";
                    break;
                }
                
                do
                {
                    tFile >> tTranslated >> tWord;
                    ADD_WORD(tWord, tTranslated);

                }while (tFile.eof() == false);

                tFile.close();

                break;
        };
    
    }while (fOption != 7);


    return 0;
};



Ovdje koristim STL - sto je IMO puno ljepse/lakse za koristit od glupih C IO funkcija...
Isto tako sam ti stablo zamijenio sa dva std::map objekta ...
Moja preporuka - ako programiras c++, onda koristi i C++ standardni library, a ne C-ov ;)
 
Odgovor na temu

refa
Tuzla

Član broj: 60356
Poruke: 59
*.dlp297.bih.net.ba.



Profil

icon Re: Problem s citanjem i pisanjem File-a10.01.2006. u 07:53 - pre 192 meseci
e bash ti hvala.

refa
 
Odgovor na temu

[es] :: C/C++ programiranje :: Problem s citanjem i pisanjem File-a

[ Pregleda: 2726 | Odgovora: 3 ] > FB > Twit

Postavi temu Odgovori

Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.