G+_Joe Maruschek Posted March 24, 2014 Share Posted March 24, 2014 I was inspired by +Dean T and his learning Japanese program, so I learned how to read data from a file in order to load it as a set of flash cards. Now you don't have to hard-code all the data in your program. You just load the data from a text file. The program then will randomly ask you a "question" and you have to type an "answer". My program loads its data from a file called "cards.txt" that needs to be in the same directory as the program. Visual Studio will copy the file for you if you add it to your project and set its properties correctly. You can add the cards.txt file from my github project, which is a copy of all the Japanese verbs from +Dean T. If you click on the cards.txt file in your Solution Explorer, you should see some properties appear for it in the lower right corner. Check to make sure the build action is set to "content" and the "copy to output" option is set to "Copy always". The format of the cards.txt file is very simple. The first "question" is on line 1, the "answer" is on line 2, the next "question" is on line 3, its answer is on line 4, and so on. Hopefully this is generic enough to allow you to put in math problems, or vocabulary words, or dates, or whatever you need to learn. I hope you can use the code in your own programs. https://github.com/joemarus/FlashCards Link to comment Share on other sites More sharing options...
G+_Dean T Posted March 24, 2014 Share Posted March 24, 2014 Very nice idea! Due to the way I study I need to be able to grab a certain 10 at a time which was the way I coded my version. However for a course of mine I am interested in making a visual application to go with each section and see if I can get it integrated with the course depending on its quality and your idea makes it much more easier to import each lessons vocabulary over and keep the code the same. Thats depending on if the codes just as simple for using a text file with windows forms or wpf formats. Link to comment Share on other sites More sharing options...
G+_Dean T Posted March 24, 2014 Share Posted March 24, 2014 Thinking more on my need for being able to pull only 10 at a time I could always set more than 1 streamreader and have them set to cards1.txt cards2.txt cards3.txt etc with what I need in each one and could then edit them as needed too. Certainly like the idea that all students could use one program and only need to change the text file depending on what lesson they are doing. Makes it easy to change the subject without giving the source code away. Link to comment Share on other sites More sharing options...
G+_Dean T Posted March 26, 2014 Share Posted March 26, 2014 Ive made a gui app now but couldnt seem to get streamreader to work with it. I could set up the streamreader, it compiled however just crashed as it would load. Link to comment Share on other sites More sharing options...
G+_Joe Maruschek Posted March 27, 2014 Author Share Posted March 27, 2014 If you don't mind, I'd like to take a look at the version with the streamreader in it, if you still have it. Link to comment Share on other sites More sharing options...
G+_Dean T Posted March 27, 2014 Share Posted March 27, 2014 I dont but it wont take too long to change the current code so il do it now Link to comment Share on other sites More sharing options...
G+_Dean T Posted March 27, 2014 Share Posted March 27, 2014 Joe Maruschek - So I went back to it and did it a different way and got it to work. The problem was that I hadnt put the cards.txt file in the debug folder... Anyway I think youll like the way I went about this. I used the streamreader in a method to return the question and answer when the program starts and when the button is clicked. I then used the streamreader along side a string builder so that the Verbs button always shows the verbs in cards.txt. My next idea is to add a load option so that when a user loads a text file it updates cards.txt to what ever name they load so they can have multiple files without the need to change them. But along side that I wish I could also have it update concepts and exercises too so it was an all in one file and I could have one application with multiple lessons that can be loaded. No idea how to proceed but I will get looking. Here is the new code - Ive added a thanks in the about box to you https://www.dropbox.com/s/h1rmmsbzcl2t0ro/JapaneseStreamReader.zip Link to comment Share on other sites More sharing options...
G+_Dean T Posted March 27, 2014 Share Posted March 27, 2014 Got it! Multiple text files (cards, concepts and exercises) that are inside a single zip file. Then have the zip file in the folder location and get the needed text using stream reader along with the ability to look within the zip file. Link to comment Share on other sites More sharing options...
G+_Nevrin O Posted March 29, 2014 Share Posted March 29, 2014 Dean T Hey seen your code and thought of a quick change that you could use to have multiple data set .txt files https://github.com/NevrinO/Coding.git just added a global variable and replaced your StreamReader sr = new StreamReader("cards.txt"); with StreamReader sr = new StreamReader(dataSet); also added a text box and a button to test it. it works fine if you know the names of the files you want to use, though you will need to add some code to clear the current question but it might get you headed in the direction you want. Link to comment Share on other sites More sharing options...
G+_Dean T Posted March 29, 2014 Share Posted March 29, 2014 Thank you! I am actually going to use that but adding some additional code. I am going to add a button that opens a file browser window and lts you chose the the file and it takes the file name rather than having to type it and add txt. But then the rest of the code such as the global variable will work in the same way. Then following from that I will be moving on to loading multiple files from a zip file by electing the zip file, extracting the files (text files) and loading them and then deleting the files on exit. Just need to sit down and start. Link to comment Share on other sites More sharing options...
G+_Joe Maruschek Posted March 29, 2014 Author Share Posted March 29, 2014 Dean T I was thinking that one way to keep those multiple parts of a lesson together is to just put them all in one file. There is no rule that says you need to only have verbs in one file and concepts in another. You could bundle them all together in one file, as long as your program knows how to pick them out the of the file. For example, you could start off the file with, say, your concepts for the lesson. You could have your program read lines from the file until it got to a special code that told you it is the end of the concept section. Like you could say "-end-" is your special marker. That's a good place to use a while() loop. Then without closing the file, you can keep reading more lines that, say, represent exercises. You could read lines until you get to another "-end-" code. That would be another while() loop. Then you could have your verbs or vocabulary section next. It can end with a "-end-" code (or whatever you want to use). There could be another section if you want. You just have to make sure that you document the structure of the file so you know how to create one for each lesson or area that you need. Putting try() and *catch()* in your loading method is also a good thing while you are reading in case the file is not formed correctly and you try to read something that is not there. Link to comment Share on other sites More sharing options...
G+_Nevrin O Posted March 29, 2014 Share Posted March 29, 2014 glad I could help. I was also thinking along the lines Joe Maruschek was of wrapping it all into formatted files. I'm gonna play around with the code some more, who knows maybe this will get me back into learning Japanese. Link to comment Share on other sites More sharing options...
G+_Dean T Posted March 29, 2014 Share Posted March 29, 2014 Joe Maruschek That is such a good idea. Similar to the Split method of string. That saves me SO much unneeded coding! Link to comment Share on other sites More sharing options...
G+_Nevrin O Posted March 30, 2014 Share Posted March 30, 2014 Couldn't help myself, made more change to your code following Joe's suggestion. Its missing the try()/catch() part and could definitely be better optimized but it runs. also changed it so that it only needs to read the file once instead of every time it asks a question. https://github.com/NevrinO/Coding.git Link to comment Share on other sites More sharing options...
G+_Dean T Posted March 30, 2014 Share Posted March 30, 2014 Great I was thinking of how I could get it to load everything rather than read from the file every time. I dont mean to be a pain but any chance you could comment the code as being a complete noob theres some stuff I dont get such as how it knows how to seperate between cards, exercises concepts etc. Im assuming it reads the file until it gets to -end- and then changes to instructions and reads until it gets to -end- and then changes to concepts but Im not sure as I am totally guessing here. Link to comment Share on other sites More sharing options...
G+_Dean T Posted March 30, 2014 Share Posted March 30, 2014 I had a few issues but after a little hack around and using a lot of those code examples I got it working from a load fil and created a full lesson file for japansethroughanime.com. I am now doing some clean up and final prettyness and I will be making it available for all JTA students. I have put a thank you in the about section for both of you. I actually have a program that can be distributed and used. People can actually get a use from this. When I first started c101 a month ago I didnt think I would see this happening for about a year! Thanks guys! Link to comment Share on other sites More sharing options...
G+_Nevrin O Posted April 3, 2014 Share Posted April 3, 2014 glad you got it to work. Sorry about the no comments thing its something i should do while i write but i tend to get the code to work first then go back an comment. and it is no bother this is a place for people to learn about coding and commenting is key to making code readable to both experts and beginners. Ill go back and comment on my code when i get off work and yes your guess is correct it. the way i set it up was it reads instructions until the first end then it reads concepts until an end then exercises until the next end and then after that it reads the cards. just fyi there are better ways of doing this. this doesn't care what data you put between the end markers so it is very important to structure your file correctly in this case. Link to comment Share on other sites More sharing options...
Recommended Posts