Question

I'm making a program that emulates a ATM, so I want to save the account data in a .txt file, and then if the user wants to see the balance of his account search for the balance associated to his account number.

To accomplish this I was thinking to store all the information of one user in a single line and then search for specific bits of the string.

Let's say the format will be:

  1. accountnumber1&Password1&Balance1/
  2. accountnumber2&Password2&Balance2/
  3. accountnumber3&Password3&Balance3/

I was thinking to count the number of end of lines to know which user I'm working for example,

If accountnumber3 (located after two \n) wants to know his balance I would get the string between &.../ located in the string that comes after two \n

My doubts/questions are:

  • Is there a easier way to do this?
  • How do I search for each string (I was thinking strstr() but I don't know how to properly use it with a file like this).
  • How do I count the number of \n and then use that as reference to search within the string.
Was it helpful?

Solution

I believe you need to optimize the format of your txt file database. My recommendation is to create a property file, which would store the information as key pairs. You can use the account number as the key and rest of the information as the pair for it. For example:

accountnumber1=Password1&Balance1/

accountnumber2=Password2&Balance2/

accountnumber3=Password3&Balance3/

Using this appraoch, your retrieval will be quick. And you just have to split the values retrieved to get the pieces of the information.

Although i have used properties file in Java conveniently but I have never done so in C. Hence I googled to check whether it is possible to do so. And here are the results:

In this case Google gives (first 4 hits for C config file library):

http://www.hyperrealm.com/libconfig/

http://rudeserver.com/config/

http://freshmeat.net/projects/cfl/

http://liblcfg.carnivore.it/

Hope it helps!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top