Jump to content

Employee management system storage in C

goatedpenguin

Hi guys I am making a bank management system program in C that will have the ability to transfer Money to the account, create an account, check the amount and have a login. Now I need to be able to store data and create multiple accounts and I am assuming I going to need some sort of text or json file to do this. How do I create a file that can store all these nuanced details and can be deleted in C and also how do I format the file in such a way where my code can read the data? Thanks in advance 🙂  

 

P.S this is a terminal based program. 

Link to comment
Share on other sites

Link to post
Share on other sites

Yeah, json would be the most ideal or some csv if you are more used to the table format used in sql database. 

 

For both json or table format, I would used a library. Too much work to write your own serialization and deserialization parser. 

Sudo make me a sandwich 

Link to comment
Share on other sites

Link to post
Share on other sites

presume this is for a school project, and not anything that will actually be used in production?

if so, i presume using some out the box json library is out the question too.

 

i'm a bit rusty on the details for C, but i'd suggest the following structure:

- main storage file:

=> first line is the number of accounts in the system

=> all the lines after that are unique names for each known account.

 

- then have a file per account, that is named the unique name.

=> each line within the file is then a specific datapoint you want to store.

 

you can then read or write on specific lines in specific files to get to your data.

Link to comment
Share on other sites

Link to post
Share on other sites

48 minutes ago, goatedpenguin said:

Hi guys I am making a bank management system program in C that will have the ability to transfer Money to the account, create an account, check the amount and have a login. Now I need to be able to store data and create multiple accounts and I am assuming I going to need some sort of text or json file to do this. How do I create a file that can store all these nuanced details and can be deleted in C and also how do I format the file in such a way where my code can read the data? Thanks in advance 🙂  

 

P.S this is a terminal based program. 

It's quite easy to write small data sets just in a text file, load it in an array and make simple dichotomic search functions to retrieve data for a given index (name, account..)

No details as my latest C program dates back from ...1991 😄

 

System : AMD R9 5900X / Gigabyte X570 AORUS PRO/ 2x16GB Corsair Vengeance 3600CL18 ASUS TUF Gaming AMD Radeon RX 7900 XTX OC Edition GPU/ Phanteks P600S case /  Eisbaer 280mm AIO (with 2xArctic P14 fans) / 2TB Crucial T500  NVme + 2TB WD SN850 NVme + 4TB Toshiba X300 HDD drives/ Corsair RM850x PSU/  Alienware AW3420DW 34" 120Hz 3440x1440p monitor / Logitech G915TKL keyboard (wireless) / Logitech G PRO X Superlight mouse / Audeze Maxwell headphones

Link to comment
Share on other sites

Link to post
Share on other sites

17 minutes ago, manikyath said:

presume this is for a school project, and not anything that will actually be used in production?

if so, i presume using some out the box json library is out the question too.

This is not really a school project... I am learning C through making projects and I already have made a couple of small games before. So now that I know I need to use a json file how do I set this up ie what header files do I need and how do I make the program "read" the json file. A small example would be appreciated 🙂 

Link to comment
Share on other sites

Link to post
Share on other sites

5 minutes ago, goatedpenguin said:

So now that I know I need to use a json file how do I set this up ie what header files do I need and how do I make the program "read" the json file. A small example would be appreciated 🙂 

That will depend on the library you end up using. Take a look here: https://www.json.org/json-en.html

 

If you scroll to the bottom, you'll find a bunch of libraries for various programming languages, including C. Most of them should contain setup instructions and (hopefully) examples how you can use the library in your own code.

 

This one looks somewhat promising: https://jansson.readthedocs.io/en/latest/

Remember to either quote or @mention others, so they are notified of your reply

Link to comment
Share on other sites

Link to post
Share on other sites

After a busy day I managed to install jansson on windows which was a nightmare and editing the include path for the json file in vscode. After editing the workspace setting json file there were no squiggly lines however I wanted to test everything was working fine, but I got an error: 
 

cd "C:\Users\myusername\Proton Drive\my_email\Other computers\my_other_computer\projects\C_programming_projects\bank_management_system\" && gcc bank_manage_sys.c -o bank_manage_sys && "C:\Users\my_name\Proton Drive\my_email\Other computers\my other computer name\projects\C_programming_projects\bank_management_system\"bank_manage_sys
bank_manage_sys.c:3:21: fatal error: jansson.h: No such file or directory
 #include <jansson.h>
                     ^
compilation terminated.
 
[Done] exited with code=1 in 0.064 seconds
 
here is the code I ran for testing if the header file was working correctly:
 
#include <stdio.h>
#include <string.h>
#include <jansson.h>

int main(){
    return 0;
}

here is my workspace settings which have the include path for jansson:

 

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**",
                "C:\\jansson-2.9\\include" 
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "compilerPath": "C:\\MinGW\\bin\\gcc.exe",
            "cStandard": "c11",
            "cppStandard": "gnu++14",
            "intelliSenseMode": "windows-gcc-x86"
        }
    ],
    "version": 4
}

Thanks in advance 🙂 

Link to comment
Share on other sites

Link to post
Share on other sites

Alright I managed solve this issue by adding the path to my env vars with the commands:

 

setx C_INCLUDE_PATH "C:\MinGW\include\jansson-2.9\include"

setx LIBRARY_PATH "C:\MinGW\include\jansson-2.9\lib\"

 

Very troublesome hahaha 😅

Link to comment
Share on other sites

Link to post
Share on other sites

Hey guys I am not rlly sure how to use jsson and the documentation is not all that clear since I am a complete noob at this. Can someone tell me where to start from? 

 

Thanks in advance 🙂 

Link to comment
Share on other sites

Link to post
Share on other sites

19 hours ago, goatedpenguin said:

Hey guys I am not rlly sure how to use jsson and the documentation is not all that clear since I am a complete noob at this. Can someone tell me where to start from? 

 

Thanks in advance 🙂 

https://jansson.readthedocs.io/en/latest/tutorial.html#the-program

 

Everything is pretty much self explanatory by reading the code example in their doc.


json_t *root;
json_error_t error;

root = json_loads(text, 0, &error);
free(text);

if(!root)
{
    fprintf(stderr, "error: on line %d: %s\n", error.line, error.text);
    return 1;
}

Text is the string content of file which you will load json from. Check out fread for how to read from a text file. The reverse of json_load is json_dumps which you will need to use for writting json to a file. I get all of these by simply reading the documentation btw. 

 

Edit: you can use json_loadf and json_dumpf which directly read and write to a file for you. These are a lot less boiler plate. 

 

if you have trouble understanding what the above means, I can only suggest you brush up on the C fundamentals. The book The C programming language is a favorite among the community.  

Sudo make me a sandwich 

Link to comment
Share on other sites

Link to post
Share on other sites

May want to use a SQLITE database and just call the functions from your C program.

 

Otherwise, if it's just for learning or practicing, make your own file format instead of complicating your life with json.

 

for example a simple format ... serialize the data, then use 2-4 bytes for length or serialized data, then dump the serialized data.   

 

bencoding (used in torrent files) would be a nice simple format of serialization you could practice making a reader and write for : https://en.wikipedia.org/wiki/Bencode

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×