CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    May 2006

    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?


  2. #2
    Join Date
    Jan 2006

    Re: Any good resources deadling with simple file input?

    Read from the file to populate some kind of data structure, then selectively extract from that data structure in memory.

    Alternatively, use an existing database engine, e.g., SQLite.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Jan 2004
    Düsseldorf, Germany

    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

    Please read Information on posting before posting, especially the info on using [code] tags.

  4. #4
    Join Date
    May 2006

    Re: Any good resources deadling with simple file input?

    Read from the file to populate the program data structures, do not read keep reading from the file directly. Thanks, I think I got it, I will work with that and see what I can come up with.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)