Hello, im designing a class where a user inputs are saved onto a file, then later retrieved to the program for other purposes, here is my relevant storing code:
and here is my relevant loading/reading code from my file. Between line 43-44 is when im reading my Unit and storing it. However it reads too much. for example, if my stored data was
N,1234,box,123.45,1,1,kg,5
unit would end up reading kg,5 instead of just kg. How do i stop it and let it ONLY read kg?
First why is unit a C-string instead of a std::string?
Now to answer the question you can, and should, limit the number of characters that the extraction operator>> will try to retrieve into a C-string by using the setw() manipulator.
file >> setw(11) >> unit_; // Always limit the number of characters being retrieved into a C-string.
But in this situation you should be using getline() with the optional third parameter, the ','. Remember that there are two different getline() functions, one that works with std::string, and another that works with a C-string, make sure you use the correct version for the data type you're using.