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

problemi sa streamovima

[es] :: C/C++ programiranje :: problemi sa streamovima

[ Pregleda: 2988 | Odgovora: 5 ] > FB > Twit

Postavi temu Odgovori

Autor

Pretraga teme: Traži
Markiranje Štampanje RSS

marko000
marko vucinic
Beograd

Član broj: 6501
Poruke: 63
*.verat.net



Profil

icon problemi sa streamovima27.07.2003. u 11:50 - pre 251 meseci
Hmm..imam jedan mali problem oko streamova.Ovih dana sam
isprobavao iste u c++.

Evo koda:


Code:

#include <fstream.h>

int main(){
    
    char buffer[50];
    char* ime=new char[10];
    cin>>ime;
    ifstream file(ime,ios::in);
    int vel=0;
    while(file.get()!=EOF) vel++;
    file.seekg(0);
    file.read(buffer,vel);
    cout<<buffer;
    file.close();
    cout<<endl;
    return 0;
}


problem je u tome sto posle

Code:

while(file.get()!=EOF) vel++;



nece nista da mi ucita u bafer..mislio sam da promeni poziciju pokazivaca
zbog file.get() ali ni pozicioniranje ne pomaze(proveravao sam poziciju sa
file.tellg() )

jednostavno nemogu da provalim u cemu je fora..

postoji resenje da zatvorim datoteku posle kriticne(gore navedene linije)
a zatim ponovo otvorim ali to mi je bas nekako glupavo..

u cemu gresim?
help?

pozdrav,marko...

p.s.da li neko zna za tutoriale koji se bave slicnim problemima..
dakle streamovi i stringovi...
 
Odgovor na temu

leka
Dejan Lekić
senior software engineer, 3Developers
Ltd.
London, UK

Član broj: 234
Poruke: 2534
*.telia.com

Sajt: dejan.lekic.org


+2 Profil

icon Re: problemi sa streamovima27.07.2003. u 12:00 - pre 251 meseci
Probaj da umesto
Code:

while(file.get()!=EOF) vel++;

staviš
Code:

while (!file.eof()) {
   vel++;
  // neko citanje ili pozicioniranje ovde...
}


ili

Code:

char cSlovo;
while (!file.get(cSlovo)) {
   vel++;
}

Ali naravno to može i bolje da se odradi... :)
Dejan Lekic
software engineer, MySQL/PgSQL DBA, sysadmin
 
Odgovor na temu

marko000
marko vucinic
Beograd

Član broj: 6501
Poruke: 63
*.verat.net



Profil

icon Re: problemi sa streamovima27.07.2003. u 12:21 - pre 251 meseci
jos kada bi napisao kako.. ;)))
 
Odgovor na temu

leka
Dejan Lekić
senior software engineer, 3Developers
Ltd.
London, UK

Član broj: 234
Poruke: 2534
*.telia.com

Sajt: dejan.lekic.org


+2 Profil

icon Re: problemi sa streamovima27.07.2003. u 12:51 - pre 251 meseci
Pa ovo gore bi trebalo takodje da radi...

Ako su fajlovi mali, najbolje bi bilo naci velicinu fajla na pocetku, "svuci" ceo fajl u neki veliki string, i onda taj strng obradjivati...
Dejan Lekic
software engineer, MySQL/PgSQL DBA, sysadmin
 
Odgovor na temu

idb
Ivan Bulic
Beograd

Član broj: 4436
Poruke: 402



Profil

icon Re: problemi sa streamovima28.07.2003. u 16:37 - pre 251 meseci
Ako sam dobro shvatio, hoces da odstampas sadrzaj fajla na cout.
Sledeci primer to radi a preuzet je sa:

http://www.cplusplus.com/ref/index.html
pa izaberi redom: iostream library =>ifstream => tellg

tamo se nalazi sledeci primer:
// read a file into memory
#include <iostream>
#include <fstream>
#include <stdlib.h>
using namespace std;

int main(int argc, char *argv[]) {
int length;
char * buffer;

ifstream is;
is.open ("text.txt", ios::binary );

// get length of file:
is.seekg (0, ios::end);
length = is.tellg();
is.seekg (0, ios::beg);

// allocate memory:
buffer = new char [length];

// read data as a block:
is.read (buffer,length);
is.close();

cout.write (buffer,length);
system("PAUSE");
return 0;
}

Gore pomenuti link ima dosta informacija koje tebe interesuju.
 
Odgovor na temu

alkal
Aleksandar Kalanj
Beograd

Član broj: 7960
Poruke: 26
*.rcub.bg.ac.yu

Jabber: alkal@elitesecurity.org
Sajt: wapdata.co.cc


Profil

icon Re: problemi sa streamovima29.07.2003. u 02:43 - pre 251 meseci
A može i:
Code:

#include <fstream>
#include <iostream>
using namespace std;

int main() {
    ifstream dat ("tekst.txt");
    cout << dat.rdbuf();
}

Pogledaj knjigu "Thinking in C++":
http://mindview.net/Books/DownloadSites
 
Odgovor na temu

[es] :: C/C++ programiranje :: problemi sa streamovima

[ Pregleda: 2988 | Odgovora: 5 ] > FB > Twit

Postavi temu Odgovori

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