-
Notifications
You must be signed in to change notification settings - Fork 0
/
count.c
81 lines (66 loc) · 2.51 KB
/
count.c
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
80
81
/*Laura Stalker
LS451494
lstalker@albany.edu
*/
#include <stdio.h> /*Include the standard input/output library*/
#include <stdlib.h> /*To use exit function*/
#include <string.h> /*String library for string manipulation functions*/
#include "constants.h" /*Header file with constants*/
#include "prototypes.h" /*Header file with prototypes*/
/*A function that takes a string and an int as a parameter, and returns an
int. The string is assumed to be the name of a relation in the database. It
creates the name of a data file from a relation name (for that relation), and
then opens that file. It calls another function to get the length of each tuple
in this particular relation. It finds the end of the data file, and uses
the length of a tuple for this relation to calculate the number of tuples in
this relation in the db. It prints this number to stdout, and returns it. The
flag param is 1 if we want this info printed.
*/
int count( char *relName, int flag){
FILE *dataFile; /*Ptr to data file we get from relName*/
int tupleLen; /*Holds length of tuple for this relation*/
long temp; /*Holds value of end of file, and then length*/
/*Create an array large enough for a string that is the attribute
name plus ".dat\0". This will be the name of the data file.
*/
char fileName[ strlen( relName) + strlen( ".dat\0")];
strcpy( fileName, relName);
strcat( fileName, ".dat\0");
/*Open the data file for reading. If unable to open, print an
error message and exit.
*/
if( (dataFile = ( fopen( fileName, "r"))) == NULL ){
printf("There was an error trying to open data file.\n");
exit(1);
}
/*Get the length of a tuple for this relation
*/
tupleLen = tuplen( relName, 0);
/*Move to the end of the file and get the position. If unaable to,
print an error message.
*/
if( fseek( dataFile, 0, SEEK_END) == 0){
temp = ftell( dataFile);
}
else{
printf("The size of the file could not be determined.\n");
fflush( stdout);
}
/*the number of tuples in the relation is equal to the number of bytes in
the data file divided by the number of bytes in a tuple.
*/
temp = temp / tupleLen;
/*Print the number of tuples in the relation to stdout.
*/
if( flag == 1 ){
printf("There are %d tuples in the %s relation.\n\n", (int) temp, relName );
fflush( stdout);
}
/*Close data file
*/
if( fclose( dataFile) == EOF){
fprintf( stderr, "Error closing data file.\n");
exit(1);
}
return (int) temp; /*Return num tuples*/
}