-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinaryfile.c
More file actions
64 lines (45 loc) · 1.79 KB
/
binaryfile.c
File metadata and controls
64 lines (45 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/** File binaryfile.c
*
* @author Nathaniel Miller
*
* Program to demonstrate writing
* to binary files using low level I/O.
*/
#include "employee.h"
int main() {
#ifdef DEBUG
printf("\nNow in Debug Mode.\n\n");
#endif
printf("Starting program binaryfile.\n"); /* notify user of program start */
int k = 0; /* loop counter */
int ent; /* hold number of entries */
char* entries = readline("Number of entries needed: "); /* prompt # of entries */
ent = atoi(entries); /* convert user entered string to integer for use later */
Employee2** emp = calloc(ent, sizeof(Employee2*)); /* allocate array of ptrs */
/* loop until the number of entries has been reached */
while( k < ent) {
emp[k] = userBinEmp(); /* fill the array with user entered employees */
k++; /* advance to next element in the array */
} /* end while */
/* output the employees to a file */
printf("About to write to file.\n");
int j = 0; /* second loop counter */
/* check if opening the file was sucessful */
if( open("btest.txt", O_WRONLY | O_CREAT) != -1 ) {
int outfile = open("btest.txt", O_WRONLY | O_CREAT); /* open file */
/* loop until number of entries has been reached */
while( j < ent ) {
outbinaryEmp(outfile, emp[j]); /* write each employee entry to the file */
j++; /* advance to next entry in the array */
} /* end while */
close(outfile); /* close the file */
} /* end if */
/* if unable to open file, or error occurs, print errors to stderr */
else {
fprintf(stderr, "\nError: unable to open file in program binaryfile.\n");
fprintf(stderr, "Error Number: %d, Information: %s, ",
errno, strerror( errno ));
} /* end else */
printf("Ending program stest.\n");
return 0; /* sucessful termination */
} /* end program main */