Thread: dat.eof()

  1. #1
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272

    dat.eof()

    I got few questions about this code.

    dat - is that a class in iostream? I tried looking it up on google, with no results. Weird.

    Is "fstream" analogous to C's FILE structure?

    I'd also like to know why my dat.write() wont work.

    When i enter some data, and call dat.write to write the structure, the data doesnt appear in my file.

    I also couldnt find dat.eof() with google. Is there a site that tells about these functions?

    Code:
    #include <iostream>
    #include <fstream>
    #include <cstdlib>
    
    using namespace std;
    
    struct tstudent {
    	int mat_br;
    	char prez_ime[35];
    	int g_stu;
    };
    
    tstudent student;
    fstream dat;
    char naziv_datoteke[40];
    
    void kreiranje_datoteke();
    void unos_podataka_u_datoteku();
    void ispis_podataka_o_svim_studentima();
    void ispis_podataka_o_zadanom_studentu(int mb);
    
    int main()
    {
         
        int izbor,mbr;
        	cout << "Unesite naziv datoteke u koju cete spremati podatke:";
        	cin >> naziv_datoteke;
        do{
        	cout <<"Izaberite :" <<endl<<endl;
        	cout <<"1. Kreiranje datoteke"<<endl;
        	cout <<"2. Unos podataka u datoteku"<<endl;
        	cout <<"3. Ispis svih slogova iz datoteke"<<endl;
        	cout <<"4. Ispis podataka o zadanom studentu"<<endl;
        	cout <<"9. Izlaz iz programa"<<endl;
        	cin >> izbor;
        	switch (izbor){
        		case 1:kreiranje_datoteke();break;
        		case 2:unos_podataka_u_datoteku();break;
        		case 3:ispis_podataka_o_svim_studentima();break;
        		case 4:
        				cout << "Maticni broj studenta:";
        				cin >> mbr;
        				ispis_podataka_o_zadanom_studentu(mbr);
                        break;
            }
        } while (izbor != 9);
    
    }
    
    void kreiranje_datoteke(){
    	char promjena;
    	dat.open(naziv_datoteke,ios::in | ios::binary);
    	if (!dat){
    		dat.open(naziv_datoteke,ios::out | ios::binary);
    		dat.close();
    	}
    	else
    
    	{
    		cout << "Datoteka " << naziv_datoteke << " vec postoji na disku." << endl;
    		cout << "Ako ostane isti naziv datoteke, sadrzaj ce biti izbrisan."<<endl;
    		cout << "Hocete li promijeniti naziv datoteke? (d/n) "<<endl;
    		cin >> promjena;
    		if (promjena=='n') return;
    		cout << "Unesite naziv datoteke u koju cete spremati podatke:";
    		cin >> naziv_datoteke;
    		dat.open(naziv_datoteke,ios::out | ios::binary);
    		dat.close();
    	};
    };
    
    void unos_podataka_u_datoteku(){
    	char dalje;
    	dat.open(naziv_datoteke,ios::app | ios::binary);
    	do{
    		cout << "Maticni broj:";
    		cin >> student.mat_br;
    		cout << "Prezime i ime:";
    		cin >> student.prez_ime;
    		cout << "Godina studija:";
    		cin >> student.g_stu;
    		dat.write((char *) &student,sizeof(student));
    		cout << "Dalje? (d/n)";
    		cin >> dalje;
    	} while (dalje=='d');
    	dat.close();
    };
    
    void ispis_podataka_o_svim_studentima(){
    	dat.open(naziv_datoteke,ios::in | ios::binary);
    	while (1){
    		dat.read((char *) &student, sizeof(student));
    		if (dat.eof()) break;
    		cout << "Maticni broj: " << student.mat_br << endl;
    		cout << "Prezime i ime: " << student.prez_ime << endl;
    		cout << "Godina studija: " << student.g_stu << endl;
    	}
    	dat.close();
    };
    
    void ispis_podataka_o_zadanom_studentu(int mb){
    	int brojac=0;
    	dat.open(naziv_datoteke,ios::in | ios::binary);
    	while (dat.eof() == 0){
    		dat.read((char *) &student, sizeof(student));
    		if (student.mat_br==mb){
    			cout << "Maticni broj: " << student.mat_br << endl;
    			cout << "Prezime i ime: " << student.prez_ime << endl;
    			cout << "Godina studija: " << student.g_stu << endl;
    			brojac++;
    		}
    	}
    	if (brojac==0)
    		cout << "Student s maticnim brojem " << mb << " nije nadjen u datoteci."<<endl;
    	dat.close();
    };

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    dat is a variable in your code, of type fstream. Yes, fstream serves much the same purpose as FILE.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Tool
    dat - is that a class in iostream?
    No, look at this line:
    Code:
    fstream dat;
    Quote Originally Posted by Tool
    Is "fstream" analogous to C's FILE structure?
    Yes, you might say that.

    Quote Originally Posted by Tool
    I also couldnt find dat.eof() with google. Is there a site that tells about these functions?
    There are many, e.g., cppreference.com
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272
    Code:
    void unos_podataka_u_datoteku(){
    	char dalje;
    	dat.open(naziv_datoteke,ios::app | ios::binary);
    	do{
    		cout << "Maticni broj:";
    		cin >> student.mat_br;
    		cout << "Prezime i ime:";
    		cin >> student.prez_ime;
    		cout << "Godina studija:";
    		cin >> student.g_stu;
    		dat.write((char *) &student,sizeof(student));
    		cout << "Dalje? (d/n)";
    		cin >> dalje;
    	} while (dalje=='d');
    	dat.close();
    };
    Im still confused why this function wont write stuff to the file.

    I tried dat.flush(), didnt help.

  5. #5
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,743
    Change this line:

    Code:
    dat.open(naziv_datoteke,ios::app | ios::binary);
    To this:

    Code:
    dat.open(naziv_datoteke, ios::out | ios::app | ios::binary);
    That should get it to work.

    Oh...make sure you take the semicolon off the end of that function. You have a semicolon after the closing curly-brace in the code you posted.

    And some suggestions I have: try using ofstream instead of fstream, and try using the << (stream output) operator. Granted...it's not necessary that you do this, but it's what I suggest
    My Website

    "Circular logic is good because it is."

Popular pages Recent additions subscribe to a feed