By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,950 Members | 989 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,950 IT Pros & Developers. It's quick & easy.

Saving info to an array, and then to a file

P: 8
I need to save the information from hotel rooms to an array. This array then needs to be saved to a file and then accessed again. Could anyone give me any tips on how to do this?

Expand|Select|Wrap|Line Numbers
  1. #include    <conio.h>        /*Console I/O Header*/
  2. #include    <stdlib.h>        /*Standard Library Header*/
  3. #include    <stdio.h>        /*Standard I/O Header*/
  4. #include    <ctype.h>        /*C Library*/
  5. #include    <string.h>        /*String Header*/
  6.  
  7.  
  8. #define FALSE 0
  9. #define TRUE 1
  10. #define MAX_ROOMS 9
  11.  
  12.  
  13. struct room_details
  14. {
  15.    char room,   
  16.         type,
  17.         ensuite,
  18.         seaview,
  19.         balcony,
  20.         available;
  21.    int cost;
  22. };
  23.  
  24.  
  25. int Initialise_File(void);
  26. void Menu(void);
  27. void Display_Rooms(void);
  28. void Browse_Rooms(void);
  29. void Edit_Rooms(void);
  30. void Update_Rooms(void);
  31. void Delete_Rooms(void);
  32. void Add_Rooms(void);
  33. void Reload_System(void);
  34. void Terminate(void);
  35. void Append(void);
  36.  
  37. char NextChar;    /*Operators Selection*/
  38. char End;        /*End Program*/
  39. char Input;
  40.  
  41. FILE *room_info;
  42.  
  43. int Room [MAX_ROOMS];
  44.  
  45. struct room_details roomdata;
  46.  
  47.  
  48. void main (void)
  49. {
  50.     int file_ok; /*1=ok, 0=problems*/
  51.  
  52.     file_ok = Initialise_File();
  53.  
  54.     if (file_ok)
  55.     {
  56.         do
  57.         {
  58.             Menu();
  59.             system("cls");
  60.  
  61.             switch (NextChar)
  62.             {
  63.                 case '1' :;
  64.                     Display_Rooms();
  65.                     break;
  66.                 case '2' :;
  67.                     Browse_Rooms();
  68.                     break;
  69.                 case '3' :;
  70.                     Edit_Rooms();
  71.                     break;
  72.                 case '4' : printf("\n\nRELOAD SYSTEM");
  73.                     Reload_System();
  74.                     break;
  75.                 case '5' :;
  76.                     Terminate();
  77.                     break;
  78.                 default : printf("Unrecognised selection");
  79.                     getch();
  80.             } 
  81.         } while (NextChar != '5');
  82.     }
  83.     else
  84.         printf("\n\nProgram Terminated");
  85. }
  86.  
  87.  
  88. int Initialise_File(void)
  89. {
  90.     int ok;
  91.  
  92.     ok = TRUE;
  93.  
  94.     if ((room_info = fopen ("J:\\City of Bristol\\Intro to Programming\\Assignment 3\\Testing\\test.txt","w+b")) == NULL)
  95.     {
  96.         printf("Unable to open");
  97.         ok = FALSE;
  98.     }
  99.     return(ok);
  100. }
  101.  
  102.  
  103. void Menu (void)
  104. {
  105.     system("cls");
  106.     printf("\nTHE CLEARVIEW HOTEL");
  107.     printf("\n\nPlease select one of the following...");
  108.     printf("\n\n[1] Display Room Availability");
  109.     printf("\n\n[2] Browse Rooms");
  110.     printf("\n\n[3] Edit Rooms");
  111.     printf("\n\n[4] Reload System");
  112.  
  113.     NextChar = getch();
  114. }    
  115.  
  116.  
  117. void Display_Rooms (void)
  118. {
  119.     printf("Room No: %d", roomdata.room);
  120.     printf("\nWEEKEND");
  121.     printf("\nSat");
  122.     printf("\nSun");
  123.     printf("\nWEEKDAYS");
  124.     printf("\nMon");
  125.     printf("\nTues");
  126.     printf("\nWed");
  127.     printf("\nThurs");
  128.     printf("\nFri");
  129.     getch();
  130.  
  131. }
  132.  
  133.  
  134. void Browse_Rooms (void)
  135. {
  136.     int data,
  137.     number_read;
  138.  
  139.     rewind(room_info);
  140.  
  141.     data=fread(&number_read, sizeof(struct room_details),1,room_info);
  142.     while (data)
  143.     {
  144.         printf("\n%d", number_read);
  145.         data=fread(&number_read, sizeof(struct room_details),1,room_info);
  146.  
  147.         printf("Room Number: %d", roomdata.room);
  148.     }
  149.     getche();
  150. }
  151.  
  152.  
  153. void Edit_Rooms (void)
  154. {
  155.     system("cls");
  156.     printf("\n\nPlease select one of the following...");
  157.     printf("\n\n[1] Update Rooms");
  158.     printf("\n\n[2] Delete Rooms");
  159.     printf("\n\n[3] Add Rooms");
  160.     printf("\n\n[4] Main Menu");
  161.  
  162.     NextChar = getch();
  163.  
  164.     system("cls");
  165.  
  166.     switch (NextChar)
  167.  
  168.         {
  169.             case '1' :;
  170.                 Update_Rooms();
  171.                 break;
  172.             case '2' :;
  173.                 Delete_Rooms();
  174.                 break;
  175.             case '3' :;
  176.                 Add_Rooms();
  177.                 break;
  178.             case '4' :;
  179.                 Menu();
  180.             default : printf("Unrecognised selection");
  181.                 getch();
  182.  
  183.         }
  184. }
  185.  
  186.  
  187. void Update_Rooms (void)
  188. {
  189.     system("cls");
  190.     printf("\nUPDATE ROOM DATA: Press # to return to menu.\n\n");
  191.     printf("Please enter the room you wish to update...");
  192. }
  193.  
  194.  
  195. void Delete_Rooms (void)
  196. {
  197.     system("cls");
  198.     getch();
  199.     Edit_Rooms();
  200. }
  201.  
  202. void Add_Rooms (void)
  203. {
  204.     char input;
  205.     int room_count;
  206.     system("cls");
  207.  
  208.  
  209.     do
  210.     {
  211.         for (room_count = 1; room_count <= MAX_ROOMS; room_count++);
  212.         {
  213.             printf("Room Number: ");
  214.             roomdata.room = getche();
  215.  
  216.             printf("\nType: ");
  217.             roomdata.type = getche();
  218.             printf("\nCost/Night: ");    
  219.             scanf("%d", &roomdata.cost);
  220.             printf("\nFACILITIES:\nEn-Suite: ");
  221.             roomdata.ensuite = getche();
  222.             printf("\nSea View: ");
  223.             roomdata.seaview = getche();
  224.             printf("\nBalcony: ");
  225.             roomdata.balcony = getche();
  226.             printf("\nOut of Use: ");
  227.             roomdata.available = getche();
  228.             fseek(room_info,sizeof(struct room_details)*input-1,SEEK_SET);
  229.             fwrite(&roomdata, sizeof(struct room_details),1,room_info);
  230.             printf("Please press '.' to exit");
  231.             input = getch();
  232.             room_count++;
  233.         } 
  234.     } while (input != '.');
  235.     Reload_System();
  236. }
  237.  
  238.  
  239. void Reload_System (void)
  240. {
  241.     system("cls");
  242.     fclose(room_info);
  243.     Append();
  244.  
  245. }
  246.  
  247.  
  248. void Terminate (void)
  249. {
  250.     fclose(room_info);
  251.     system("cls");
  252.     getch();
  253. }
  254.  
  255.  
  256. void Append (void)
  257. {
  258.     ((room_info = fopen ("J:\\City of Bristol\\Intro to Programming\\Assignment 3\\Testing\\test.txt","ab")) == NULL);
  259.     Menu();
  260. }
  261.  
