Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct Account {
- string holder_name;
- string holder_password;
- double current_sum;
- };
- int save_to_file(Account * account);
- Account * load_from_file(string holder_name);
- int main(){
- Account account = { "BEN", "password", 200.0 };
- save_to_file(&account);
- load_from_file("BEN");
- return 0;
- }
- int save_to_file(Account * account){
- const char * cstr = account->holder_name.c_str();
- int size = sizeof(struct Account);
- FILE * fp = fopen(cstr, "wb");
- char * c;
- c = (char *) account;
- for(int i=0; i < size; i++)
- {
- putc(*c++, fp);
- }
- return fclose(fp);
- }
- Account * load_from_file(string holder_name)
- {
- FILE * fp;
- char *c;
- int i;
- int size = sizeof(struct Account);
- struct Account * ptr = (struct Account *) malloc(sizeof(struct Account));
- if ((fp = fopen(holder_name.c_str(), "rb")) == NULL)
- {
- perror("Error occured while opening file");
- return NULL;
- }
- c = (char *)ptr;
- while ((i = getc(fp))!=EOF)
- {
- *c = i;
- c++;
- }
- fclose(fp);
- return ptr;
- }
Add Comment
Please, Sign In to add comment