1. 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];

2. 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);

3. Declare file pointers:
Inside the program main function, two file pointers need to be declared: Input database file pointer line 19 and Output text file pointer line 20.

4. Declare data structure:
Declare an instance of the generic company data structure line 25 used to hold read file contents. Use the ‘new’ operator creating a block of memory returning a pointer to the block. The pointer is named and is used throughout the program to reference the structure.
company* info= new company;

5. 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 is used. The array can be declared with a long string constant [STRLONG]. The declaration should be placed at line 22. That input 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, the fopen specification should be to ‘read binary’ mode.

6. 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 before it 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’ condition expression. 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.

1 #include
2 #include
3 #include
4 // set string lengths
5 #define STRSHORT = 15 chars
6 #define STRMED = 30 chars
7 #define STRLONG = 80 chars
8 using namespace std;
9
10 struct company
11 {
12 ?? add member elements for structure
13 };
14
15 void printInfo(company* info, FILE* fp);
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 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, “nError 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,”nnn Block %dnn”,BlockNumber); /add header text
60 printInfo (fill in correct calling arguments);
61 }
62 return 0;
63 }



Source link

Leave a Reply

Your email address will not be published.