473,406 Members | 2,439 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Saving info to an array, and then to a file

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
2 1705
timon
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
timon
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

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

Similar topics

4
by: Michael Kennedy [UB] | last post by:
Hi Everyone, I have this multithreaded C# windows forms application which does a lot of image processing. Occasionally, I get the following error: A generic error occurred in GDI+....
3
by: andy_ro | last post by:
Hi group, I have an web application where the user can upload a pdf file. This file is stored in a table, in a column of type ntext. The user can later request the content of this column, and...
1
by: Mr. B | last post by:
VB.net 2003 c/w Framework 1.1 and MS Access db We have a commercial program that does our Acounting and Time Sheets (Timberline). At least once a day our Accounting department runs a Script...
3
by: JamesB | last post by:
I am saving image files from my website using the downloaddata on the web client. This gives me a byte array, which I am then saving to a file with the following code: ' Create the new, empty...
12
by: tjonsek | last post by:
I get a generic error (not very helpful) when attempting to save a re-sized image back to its original location. Here is the code snippet: Dim g As System.Drawing.Image =...
5
by: TheGanjaMan | last post by:
Hi everyone, I'm trying to write up a simple image stamper application that stamps the Exif date information from the jpegs that I've taken from my digital camera and saves the new file with the...
6
by: Jeff | last post by:
Hey (and thank you for reading my post) In visual web developer 2005 express edition I've created a simple website project.. At this website I want users who register to be able to upload a...
8
by: Yu SONG | last post by:
Hi all, What would be the most efficient way to save an array of floats to a file (in text format)? At the moment, my code looks like: /* * Saving an array of floats to a file
7
by: Dave Kelly | last post by:
There has to be a name for what I want to do and I don't know what words to google for. I have a form here: http://www.texasflyfishers.org/firstpage.htm I want to submit it to the server and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.