Deitel & Deitel (C++ How To Prog) Misleading?

This is a discussion on Deitel & Deitel (C++ How To Prog) Misleading? within the C++ Programming forums, part of the General Programming Boards category; Gentlemen (doubt any ladies enjoy programming here) I have an interesting discovery that I'm sure many of you already know. ...

  1. #1
    Registered User johnnyd's Avatar
    Join Date
    Mar 2002
    Posts
    70

    Question Deitel & Deitel (C++ How To Prog) Misleading?

    Gentlemen (doubt any ladies enjoy programming here) I have an interesting discovery that I'm sure many of you already know. So without trying to seem like some naive Christopher Columbus, let me pose this question to you this way:

    When you open a Random file as (ios::ate), for example:

    Code:
    ofstream outfile("somefile.dat", ios::ate);
    even if the file doesn't exist, it will be created. Also, this command moves directly to the end of the file, so data entry can start right there. For my intents and purposes, that's fine. I also find that I can move around anywhere in the file using seekp and placing records in their relative position just fine as well.

    However, in the book C++ How To Program by Deitel and Deitel, they do several extra (and seemingly unnecessary) things.

    If you own the book, or have access to it, I'm using the 4th Edition, and the problem to which I refer is on Pages 828-9. Let me retype the portion of the code for you here to save you the trouble of getting up from your comfortable position. I've omitted much of the other unnecessary code and just included the pertinent parts. Here goes:

    Code:
    ofstream outCredit("credit.dat", ios::binary);
    
    if (!outCredit){
       cerr<<"File could not be opened."<<endl;
       exit(1);
       }
    
    //ClientData is a custom built class they wrote up and 
    //included in this code. However, I've omitted it for the sake of 
    //brevity.
    ClientData blankClient; 
    
    //This is the business end of this block of code
    for (int i = 0; i < 100; i++)
       outCredit.write(reinterpret_cast< const char * >(&blankClient), sizeof(ClientData));
    Now what this piece of code does is simply to create 100 blank spaces in the target file for later use. The logic behind this is rather obvious - so that later when we are ready to start imputting real data, we have 100 spaces to work with. Now I realise this was absolutely necessary with C, but not with C++.

    Let me explain. Since I've inadvertently discovered the power of opening a file with ios::ate, I've not only realised that I didn't need to create 100 spaces, I also realised that I was not limited to how many records I could add to this file as was the case in C!!

    So why did Deitel and Deitel do this? I feel misled in the sense that I have a lot of code in my program similar to the above, which is good for practically nothing!

    Also, to make matters worse, in Deitel and Deitel, whenever they are ready to open the file for input of REAL data, they use ios::binary. Now I had an interesting problem with that.

    Everytime I entered a second record into the file, the previous one got erased! Even though the second file was inserted at the correct position in the file. So when I printed out the file, all of the previous records were blanked, save the last record I entered.

    I soon realised that everytime I opened a binary file with ios::binary, it truncated the ENTIRE file!!! (emptied it) and then placed the last entry into it! (Which explains the above phenomenon).

    My question is this: Is this a clear error on behalf of the authors of the book? Did anyone else out there in all of C-Dom have the same issue as I did? Is it because my implementation of C++ is different from everybody else's?

    I'm using Borland C++ 5.02 by the way. The only reason I haven't yet changed over to something else is that I have to ensure the code I write here can run on the examiner's PCs at school.

    If anyone has any insights into this matter or can tell me if my C++ is just plain weird, please make your comments. I would really LOVE to hear them. Thanks.
    Excuse me, while I water my money tree.

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    First off, it is rather presumptuous of you to say that there aren't any female programmers here. Second, you seem to be generally confused about a few concepts here. Though I do have to agree with some of your points, not all of them are well thought out.

  3. #3
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    Two thoughts:

    1. Did you check the errata page for the book?

    2. Email the authors of the book at the address given. I have done this before and have gotten a response back.

    We do not use the 4th ed of Deitel anymore. The previous editions were better.
    Mr. C: Author and Instructor

  4. #4
    Registered User johnnyd's Avatar
    Join Date
    Mar 2002
    Posts
    70

    Why?

    master5001, I wasn't trying to be presumptious. But never mind that. If there are any female coders out there that were in any way offended by that remark, I sincerely apologise, that was not my intention. Please don't read into that remark with contempt. I would never wishfully so something like that.

    Secondly, the errata pages make no mention of the things I mentioned. Apparently the Deitels are using a Unix implementation of C++. Not sure what compilers they use. Possibly the Unix OS requires this? Any thoughts about this?

    Thirdly, why do you think previous editions of the book were better? I would gladly appreciate your insights.

    Finally, no one has answered my core question. Has anyone a similar/different experience than the one I mentioned earlier?

    Thanks in advance.
    Excuse me, while I water my money tree.

  5. #5
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    >>>doubt any ladies enjoy programming here


    Maybe you should ask Prelude
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  6. #6
    Registered User johnnyd's Avatar
    Join Date
    Mar 2002
    Posts
    70
    Perhaps I should just repost the entire thing without that remark about no ladies enjoying coding here...

    Seems most ppl are paying more attention to that than the rest of the post. Oh well...
    Excuse me, while I water my money tree.

  7. #7
    Registered User
    Join Date
    Jan 2002
    Posts
    559
    Every book has errors. Let them know about it, maybe no one else has, or if there's a reason for why they did it that way, they'll probably let you know.

  8. #8
    Registered User johnnyd's Avatar
    Join Date
    Mar 2002
    Posts
    70

    Question

    Is Prelude, like, the head of this site? Just curious.
    Excuse me, while I water my money tree.

  9. #9
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    Originally posted by johnnyd
    Is Prelude, like, the head of this site? Just curious.
    Nah, she has MORE power.

  10. #10
    Registered User SAMSAM's Avatar
    Join Date
    Nov 2001
    Posts
    218
    I read C how to program 2nd edition by deitel sr&jr (3 times over).itsthe best , book i ever read about C(chalenging exercises).

    i also have the C++ how to program(2nd edition) which i
    think is very detailed and informative . yet not innovative
    in sample department (it just copies the samples in C edition
    to its C++ version). your case is one of them which is in
    file processing section(random access files) of C book and they simply recoded it in C++ , with the C++ how to program.

    email them they will probably give your money back

  11. #11
    Registered User johnnyd's Avatar
    Join Date
    Mar 2002
    Posts
    70

    Exclamation

    Prelude is a SHE?!?

    Dear God, I cannot even begin to express how embarassed I am - all this while... wow... ...this is quite an eye opener.

    I must say though that I'm impressed with her wealth of knowledge. I hope no one on this site thinks I'm being sexist.

    It's just based on my psychosocial development. Where I'm from, there's no such thing as a female programmer. Not because females are not socialised to go into the field, its just that females are not the least bit interested in it. For the most part anyway.

    Goes to show the world is a much bigger place than I could possibly imagine. In any event, I now realise the extent of my fallacy. Prelude, if you're reading this, please take no offence. At the very least, I'm willing to admit my ignorance. My sincerest apologies.

    I'm curious though... there were quite a few posts in which we have conversed (Prelude and myself) and I have referred to her as being male, and I was not corrected. Why?
    Excuse me, while I water my money tree.

  12. #12
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,701
    Here's what's going on johnnyd.

    If you open a file with ios::binary|ios::app, you can append but you can't seekp() because you will always append.
    If you open a file with ios::binary|ios::out, a previously existing file will be trucated.
    So to get around that we can open (an fstream) using ios::binary|ios::out|ios::in. Now the file isn't truncated, but if the file doesn't previously exist, the open fails - great. Since the latest C++ standard removed the ios::nocreate and ios::noreplace open modes, you have to rely on the open() behavior of ios::in and ios::out to work around it.
    Here is some code to demonstrate:
    Code:
    for (int n = 0; n<5; n++)
    {
        fstream o;
        
        //test to see if file exists
        o.open("temp",ios::in);
        if (!o)
        {
            //file doesn't exist, so create it
            o.clear(0);
            o.open("temp",ios::out);
            if (!o)
                cout << "WTF" << endl;
        }
        o.close();
    
        //open in binary for output, use ios::in to prevent truncation
        o.open("temp",ios::binary|ios::out|ios::in);
    
        //remove this line and use ios::ate if you like (I like this)
        o.seekp(0,ios::end);
        
        o << char('A' + n);
        o.close();
    }
    This will create a file with "ABCDE" in it. Run it again, and you'll have "ABCDEABCDE".

    Or you can do it the Deitel way...NOT. ;)

    gg

  13. #13
    Registered User johnnyd's Avatar
    Join Date
    Mar 2002
    Posts
    70
    SAMSAM, I believe your theory is right. I think they just rewrote the C How To Program using the examples from it (forgetting that some of them don't apply to C++).

    Codeplug, while your suggestion is valid, I could simply use the ios::ate function to do data entry on the random file. It works just fine, and uses a lot less code.

    Thanks guys for your suggestions. I don't think I've learned more from any of my previous posts. This site rocks...
    Excuse me, while I water my money tree.

  14. #14
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511

    Talking

    Johnny:

    Basically SamSam answers my question about Deitels texts. They basically reuse the same stuff for all there programing texts. This fourth edition stresses UML. That is fine to learn about UML and some OOAD but I think they have gone over board with it. Also, they have added a chapter on web programming- fine but most beginning programmers really do not need this. There are more reasons...

    C++ HTP (2nd and 3rd ed) and C HTP are better books.

    As for compiler - check the preface of the 4th ed (I did) It mentions other compiler choices. It does not specifically say witch they used>

    Once againg email deitel@deitel.com (in preface)
    Mr. C: Author and Instructor

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need help making a dot bounce up and down y axis in this prog
    By redwing26 in forum Game Programming
    Replies: 10
    Last Post: 08-05-2006, 01:48 PM
  2. C++ How to Program 3th Ed by Deitel and Deitel
    By The SharK in forum C++ Programming
    Replies: 10
    Last Post: 10-26-2005, 11:47 PM
  3. C++ How to Program by Deitel and Deitel
    By Thantos in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 07-24-2004, 03:29 AM
  4. Try my prog...
    By Commander in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 05-09-2002, 08:43 AM
  5. password prog
    By ihsir in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 01-06-2002, 06:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21