Thats as far as I've got before I've become stuck. Some bits of code in there are experimental and others have no purpose as of yet.

Any help on sorting this out would be welcome...

Thanks
Jun 13 '07 #1
Share this Question
Share on Google+
2 Replies


P: 6
well,
there diferents to do that.
you can use fstream or
A)
Expand|Select|Wrap|Line Numbers
  1. #include <fstream>
  2. //load
  3. ifstream flu;
  4. flu.open ("myfile.txt");
  5. if (flu) {
  6.           flu.getline (myString, 100, '\n'); 
  7.             //charVector, max char, char to end->you can use '#' for example
  8.            //to separate differents fields
  9.  
  10. }
  11. else {throw error(noFlujo);}
  12. flu.close ();
  13.  
  14. //save
  15. oftream flu;
  16. flu.open ("myfile.txt");
  17. if (flu) {
  18.  
  19. }
  20.  
  21.  
  22.  
Jun 13 '07 #2

P: 6
well,
there diferents to do that.
you can use fstream (C++) or stdio (C)
A)
Expand|Select|Wrap|Line Numbers
  1. #include <fstream>
  2. //load
  3. ifstream flu;
  4. flu.open ("myfile.txt");
  5. if (flu) {
  6.           flu.getline (myString, 100, '\n'); 
  7.             //charVector, max char, char to end->you can use '#' for example
  8.            //to separate differents fields
  9.  
  10. }
  11. else {throw error(noFlujo);}
  12. flu.close ();
  13.  
  14. //save
  15. oftream flu;
  16. flu.open ("myfile.txt");
  17. if (flu) {
  18.     flu<<"inicio"<<endl<<myString<<endl<<'#'<<field2<<"...";
  19. }
  20.  
  21. //B)
  22. #include <stdio>
  23. //load
  24.             char bloq[10]
  25.     FILE *canal;
  26.     canal=fopen ("myfile.dat", "rb"); //there's diferent modes
  27.             fread(&bloq, 10, 1, canal);
  28.     fclose(canal);
  29. //save
  30.      canal=fopen("myfile.dat", "wb");
  31.      fwrite(&bloq, 10, 1, canal);
  32.  
  33.  
i dont´t know what you mean with "saving info in an array"
Jun 13 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.