Text files and Storing in variables

Hello,

I'm currently writing 2 classes for a program I am writing and I want to be able to read in from a text file and then store what is read to temp variables that will ultimately be stored in objects to the class. I've written a function that writes out to a function as long as the user wants using a do-while loop. I get the users information from a keyboard, store there answers in temp variables and pass that data into a function that reads out to a file. The format that the read_out function posts to the text file is:

int:char *:int:int "string": int:int: ...(as long as needed):int'\n'

ex. 123:some text:2 steps:2:3:4:5'\n' (very important it ends with a newline)

Since I have set up my write_to_file function to place data in the text file like the above example, I know that I could very well set up variables to be read into for each field.

For example, I know that since the layout is int:char *:int...and so on. That I can read until I hit each delimiter (":") using the stream extraction operator. The problem I am having is with the stream extraction operator (>>). Since normally when you receive an int value you can read until a newline character or enter chart is hit by a user, and in my case the new line character isn't received much later on the same line, I don't know how I'm going to get it to stop reading.

I would like to read until each ':' is hit and all preceding data will be stored into a variable of the corresponding type.


What I've thought of for the design is:

using a loop to read each field until a newline character is hit
while the newline character isn't reached, store field preceding the ":" into an appropriated type variable, once I have stored all variables and hit the newline character, I want to take the variables and store them into an object I have created. Once the variables are stored into one object I would just like to reuse these values, writing over them, and then do the same process once the newline character is reached.

If someone could clarify to me how to read in integer values and stop at a given delimiter that would be great. I know that for chars I could use either get or getline, but are there any functions that exist like that for integers?

Thank you and if any clarification is needed for my question please let me know.


http://en.cppreference.com/w/cpp/string/basic_string/getline

That's what you're looking for.

If you want to try somthing more advanced, you can try memcpy (http://en.cppreference.com/w/cpp/string/byte/memcpy). The advantage to this is that you don't need a delimiter, because you have a garunteed size. Note that you would only be able to memcpy plain old data (POD), which means only numbers and characters. The advantage still exists, though. What this allows the programmer to do, is to specify the length of a list of objects (for example, a string, or a list of objects) before the object being read. I have actually begun to use this technique for vectors to eliminate the need to write a delimiter, and have been able to completely remove the use of delimiters in my files with the exception of strings (which really doesn't matter, because context is eliminated). I can nest lists in a file without having to worry about delimiter collisions, aka: what if we have a 2D array, and the first array is empty? We hit the delimiter right away, and nothing more is read. When we know the size before we read, we can move on to reading each element (or not), knowing how many elements we're reading.

Anyway, you just in the beginnings, so feel free not to try it. :) It's probably somthing you might want to think about doing once you've mastered delimiters.
Thank you so much! Sorry for the late response!
Post your code so we can see what's up.
@Momothegreat

Thanks for all of the help, but I actually got this solved long ago and I forgot to mark this as solved. However, if you're any good with recursion, I just posted a new question that I could really use help with (code is already included!)

Thank you!
Topic archived. No new replies allowed.