I really can't tell what you're trying to do in the printCart() function, you need to provide more content. |
I'll to be more specific, but English is not my mother tongue so it may be hard to understand it. I'm writing a simple program in various languages and variations for each language. In C++ I want to: use 1 filestream or 1 ifstream and 1ofstream, use the std:getline and the istream::getline, use strings, c-strings and stringstreams.
The program takes data from art.txt formatted as a list of: "code|name|price", uses it to fill a string vector (this works fine), then uses this vector to fill a ART vector (with ART being a class with string code, name and float price, this works too), then ask the user which articles he wanna buy and, based on code given, adds the article (formatted as "code - name - price euro" to a cart.txt file (this works, but only if I 1ifstream and 1ofstream)). I want to achieve this by using a single fstream:
* first use it in input mode to read from art.txt
* then close it and
** use it, in output mode to write on cart.txt (when user types a valid code)
** use it, in input mode to read from cart.txt (when user types "cart")
At this point, the problem is that cart.txt is not updated until the while cycle in which the user is asked to either type a code or "cart" has ended
*first way: I close fs (this updates cart.txt), call the printCart(fs) and in the function open fs(cart.txt) in input mode, use the ifstream.getline to store its content in a stringstream (I need to fix this and I think I know how thanks to your first quote), then fs is closed again and it's reopened (cart.txt) after the function has ended (so in the while cycle) in append mode. Do I need to flush the file, or having closed it before the function will suffice?
*second way: I DON'T close fs but flush it (I may have understood wrongly, but doesn't this mean that the cart.txt file is updated with the current buffer in fs (which is in output mode)?), call the printCart(fs) that reads from it: now that I write it down I don't think this should be possible: I have no way of reading from, fs, since it's still opened in output mode. Is this correct?
Is this what you were saying here?
Also when switching between writing and reading you need to flush the output stream before you try to read from the stream. |
For both of these ways I'd like to use the string::getline and the istream::getline in the printCart() (and I'm working on it right now)
Why are you trying to get a C-string into a std::string? It would probably be better if you used the proper getline() function for a std::string: getline(fs, s); |
I didn't know char* was a c-string: isn't char* and &string[0] the same? I mean, aren't them both an address of memory of a single character? I'll try to declare a char array and use it, again, just for learning how to do something.
UPDATE: I don't understand why in the example at http://www.cplusplus.com/reference/istream/istream/getline/ name and title are passed instead that name[0]...
What? But no a std::string is not a stream. |
No I mean, the first parameter is an istream &, can I pass it a fstream& (so an iostream) in input mode instead?
Thank you for your time in helping me, I really appreciate it.