It would appear, based on the error message, that you have accfile defined at the outer scope as being an ifstream, and you still can't write to an ifstream.
It would appear, based on the error message, that you have accfile defined at the outer scope as being an ifstream, and you still can't write to an ifstream.
Anything that is declared after an opening brace has a lifetime that lasts until the corresponding closing brace and then goes "out of scope". So you cannot use accfile outside the scope where it was declared.
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
All the errors have been sorted.
I am now having a problem with only the latest pieces of data that I output being saved in the file. Whatever else was in there vanishes. From examples that I've looked at, it should be working.
Am I ouputting to the file wrong? How do I correctly append the file?
You should open the file with std::ios::app as the mode flag.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)
If I'm using those, I don't need to write the "std::ios::" part?Code:#include <iostream> using namespace std;
Do I write this?
Code:accfile.setf(app);
You would still need to qualify with ios.Originally Posted by Furious5k
More like:Originally Posted by Furious5k
or:Code:ofstream accfile(filename, ios::app);
Code:accfile.open(filename, ios::app);
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Could anyone point me in the right direction, I'm struggling to figure out/find example of how I can erase a line of text from a txt file?
You can't. You can copy the file or the parts you want to keep.
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
Alright, how would I go about doing that?
Would I just read everything into an array and then write it back skipping the unwanted bits? The only thing I found in tutorials is "ios::trunc", would I write the following before writing everything (minus unwanted bits) to the file to get an updated version?
Code:accfile.open(filename, ios::trunc, ios::app);
You do not want ios::trunc, ios::app - you either want to append, or you want to truncate (which makes the file zero length) - and if you want to combine such attributes, they should be combined with | not a comma.
Yes, to remove a something in a file, you have two choices:
- Read all the file into memory (an array or some such), create a new file with the same name [do not crash at this point, or all data is lost!], write back out the bits you actually want to keep [of course, you can "not store them in memory" if that is easy to determine when you are reading the file into memory].
- Create a temporary file, read from the input file, write the data (except the bits you want to remove) to the temporary file. Remove the old file, then rename the temporary file to the original name.
You can obviously do the first variant by writing the new data to a temporary file, then removing the original and renaming the temporary file, which avoids the "all is lost if you crash here".
--
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.
Is the code for creating/removing/renaming files difficult? (working on a 10" netbook during holidays frustrated me into giving up until I got home, so I'm a bit tight on the deadlines now)
I'm getting a "Segmentation fault", which is odd since I'm outputting much the same as I did before. (I made a copy of the file for crashes, thanks for the pointer).Code:ofstream accfile; accfile.open ("loans.txt", ios::trunc | ios::app); string book; cin >> book; for (int i = 0; i < (BOOK_MAX+1); i++) { if (book == Loans[i].Book) { accfile << Loans[i].User << ";" << Loans[i].Book; } } accfile.close();
Last edited by Furious5k; 01-06-2009 at 07:19 AM.
I agree with matsp: ios::trunc | ios::app does not make sense. In The C++ Standard Library: A Tutorial and Reference Josuttis states on page 632 that "other combinations not listed in the table, such as trunc|app, not allowed".
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)