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];
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 <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 // set string lengths
5 #define STRSHORT = 15 chars
6 #define STRMED = 30 chars
7 #define STRLONG = 80 chars
8 using namespace std;
10 struct company
12 ?? add member elements for structure
15 void printInfo(company* info, FILE* fp);
17 int main()
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
25 create instance of company information structure
27 printf("Enter input file name :");
28 scanf("%s", input file name );
29 fflush(stdin); /* flush keyboard buffer */
31 // open binary data file
32 if ( fill in the fopen expression to open the database file )
34 fprintf(stderr, "Error opening input file.");
38 printf("Enter output file name :");
39 scanf("%s", output file name );
40 fflush(stdin); /* flush keyboard buffer */
42 // open output text file
43 if ( fill in the fopen expression to open the output text file )
45 fprintf(stderr, "Error opening output file.");
48 for (BlockNumber=0; BlockNumber<10; BlockNumber++)
50 /* Move the position indicator to the specified element. */
51 if ( fill in the fseek expression to position to BlockNumber )
53 fprintf(stderr, "\nError using fseek().");
56 /* Read in a single info. */
57 fread( fill in the fread expression to read data );
59 fprintf(op,"\n\n\n Block %d\n\n",BlockNumber); /add header text
60 printInfo (fill in correct calling arguments);
62 return 0;