Any good resources deadling with simple file input?
Im trying to make a program that reads input from a file and outputs it on to the screen. For the sake of example, lets assume that I'm making a weekly planner program. I would like to have the user input daily tasks into a text file (probably via a vector of strings). The program will then save this data and ask the user if he would like to view his tasks, and, if so, which day would he like to see. If the use enters, "Tuesday" the program will output only tasks that have been entered for Tuesday.
The problem I'm running into is that in all of the tutorials I have found so far, the text file data is inputted wholesale and not selectively into the program. That is, the tutorial sample program opens the file stream and begins reading the data from the beginning of the text file, one line at a time until the end of the text is reached. Is there a way to be more selective with the data that is inputted (i.e. inputting only the text data for "Tuesday" or for "Thursday" rather than the entire text file). Or, alternatively, do you know of a website that has a tutorial on dealing with this issue?
Re: Any good resources deadling with simple file input?
To make one thing clear: Your programm cannot know, where the "Tuesday" section starts without reading the file from beginning until it finds Tuesday. So to read only the Tuesday section, you have to:
Start reading the file line by line from the beginning
Throw away everything before the "Tuesday"-tag
Store the entries after the "Tuesday"-tag until you encounter the next tag ("Wednesday" ?)
Stop reading after encountering the next tag (or EOF)
However, as lazerlight already pointed out, this would be odd. It's a huge disadvantage if you have to read the file again to see Monday's tasks. Better to read the whole file and store all data in memory structures. For start you can have an array of seven std::vector<std::string>, one for each day. Or you can use a map<string, vector<string> > that maps the day to the task list. Then have your program just print the data from the memory structures.
More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf
Premature optimization is the root of all evil --Donald E. Knuth