Thread: ios::binary.... a discussion (mostly)

  1. #1
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412

    ios::binary.... a discussion (mostly)

    Code:
    #include <fstream>
    using namespace std;
    
    int main()
    {
    
         ofstream outs;
    
         outs.open("file.dat", ios::binary);
    
         outs << "ABCD" << "\n\n\n";
    
         outs.write("1234",4);
    
         outs.close();
    
    return 0;
    }
    ok... so we have a handly little binary file named file.dat that when opened with notepad contains...

    ABCD###1234

    (count the #s as small black squares)

    What is the purpose of this? I skipped the whole binary process in file I/O because it didn't interest me at the time... now it does.

    Over the past couple of years, we have had a lot of questions regarding binary files. For some reason, all this time, I was led to believe that binary files were not human readable. (Couldn't have been more off the mark with that thought...)

    What the heck do you use a binary file for that a ascii file cannot do?

    As naive as it may sound... I actually thought when I opened the file I would see a bunch of ones and zeros... you know... binary.... LOL.

    Blue

  2. #2
    Something Clever ginoitalo's Avatar
    Join Date
    Dec 2001
    Posts
    187
    Well Betazep,

    Binary files have a much much much faster access/retrieval time.

    Binary files are also much nicer when storing encapsulated data as in classes/struct's.

    You can move n bytes forward/backward
    or n bytes from the beginning or the end.

    It's a little more cryptic to the untrained eye.

    ...as to the storing or 1's and 0's
    I'm not sure but I'd like to know too

  3. #3
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    OIC... I am going to try that.
    Blue

  4. #4
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    Here go here and look at this its all about file I/O in C++: http://www.cpp-home.com/FileIO_tutorial.php just scroll down in it, it has a pretty good section on binary files.
    "only two things are infinite, the universe and human stupidity, and im not sure about the former." - albert einstein

  5. #5
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    Excellent link... thanks.
    Blue

  6. #6
    Something Clever ginoitalo's Avatar
    Join Date
    Dec 2001
    Posts
    187
    Here's something I cooked up :

    Code:
    #include<iostream.h>
    #include<fstream.h>
    #include<string.h>
    
    
    struct applicant {
    	char name[61];  // applicant's name
    	char phone[21]; // phone number
    	long date;      // date of application in yyyymmdd format
    };
    
    
    int main(void){
    
    	fstream fs("2111112.aaa", ios::in | ios::out | ios::binary);
    
            applicant x;
                    x.date=9;
                    strcpy(x.name,  "ginoitalo");
                    strcpy(x.phone, "13281");
    
    
    cout<<endl<<"--------------"<<endl<<"Name="<<x.name<<endl;
    cout<<"Date="<<x.date<<endl;
    cout<<"Phone="<<x.phone<<endl<<"--------------"<<endl<<endl;
    
            fs.seekp(0, ios::beg);
            fs.write( (char*)&x, sizeof(applicant) );
    
            fs.seekg(0, ios::end);
    
    
    cout<<"End of file        = "<<fs.tellg()        <<endl;
    cout<<"Size of applicant  = "<<sizeof(applicant) <<endl;
    
            int recs = ( 1*fs.tellg() / sizeof(applicant) );
            cout<<endl<<"Records in file="<<recs<<endl;
    
    
            applicant in;
            fs.seekg(0, ios::beg);
            fs.read((char*)&in, sizeof(applicant) );
    
    
    cout<<endl<<"--------------"<<endl<<"Name="<<in.name<<endl;
    cout<<"Date="<<in.date<<endl<<"Phone="<<in.phone<<endl;
    cout<<"--------------"<<endl<<endl;
    
    return 0;
    }

  7. #7
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    Hmm, you didnt even try to run that code did you b/c I can tell you its not going to work haha.
    "only two things are infinite, the universe and human stupidity, and im not sure about the former." - albert einstein

  8. #8
    Something Clever ginoitalo's Avatar
    Join Date
    Dec 2001
    Posts
    187
    Runs fine in VC++ 6.0

  9. #9
    Something Clever ginoitalo's Avatar
    Join Date
    Dec 2001
    Posts
    187
    and amazingly BCC32

    ..well looks like no errors..

    perhaps xds4lx will do his homework before posting next time

  10. #10
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    Doesnt here, and i have Visual C++ 6 Professional w/ service pack 5 + the processor pack. It doesnt work! You never test to see if the file opened, a big no no! Then when you try to load it in from file its loading garbage, and dont tell me how to program ive been doing it a lot longer than you im sure! Plus you have typos in there so hmm that cant be runnable!. And you are using old C++ headers that are non-standard, try using the standard headers w/ out the .h
    Last edited by xds4lx; 04-11-2002 at 09:35 PM.
    "only two things are infinite, the universe and human stupidity, and im not sure about the former." - albert einstein

  11. #11
    Something Clever ginoitalo's Avatar
    Join Date
    Dec 2001
    Posts
    187
    #include<iostream>
    #include<fstream>
    #include<string>

    using namespace std;


    Works fine.

    and...What typos ?

  12. #12
    Something Clever ginoitalo's Avatar
    Join Date
    Dec 2001
    Posts
    187
    p.s.

    Using Visual C++ Enterprise edition

    and

    Borland 5.5 Command line

  13. #13
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    Fight!!! j/k

    I get the idea though. I will try it out when I have the time.
    Blue

  14. #14
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    Here, heres a screenshot of the results of execution.
    --------------
    Name=ginoitalo
    Date=9
    Phone=13281
    --------------

    End of file = -1
    Size of applicant = 88

    Records in file=48806446

    --------------
    Name=¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦ ¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦
    ¦¦¦¦¦¦¦¦¦¦¦¦¦.¦_ginoitalo
    Date=-858993460
    Phone=¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦.¦_ginoitalo
    --------------

    Press any key to continue
    "only two things are infinite, the universe and human stupidity, and im not sure about the former." - albert einstein

  15. #15
    Something Clever ginoitalo's Avatar
    Join Date
    Dec 2001
    Posts
    187
    .sigh.

    The things I do....

    This will show the file doesn't read trash and that the file is properly opened and that it truly reads from the file
    (which it all did in the above version but wasn't evident enough to some)

    ...and there will be no fighting on the board
    or I will call your mother :)

    Code:
    #include<iostream>
    #include<fstream>
    #include<string>
    
    using namespace std;
    
    
    struct applicant {
    	char name[61];  // applicant's name
    	char phone[21]; // phone number
    	long date;      // date of application in yyyymmdd format
    };
    
    
    int main(void){
    
    	fstream fs("2111112.aaa", ios::in | ios::out | ios::binary);
    
    	applicant x;
                x.date=9;
                strcpy(x.name,  "ginoitalo");
                strcpy(x.phone, "13281");
    
    	cout<<endl<<"--------------"<<endl<<"Name="<<x.name<<endl;
    	cout<<"Date="<<x.date<<endl;
    	cout<<"Phone="<<x.phone<<endl<<"--------------"<<endl<<endl;
    
    	if(!fs.fail()){
    			fs.seekp(0, ios::beg);
    			fs.write( (char*)&x, sizeof(applicant) );
    			fs.close();
    	}
        else{
    		exit(99);
    	}    
    
    
    	// File is Totally Closed as of now.
    
    	// Re-open to prove it doen't read grabage.
    	fstream fs2("2111112.aaa", ios::in | ios::out | ios::binary);
    	
    	if(!fs2.fail()){
    
    		fs2.seekg(0, ios::end);
    		
    		cout<<"End of file        = "<<fs2.tellg()        <<endl;
    		cout<<"Size of applicant  = "<<sizeof(applicant) <<endl;
    
            int recs = ( 1*fs2.tellg() / sizeof(applicant) );
            cout<<endl<<"Records in file="<<recs<<endl;
    
            applicant in;
            fs2.seekg(0, ios::beg);
            fs2.read((char*)&in, sizeof(applicant) );
    		fs2.close();
    
    		cout<<endl<<"--------------"<<endl<<"Name="<<in.name<<endl;
    		cout<<"Date="<<in.date<<endl<<"Phone="<<in.phone<<endl;
    		cout<<"--------------"<<endl<<endl;
    	}
    	else{
    		exit(98);
    	}
    
    
    return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. instantiation point discussion
    By George2 in forum C++ Programming
    Replies: 1
    Last Post: 03-08-2008, 03:48 AM
  2. Terrain Screenshot and Discussion
    By Epo in forum Game Programming
    Replies: 28
    Last Post: 01-20-2006, 08:26 PM
  3. A Good Discussion - Stay on Topic
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 71
    Last Post: 04-28-2004, 08:52 AM
  4. enlightening discussion...
    By no-one in forum A Brief History of Cprogramming.com
    Replies: 48
    Last Post: 04-24-2004, 03:27 PM
  5. Slogan for a discussion board for Programmers of Bangladesh
    By zahid_isback in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 04-20-2004, 01:38 AM