diff options
| author | Franklin Wei <git@fwei.tk> | 2015-05-12 19:59:44 -0400 |
|---|---|---|
| committer | Franklin Wei <git@fwei.tk> | 2015-05-12 19:59:44 -0400 |
| commit | b05123ca8b967a31cd074792ca920a3a74f61bc4 (patch) | |
| tree | 0053b98f2815243b881e477787a98326dbde1f0e /src/save.c | |
| parent | 9082eb2a10f935a23e140e0089fae26570081883 (diff) | |
| download | market-sim-b05123ca8b967a31cd074792ca920a3a74f61bc4.zip market-sim-b05123ca8b967a31cd074792ca920a3a74f61bc4.tar.gz market-sim-b05123ca8b967a31cd074792ca920a3a74f61bc4.tar.bz2 market-sim-b05123ca8b967a31cd074792ca920a3a74f61bc4.tar.xz | |
split into multiple files
Diffstat (limited to 'src/save.c')
| -rw-r--r-- | src/save.c | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/src/save.c b/src/save.c new file mode 100644 index 0000000..989ab60 --- /dev/null +++ b/src/save.c @@ -0,0 +1,37 @@ +#include "globals.h" + +/* NOTE: integers are represented internally by unsigned long long ints, but in the save they are always 64 bits */ + +void save_handler(struct player_t *player) +{ + printf("Enter the file to save your portfolio in: "); + + char buf[128]; + scanf("%127s", buf); + + printf("Writing data...\n"); + FILE *f = fopen(buf, "w"); + + const char *magic = "PORTv1"; + fwrite(magic, strlen(magic), 1, f); + + uint64_t be_cash = to_be64(player->cash.cents); + + fwrite(&be_cash, sizeof(be_cash), 1, f); + + for(uint i = 0; i < player->portfolio_len; ++i) + { + struct stock_t *stock = player->portfolio + i; + + uint64_t be_symlen = to_be64(strlen(stock->symbol)); + fwrite(&be_symlen, sizeof(be_symlen), 1, f); + fwrite(stock->symbol, strlen(stock->symbol) + 1, 1, f); + + uint64_t be_count = to_be64(stock->count); + fwrite(&be_count, sizeof(be_count), 1, f); + } + + fclose(f); + + printf("Done saving."); +} |