First off, let me start by saying i know that you guys are not going to give me a complete version of what I need,
nor tell me exactly how to do it. That said, I am going to mention that I am in desperate need of help,
don't understand at all what I am truly doing, but will take whatever help you guys are willing to offer,
and see where I can go from there.
That said, this is the sequential file assignment:
Code:
1. Look at the last several parts of the programming assignment.
We want to modify sequential file creation to handle the following datatype:
struct Student {
int key; /* between 100 and 999 */
char name [7]; /* makes tabs work nicely */
int test1, test2, test3;
/* values between 0 and 120 */
int hwCount; /* values between 0 and 10 */
int hwPoints; /* values between 0 and 50 */
};
Note that total is not represented anywhere in the stored records—this is required.
In order to report it, it must be computed. Total counts all test grades and homework points,
plus a 1-point bonus per HW assignment (that is, add in hwCount).
2. Create an input file of between 20 and 30 records [more if you’d like],
in key-sorted order, and create the sequential file. Make all data values correct.
3. Create a transaction file of records of the following type.
struct Transaction {
int type; /* values between 0 and 5 */
int key; /* between 100 and 999 */
int value; /* interpretation depends on value of type */
char name [7]; /* usually ignored */
};
For simplicity, there are no deletes.
4. The keys should be in sorted order, with duplicates. The interpretation is as follows:
Type = 0, insert a new record with name name and key key, and all other values 0.
key should have at least one character, that is, strlen (key) > 0.
Type = 1, 2, or 3, add value points to test. (value may be negative.)
Check for constraint on resulting test score.
Type = 4, add one assignment and value points for homework, checking the constraint.
value should be non-negative, and less than or equal 5.
Type = 5, add value points for homework, without changing hwCount.
Total hwPoints should not exceed 5 * hwCount.
Note to create and populate a new record,
you need to use one transaction of type 0, one each of types 1, 2, and 3, and several of type 4.
5. Create a program that opens both the sequential file and the transaction file for reading,
and another sequential file for writing and reading (w+). Look at the first key in each read file.
If the master key is earlier, copy the record.
If the transaction key is earlier:
If it is an insert (type = 0), create a new record.
Populate it with subsequent modify operations (type 1-5).
Copy the final record.
It is an error if a record that doesn’t exist is modified. Print an error message to stderr and continue.
If the two keys are equal:
If it is an insert, print an error message and continue.
You may check whether the names are also equal, and base your error message on that test.
Otherwise, perform the requested modification.
It is an error if the numerical conditions are violated.
Use the extreme range value (0 or max), rather than rejecting, print an error message, and continue.
6. Store a sentinel (key = 0, name = “”, scores all 0) at the end of the file,
to avoid some C/UNIX file read errors.
7. Keep track of the number of initial records, the number of transactions,
the number of errors, and the number of final records, and print the final values.
8. Reset the result file (fseek (filename, 0, SEEK_SET)).
9. Read the result file. For each record, print the key, the name, the point total,
and the percent average (based on 350 points). Finally, print the number of records processed.
Now before I even embarrass myself by posting what little I was able to accomplish,
note that i am trying, just a slow, slow learner.. in a fast, fast teaching teachers' class.
This is the seq file that I have so far:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Student {
int key;
char name [7];
int test1, test2, test3;
int hwCount;
int hwPoints;
int total;
}
dummy = {-1, "", 0, 0, 0, 0},
* aStudent;
FILE * infile, * recordFile, *transFile;
int openFiles ( ) {
if ((infile = fopen ("data.txt", "r")) == NULL) {
fprintf (stderr, "Error in opening input file\n");
return 0;
}
if ((recordFile = fopen ("records.txt", "w+")) == NULL) {
fprintf (stderr, "Error in opening record file\n");
return 0;
}
if ((transFile = fopen ("transactions.txt", "w+")) == NULL) {
fprintf (stderr, "Error in opening transaction file\n");
return 0;
}
return 1;
}
void closeFiles ( ) {
fclose (infile);
fclose (recordFile);
fclose(transFile);
}
struct Student * getOneStudentFromData ( ) {
struct Student * aStudentPtr;
int theId, theTest1, theTest2, theTest3, theHwCount, theHwPoints;
char theName [20];
fscanf (inFile, "%d%s%d%d%d%d",
&theId, &theName, &theTest1, &theTest2, &theTest3,
&theHwCount, &theHwPoints);
aStudentPtr = (struct Student *) malloc (sizeof (struct Student));
aStudentPtr->key = theId;
aStudentPtr->test1 = theTest1;
aStudentPtr->test2 = theTest2;
aStudentPtr->test3 = theTest3;
aStudentPtr->hwCount = theHwCount;
aStudentPtr->hwPoints = theHwPoints;
strcpy (aStudentPtr->name, theName);
return aStudentPtr;
}
void writeRecord (struct Student * theStudentPtr) {
fwrite (theStudentPtr, sizeof (struct Student), 1, recordFile);
}
struct Transaction {
int type;
int key;
int value;
char name [7];
}
int readDataAndWriteRecords ( ) {
static int recordsSeen = 0;
struct Student * theStudentPtr;
theStudentPtr = (struct Student *) malloc (sizeof (struct Student));
do {
theStudentPtr = getOneStudentFromData ( );
recordsSeen ++;
writeRecord (theStudentPtr);
} while (theStudentPtr->key != -1);
return recordsSeen;
}
int readRecordsAndPrintData ( ) {
static int recordsSeen = 0;
struct Student * myStudentPtr;
myStudentPtr = (struct Student *) malloc (sizeof (struct Student));
do {
fread (myStudentPtr, sizeof (struct Student), 1, recordFile);
recordsSeen ++;
if (myStudentPtr->key!= -1) {
printf ("%d\t%s\t%d\t%d\t%d\t%d\n",
myStudentPtr->key,
myStudentPtr->name,
myStudentPtr->test1,
myStudentPtr->test2,
myStudentPtr->hwCount,
myStudentPtr->hwPoints);
}
} while (myStudentPtr->key != -1);
return recordsSeen;
}
int main ( ) {
int numRecords;
if (!openFiles ( )) {
return 0;
}
numRecords = 0;
numRecords = readDataAndWriteRecords ( );
printf ("%d records read from data file\n", numRecords);
fseek (recordFile, 0, SEEK_SET);
numRecords = 0;
numRecords = readRecordsAndPrintData ( );
printf ("
printf ("%d records read from record file\n", numRecords);
closeFiles ( );
}
Along with this is the data, records, and transactions file.
The records file I have blank, with nothing in it at all;
The data file looks like this:
Code:
100 Jeff 98,76,86 8 41
101 Mark 66,51,76 4 31
102 Jenn 79,89,93 9 44
103 Chris 44,78,45 3 28
104 Matt 74,81,88 8 39
105 Dan 67,91,83 6 24
106 Dave 55,81,75 7 45
107 Rich 91,93,89 9 32
108 Michael 55,61,78 3 26
109 Tony 67,68,78 5 45
110 Rob 89,71,80 7 42
111 Sonny 78,81,89 8 15
112 Luke 91,83,75 8 24
113 April 66,79,70 6 35
114 Sheryl 89,81,85 9 50
115 Tom 79,61,90 5 33
116 Lamont 80,76,89 7 41
117 Steve 90,98,100 10 50
118 Melissa 90,78,69 8 25
219 Austin 91,45,88 7 29
0 0,0,0 0 0
and the transactions file looks like this:
Code:
3 100 5 Jeff
4 101 5 Mark
1 102 3 Jenn
2 103 5 Chris
4 104 4 Matt
3 105 3 Dan
4 106 4 Dave
5 107 14 Rich
3 108 2 Michael
0 109 Jim,120 Tony
4 110 5 Rob
2 111 -4 Sonny
3 112 4 Luke
4 113 3 April
0 114 Al,121 Sheryl
4 115 4 Tom
5 116 18 Lamont
1 117 3 Steve
2 118 1 Melissa
3 119 3 Austin
0 0 0
I already shamefully handed this in, knowing I was going to do terribly, and that I did.
My hopes for what little partial credit I would get failed,
and currently I am sitting on an F for the class and will stay there unless I fix this.
My professors comments were:
Code:
...Your sequential file program didn't even compile, and when fixed doesn't work.
It never looks at transactions. ...
And that's it. An obvious issue I can point out directly,
is I truly have no idea how I would go about checking the different files initiated out of the seq file.
ANY help will be greatly appreciated. Remember i don't expect you to hand it to me at all,
but whatever you DO offer, I'll be greatful for. I just need direction and assistance, that's it.
Thanks for the nth time.