I need some ideas for dealing with list data that would have more permanence and be stored and retrieved as needed instead of the program beginning with an empty list, data being added and manipulated and the program ending by freeing the data. Use code fragments to illustrate your response.© BrainMass Inc. brainmass.com March 22, 2019, 2:23 am ad1c9bdddf
My name is Jim. I hope I will answer any questions you have.
When you want to deal with list data that has more permanence and is stored and retrieved as needed instead of with a program beginning with an empty list, data being added and manipulated and the program ending by freeing the data, you have to interface with some type of storage mechanism, like a database table or a file of some kind.
If you think about blogs on the internet, you might not think of them as list data that's stored in a database, but that's exactly what they are. Each time someone writes another essay, or entry in their blog if you will, it's stored in some kind of non-volatile storage. In most cases it's store on the web host's hard drive. A Twitter feed is the same thing, a sequential list of small texts that, ultimately, has to be stored somewhere too.
The mechanical manner in which they are transferred from the hard drive to the screen, the way the programming language implements the display, typically involves reading a database table, taking one record at a time, manipulating it in whatever ways are necessary to display it the way it's intended to be, and then actually outputting it. In a less sophisticated implementation, it might just be a computer language that reads a sequential file and goes through the same procedures, of manipulating the record and ultimately displaying it.
That would look like this:
while ( getline (infile, line) )
It's just a simple loop that reads a line of text, calls the function "displayBlogPost" passing it the line of text it read. Then it goes back to the top of the loop, and reads another line of text. It will continue doing that until the end of file condition is raised. Then it stops executing that block of code and continues with whatever code is immediately below the closing curly brace.
Please understand that there would also have to be code that opens the file that "getline" is reading, initializes variables, and things like that. The function displayBlogPost would perform whatever processing is needed to actually get the post up on the screen. Taking Twitter as an example, it might need to parse the line of text to see if there's a picture that's supposed to be displayed as part of the post. If it finds one, it calls another function to retrieve the picture (which would also be stored on a hard drive somewhere), formats it as needed (it might need to make it smaller ...
This in-depth solution discusses basic ways to store a list of data into non-volatile memory, and to access it and alter it. It is about 1,500 words.