The contract called for creation of a random access database of plumbing shops w
ID: 652450 • Letter: T
Question
The contract called for creation of a random access database of plumbing shops within the near perimeter of FIU.Engineering school. The database features a rating number from 1-10 to offer a guideline to the quality of service providedby the companyand complete the code to read the company names from the pipes.bin database and print them to a text file. Once the outputtext file is created, it can be loaded into an editor and printed to be turned in as completion of this homework.
Create a CodeBlocks project to build a program to open the plumbing database and print each data block to a text file.
The database is located at http://web.eng.fiu.edu/watsonh/EEL2880/pipesDatabase.html
Download and save the binary database in the project folder you created for the program. When the program is compiledand executed from within CodeBlocks (Run), the default directory for the data file is where the main.cpp code is located.
The steps are as follows:
1. Create CodeBlocks project (C++ console application)
2. Download the binary database file to project folder
3. Copy the skeleton code to the project source file (main.cpp)
4. Fill in and complete the blanks for the source code
5. Compile and build the 'plumbing' program
6. Run the program: use the database as the input file (pipes.bin) and output to a text file
Macros:
Complete the lines of code to read the plumbing database using the following features:
Define in the program that there are three defined string lengths (lines 5-7)
STRSHORT = 15 chars
STRMED = 30 chars
STRLONG = 80 chars
Each block is location and contact information for a single plumbing company. Consider it similar to having a single indexcard for each company with the database being a collection of 10 cards.
The idea is to retrieve a single block from the database at a time and output all the information for each card to the outputtext file. Once all 10 blocks have been processed, all files can be closed. The output text file can be opened with an editor,printed, and turned in for the assignment.
Define data structure:
The structure for each company data block has the following members in the exact sequence (line 12):
char name [STRLONG];
char street [STRLONG];
char city [STRMED];
char state [STRMED];
char zip [STRSHORT];
char phone [STRSHORT];
char cell [STRSHORT];
int rating;
char contact [STRLONG];
Function Prototype:
One user defined function is in the program has to have a prototype (line 15) as follows:
void printInfo(company* info, FILE* fp);
Declare file pointers:
Inside the program main function, two file pointers need to be declared: Input database file pointer line 19 and Output textfile pointer line 20.
Declare data structure:
Declare an instance of the generic company data structure line 25 used to hold read file contents. Use the 'new' operatorcreating a block of memory returning a pointer to the block. The pointer is named and is used throughout the program toreference the structure.
company* info= new company;
Open input database file:
Lines 27-29 prompt the user for the input file name. A char array for the input file name needs to be declared before it isused. The array can be declared with a long string constant [STRLONG]. The declaration should be placed at line 22. Thatinput file name is then used on line 32 where the file is to be opened.
The 'if' epression should look like the following and specify read binary :
if ((file pointer = fopen (calling arguments)) == NULL)
Line 32 uses the fopen function http://www.acm.uiuc.edu/webmonkeys/book/c_guide/2.12.html#fopen .
Fill in the appropriate values used in the 'if' condition expression. Since the pipes database file is a binary database, thefopen specification should be to 'read binary' mode .
Open output text file:
Lines 38-40 prompt the user for the output text file name. A char array for the output file name needs to be declared beforeit is used. The array can be declared with a long string constant [STRLONG]. The declaration should be placed at line 23.That output file name is then used on line 43 where the file is to be opened.
The 'if' epression should again look like the following except now specify write text:
if ((file pointer = fopen (calling arguments)) == NULL)
Use the fopen function referenced above to open the output text file. Fill in the appropriate values used in the 'if' conditionexpression. Since the output text file is composed of strings, the fopen specification should be set to 'write text' mode.Line 48 sets up a loop to read each company information block one-at-a-time from the beginning of the file to the end.Each time a block is read, the contents are printed to the output text file.
Loop through the database:
First the database file needs to be positioned to the block corresponding to the current BlockNumber. In order to do that, thefile is positioned to the block using fseek using the offset.Line 51 is the 'if' statement useed for fseek to position the file. The rest of the 'if' statement detects a failed seek conditionand exits the program (lines 52-55).
Seek to data block:The specification for the fseek function (other than the text book) can be found at
http://www.acm.uiuc.edu/webmonkeys/book/c_guide/2.12.html#fseek
Fill in the appropriate values used in the 'if' condition expression.The 'if' expression should look like the following, but specify the condition expression
if ( (fseek( correct calling arguments ), != 0)
The input file stream returns the file pointer value when the the file is successfully opened (line 32). The fseek offset iscalculated from the beginning of the file, position 0 using the appropriate whence constant. The appropriate Block Numberoffset is computed as the BlockNumber*sizeof( company data structure ).
Read the database block:The specification for fread on line 57 function (other than the text book) can be found at
http://www.acm.uiuc.edu/webmonkeys/book/c_guide/2.12.html#fread
fread(information structure pointer, sizeof(information structure), Number of blocks to read, input file pointer)Fread has 4 calling arguments, see the web reference or the text book. The first calling argument is the name of a pointerfor the database information structure where the input data will be placed. The second calling argument has to specify howmany bytes are to be read into the structure. Rather than counting the number of bytes, the computer will do that for you byusing the function: sizeof( information structure). The third calling argument will be 1 (one) because only one block of datawill be read. The last calling argument is the name of the file pointer for the input stream (line 19).
Print information for each company:Call the user defined printInfo function line 60 with the appropriate calling arguments. Hint: both calling arguments are
pointers.End of loop through database
User defined printInfo function:
Fill in the calling arguments on line 65.Line 68 takes data from the company data structure to print company name to the output text file. Lines 69-76 need to befilled in with the corresponding statements. Print out the rest of the information from the company data structure. Refer tothe data structure declaration used on lines 10-13.
Once this program has been run, there should be a file in the project folder with the name given for the output file name.
Open that output text file with an editor and print its contents on paper to turn in as the completion of the assignment.
Good luck team!
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 // set string lengths
5 #define ??
6 #define ??
7 #define ??
8 using namespace std;
9 10 struct company
11 {
12 ?? add member elements for structure
13 };
14
15 ?? user defined function prototype to print information function
16
17 int main()
18 {
19 declare input file pointer ??
20 declare output file pointer ??
21 int BlockNumber;
22 char ??[STRLONG]; //input file name
23 char ??[STRLONG]; // output file name
24
25 ?? create instance of company information structure
26
27 printf("Enter input file name :");
28 scanf("%s", input file name );
29 fflush(stdin); /* flush keyboard buffer */
30
31 // open binary data file
32 if ( fill in the fopen expression to open the database file )
33 {
34 fprintf(stderr, "Error opening input file.");
35 exit(1);
36 }
37
38 printf("Enter output file name :");
39 scanf("%s", output file name );
40 fflush(stdin); /* flush keyboard buffer */
41
42 // open output text file
43 if ( fill in the fopen expression to open the output text file )
44 {
45 fprintf(stderr, "Error opening output file.");
46 exit(1);
47 }
48 for (BlockNumber=0; BlockNumber<10; BlockNumber++)
49 {
50 /* Move the position indicator to the specified element. */
51 if ( fill in the fseek expression to position to BlockNumber )
52 {
53 fprintf(stderr, " Error using fseek().");
54 exit(1);
55 }
56 /* Read in a single info. */
57 fread( fill in the fread expression to read data );
58
59 fprintf(op," Block %d ",BlockNumber); /add header text
60 printInfo (fill in correct calling arguments);
61 }
62 return 0;
63 }
64
65 void printInfo(fill in correct calling arguments)
66 {
67 // print all the info values
68 fprintf (fp,"Name: %s ",info->name);
69 fprintf ( fill in correct arguments );
70 fprintf ( fill in correct arguments );
71 fprintf ( fill in correct arguments );
72 fprintf ( fill in correct arguments );
73 fprintf ( fill in correct arguments );
74 fprintf ( fill in correct arguments );
75 fprintf ( fill in correct arguments );
76 fprintf ( fill in correct arguments );
77 return;
78 }
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// set string lengths
#define STRSHORT 15
#define STRMED 30
#define STRLONG 80
using namespace std;
struct company
{
char name [STRLONG];
char street [STRLONG];
char city [STRMED];
char state [STRMED];
char zip [STRSHORT];
char phone [STRSHORT];
char cell [STRSHORT];
int rating;
char contact [STRLONG];
};
void printInfo(company*, FILE**);
int main()
{
FILE *input;
FILE *output;
int BlockNumber;
char inFile[STRLONG]; //input file name
char outFile[STRLONG]; // output file name
company* info= new company;
printf("Enter input file name :");
scanf("%s", inFile );
fflush(stdin); /* flush keyboard buffer */
// open binary data file
if ((input = fopen(inFile, "r")) == NULL)
{
fprintf(stderr, "Error opening input file.");
exit(1);
}
printf("Enter output file name :");
scanf("%s", outFile);
fflush(stdin); /* flush keyboard buffer */
// open output text file
if ((output = fopen(outFile, "w")) == NULL)
{
fprintf(stderr, "Error opening output file.");
exit(1);
}
for (BlockNumber=0; BlockNumber<10; BlockNumber++)
{
/* Move the position indicator to the specified element. */
if (fseek( input, sizeof(company)*BlockNumber, SEEK_SET)) //using fseek from start by size of company structure
{
fprintf(stderr, " Error using fseek().");
exit(1);
}
/* Read in a single info. */
fread(info, sizeof(company), 1, input); //then reading from that position company structure data directly
fprintf(output," Block %d ",BlockNumber); //add header text into the output file
printInfo (info, &output); //printing the compnay info data read, back to the output file
}
return 0;
}
void printInfo(company* info, FILE** fp)
{
// print all the info values into the fp file pointer
fprintf (*fp,"Name: %s ",info->name);
fprintf (*fp,"Street: %s ", info->street);
fprintf (*fp,"City: %s ", info->city);
fprintf (*fp,"State: %s ", info->state);
fprintf (*fp,"Zip: %s ", info->zip);
fprintf (*fp,"Phone: %s ", info->phone);
fprintf (*fp,"Cell: %s ", info->cell);
fprintf (*fp,"Rating: %d ", info->rating);
fprintf (*fp,"Contact: %s ", info->contact);
return;
}
----------------------------------------------------------------------------------------------------------------
OUTPUT (reading from pipes.bin from : http://web.eng.fiu.edu/watsonh/EEL2880/pipesDatabase.html)
----------------------------------------------------------------------------------------------------------------
Block 0
Name: Marlin Plumbing
Street: 20145 Northeast 15th Place
City: Miami
State: FL
Zip: 33179
Phone: 305.652.3031
Cell: 305.665.8813
Rating: 5
Contact: Jerry
Block 1
Name: Florida Jetclean
Street: 18910 North 5th Avenue
City: Miami
State: FL
Zip: 33179
Phone: 800.226.8013
Cell: 305.884.2378
Rating: 3
Contact: Harry
Block 2
Name: Water Damage Services
Street: 1076 NE 196 St
City: Miami
State: FL
Zip: 33179
Phone: 866.651.4297
Cell: 786.555.1234
Rating: 5
Contact: James
Block 3
Name: Freddies Plumbing Corporation
Street: 1301 Northease 199th Street
City: Miami
State: FL
Zip: 33179
Phone: 305.651.2983
Cell: 786.290.6145
Rating: 7
Contact: Freddy
Block 4
Name: Lawrence Plumbing Supply
Street: 20000 Northeast 15th Court
City: Miami
State: FL
Zip: 33179
Phone: 350.652.4191
Cell: 305.914.6521
Rating: 4
Contact: Larry
Block 5
Name: A-Z Statewide Plumbing
Street: 2215 Southwest 58th Terrace
City: West Park
State: FL
Zip: 33023
Phone: 954.963.5800
Cell: 954.583.6497
Rating: 2
Contact: Robert
Block 6
Name: Harry Raskin, Inc
Street: 20362 Northeast 16th Place
City: Miami
State: FL
Zip: 33179
Phone: 305.653.2270
Cell: 305.653.2270
Rating: 8
Contact: James
Block 7
Name: Aarco Leak Detection
Street: 1048 Northeast 167th Street
City: North Miami Beach
State: FL
Zip: 33162
Phone: 305.947.7283
Cell: 786.728.9874
Rating: 4
Contact: Steven
Block 8
Name: EMPTY
Street: no data
City: no data
State: no data
Zip: no data
Phone: no data
Cell: no data
Rating: 0
Contact: no data
Block 9
Name: EMPTY
Street: no data
City: no data
State: no data
Zip: no data
Phone: no data
Cell: no data
Rating: 0
Contact: no data
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.