Report abuse

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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
void readNUMBER(void) {
	FILE *employee;
	struct employeeRecord query;
	int record, x;
	long offset;
	char *name, *address;

/*get record # */		
	printf("Which record do you want to find? ");
	scanf("%d", &record);

/*open file */
	employee = fopen("employeeRecords", "r");
	if(employee == NULL) {
		printf("Error opening file.\n");
		return;
	}

/^seek to the record onfile */
	offset = (long)(record-1)*sizeof(query);
	x = fseek(employee, offset, SEEK_SET);
	if(x != FALSE) {
		printf("Error reading from file!\n");
		return;
	}

/*read the record into memory */
	x = fread(&query, sizeof(query), 1, employee);
	if(x == FALSE) {
		printf("Error reading record!1\n");
		return;
	}

/*malloc space for the strings stored outside of the structure on file */
	name = (char *) malloc( sizeof(char) * query.nameLength); 
	if (name == NULL) exit (1); 
	address = (char *) malloc( sizeof(char) * query.addressLength); 
	if (address == NULL) exit (1); 
	
/*Seek to the name string */
	offset = (long)(record-1)*sizeof(query)+sizeof(query);
	x = fseek(employee, offset, SEEK_SET);	
	if(x != FALSE) {
		printf("Error reading from file!\n");
		return;
	}

/*Read name into memory */
	x = fread(&name, query.nameLength, 1, employee);
	if(x == FALSE) {
		printf("Error reading record!2\n");
		return;	
	}

/*Seek to address */
	offset = (long)(record-1)*sizeof(query)+sizeof(query)+query.nameLength;
	x = fseek(employee, offset, SEEK_SET);	
	if(x != FALSE) {
		printf("Error reading from file!\n");
		return;
	}

/*read address into memory */
	x = fread(&address, query.addressLength, 1, employee);
	if(x == FALSE) {
		printf("Error reading record!3\n");
		return;
	}

/*Print results */
	printf("Record: %d\n", record);
	printf("Name: %s\n", name);
	printf("Address: %s\n", address);
	printf("Phone Number: %d\n\n", query.phoneNumber);
	fclose(employee);
	free(name);
	free(address);
return;
}