Guest User

Untitled

a guest
Mar 23rd, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. struct Account {
  2. string holder_name;
  3. string holder_password;
  4. double current_sum;
  5. };
  6.  
  7. int save_to_file(Account * account);
  8. Account * load_from_file(string holder_name);
  9.  
  10. int main(){
  11. Account account = { "BEN", "password", 200.0 };
  12. save_to_file(&account);
  13. load_from_file("BEN");
  14. return 0;
  15. }
  16.  
  17. int save_to_file(Account * account){
  18. const char * cstr = account->holder_name.c_str();
  19. int size = sizeof(struct Account);
  20.  
  21. FILE * fp = fopen(cstr, "wb");
  22. char * c;
  23.  
  24. c = (char *) account;
  25.  
  26. for(int i=0; i < size; i++)
  27. {
  28. putc(*c++, fp);
  29. }
  30.  
  31. return fclose(fp);
  32. }
  33.  
  34. Account * load_from_file(string holder_name)
  35. {
  36. FILE * fp;
  37. char *c;
  38. int i;
  39. int size = sizeof(struct Account);
  40. struct Account * ptr = (struct Account *) malloc(sizeof(struct Account));
  41.  
  42. if ((fp = fopen(holder_name.c_str(), "rb")) == NULL)
  43. {
  44. perror("Error occured while opening file");
  45. return NULL;
  46. }
  47.  
  48. c = (char *)ptr;
  49.  
  50. while ((i = getc(fp))!=EOF)
  51. {
  52. *c = i;
  53. c++;
  54. }
  55.  
  56. fclose(fp);
  57. return ptr;
  58. }
Add Comment
Please, Sign In to add comment