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:
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.
ofstream outfile("somefile.dat", ios::ate);
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:
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++.
ofstream outCredit("credit.dat", ios::binary);
cerr<<"File could not be opened."<<endl;
//ClientData is a custom built class they wrote up and
//included in this code. However, I've omitted it for the sake of
//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));
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.