Yes, if you want to combine such attributes (e.g., ios::in | ios::out), but you cannot combine those two.
Yes, if you want to combine such attributes (e.g., ios::in | ios::out), but you cannot combine those two.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
I tried to seperate them, like so, but it still didn't work (same segmentation fault).
Code:ofstream accfile; accfile.open ("loans.txt", ios::trunc); accfile.close(); accfile.open ("loans.txt", ios::app);
Yes, I said that - but I also said "Do not combine app with trunc" as you are at the same time saying "truncate the file" and "append to the file" - which makes absolutely no sense - and what you actually get from it is completely undefined.
What I wanted to tell is that IF you have valid combinations that you want to pass in, you need to combine them using the bitwise or operator.
Did you actually test if accfile is valid before writing to it? Such as
It may well be that the iostream implementation actually detects the bad combination and the opening of the file fails, perhaps?Code:if (!accFile) { cout << "Could not open the file... " << endl; return; }
I am also a bit suspicious about this one:
Why is there a +1 there?Code:for (int i = 0; i < (BOOK_MAX+1); i++)
--
Mats
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
The segmentation fault might have some other cause. For example, how many elements does Loans have?Originally Posted by Furious5k
Note that separating them the way you did just makes the first opening of the file have no net effect. You basically opened, did nothing, then closed.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Well, in the sequence as described here, it truncates the file, so it does indeed have an effect - it removes the conent in the file. Why you would THEN append to the file, I don't really understand, as there is no content to append onto - so the second file opening is absolutely meaningless waste of time [as long as the close is removed]. When the file is truncated, it has a zero length, the write (put) pointer is at the beginning of the file, and the file is opened for write - so what does the open with ios::app actually do at this point?
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
matsp - due to my lack of experience. I think it should be -1. An array[5] would be 0 to 4, yes?
Also, sorry for misreading.
laserlight - I was under the impression that opening the file with ios::trunc would erase its contents. How do I do this properly?
On another note, would this sort a "string myaray[NUMBER]" alphabetically?
Code:#include <algorithm> sort(myarray, myarray+NUMBER)
Last edited by Furious5k; 01-06-2009 at 07:57 AM.
Does renaming a temporary file make it permanent?
What are you trying to do?Originally Posted by Furious5k
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
I have read the file into an array earlier on in the code.
Now I want to erase the contents and re-enter them from the array skipping the location in the array which holds the same content as the one entered by user
The alphabetical sort is for another part of the code. I probably shouldn't have posted it here, sorry for the confusion.
That is probably causing your segmentation fault then - and it probably should be
No +1 or -1.Code:for (int i = 0; i < BOOK_MAX; i++)
It does - but you do not then need to use ios::app on the file to add to it - since it is already empty.Also, sorry for misreading.
laserlight - I was under the impression that opening the file with ios::trunc would erase its contents. How do I do this properly?
On another note, would this sort a "string myaray[NUMBER]" alphabetically?
[/QUOTE]Code:#include <algorithm> sort(myarray, myarray+NUMBER)
I suppose so.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
It's working now. But when I cat the contents of the file I get the content I want plus a great many ";". I know which part of the code it is.
How would I tell the code to check for null value (never done it)?
That's how I understand it.Code:if (Loans[i].Book = NULL)
With the temporary files, I was referring to another site I looked at, which defined "tmpfile" as "Open a temporary file". I guess it just creates a file then, which is (usually) used temporarily?
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Nevermind, thank you C_ntua.
But I now receive an error " no match for `string & == int' "