0

I am a beginner in visual studio and has only code C and C++ in command line settings. Currently, I am taking a module(software development) which requires me to come up with an expense tracker - a program which helps user tracks his/her daily expenses. Therefore, at the end of each individual day, or after a user uses finishes the program, we would have to perform data storage to store all the info in one place which we would export it during the next usage.

My constraint include not using any relational database(although i have no idea what it is :( ). Data storage must be done using XML or text files. Following this, I have several questions regarding data storage:

1) If data is stored successfully, do we export it everytime we start the program? And everytime after the user closes the program, we overwrite the existing data file and then store it accordingly?

2) I have heard from some people that using text file may be easier. Searching on the internet and library only provides me with information regarding XML and not text. Would anyone be able to help me with it? Like tutorials link and stuff?

Thank you very much!

user2114036
  • 153
  • 3
  • 13
  • XML is just a text file with formatting rules. But why not use a spreadsheet? Excel and others enable one to have forms on it. Seems a lot easier than writing lots of functionality from scratch. – Ed Heal Mar 05 '13 at 07:14
  • Hi, I think my module restricts us to just text or xml files. Howevr, do u have links on how to integrate excel into visual studio c++? We are actually interested in generating graphs through excel – user2114036 Mar 05 '13 at 08:08
  • Write output to XML: http://blogs.msdn.com/b/brian_jones/archive/2005/06/27/433152.aspx Excel can then read those files that you create and generate the Graphs. However Excel has an excellent mechanism to generate forms/graphs and is quite an easy tool to do things like expense forms. – Ed Heal Mar 05 '13 at 08:21

2 Answers2

0

File writing/handling works similar to every other buffer in c++.

you can enable file handling using the fstream header. You can create a file, write to it and over-write every time the program is run, or can even create a file the first time the program is run and then append to it every subsequent time the program runs.

Ive only ever done text files, never tried XML, but Im guessing they're similar.

http://www.cplusplus.com/doc/tutorial/files/ should give you everything you need to know.

  • Hi, I have learnt simple creating/writing/closing of files. For instance, I do know how to write few lines of strings and stuff. However, in my project, I probably have to store information like name, price, date of an item which a user just bought. Then this information will increase as the user continues to use the program. So do i say at the beginning whenever i run the program, I have to read in all these information? (I hope im clear :( ) – user2114036 Mar 05 '13 at 07:23
  • all that information can be stored as strings, depending on how you want to do it. each field can be line delimited, space delimted etc, the only thing that will change will be the parser you write to read the data later. once the information is stored in the file, you shouldn't NEED to read in the information. depending on what your program is trying to do. for example, if all your program wants to do is store information for archiving/posterity, you don't need to read the data after you've written it. but say you were to graph data from the file you would. Does that answer your question? – Karmanya Aggarwal Mar 06 '13 at 05:33
0

Your choice of XML vs plain text depends on the kind of data that you'll be storing. The reason why you'll only find XML libraries on the internet is because XML is a lot more complicated than plain text. If you don't know what XML is or if the data that you're storing isn't very complex, then I would suggest going with plain text.

For example, to track expenses, you might store a file like this:

sandwich 5.00
coffee 2.30
soft drink 1.50
...

It's very easy to read/write lines like this to/from a file in C++.

TianyuZhu
  • 752
  • 6
  • 8
  • Hi, actually its slightly more complicated. For instance, I can have several users. These users have their own individual expenses. For expenses itself, we have different categories such as entertainment, food, clothes etc and each category will have their own individual item which contains names, price, date, payment mode etc. And in each payment mode, there will be corresponding payment account. Yep, its slightly more complicated so may I know how to store the data? Like if I want to store them according to their categories type? Thanks! – user2114036 Mar 05 '13 at 07:33
  • You can add more columns to the data. Above I have one column for the description (sandwich, coffee, etc) and one column for the price. You can add other columns like category or date. Then you can read them all in at the beginning of your program, and when the user wants to see all expenses under a certain category, just go through your list and show them every item that belongs in the category. – TianyuZhu Mar 05 '13 at 14:10