Delete the above code, for now.Code:for(i = 0; i < 45; i++) putchar('\n');
Delete the above code, for now.Code:for(i = 0; i < 45; i++) putchar('\n');
ok, deleted. lol :]
Can you correct the transaction file? The lines I've highlighted need some re-working, so there is just ONE transaction, per line.
I didn't see your corrections to the names so I've done my own: Michael is now Mike, and Melissa is now Melisa.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 Mike 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 Melisa 3 119 3 Austin 0 0 0
ah, sorry for not showing the changes, but here is both the data and trans files changed:
data.txt =
transactions.txt =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 Mike 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 Jake 90,78,69 8 25 219 Austin 91,45,88 7 29 0 0,0,0 0 0
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 Mike 1 109 4 Tony 4 110 5 Rob 2 111 -4 Sonny 3 112 4 Luke 4 113 3 April 2 114 9 Sheryl 4 115 4 Tom 5 116 18 Lamont 1 117 3 Steve 2 118 1 Jake 3 119 3 Austin 0 0 0
What about a type = 0 (insert) transaction? Also the subsequent transactions on that record to "fill it out" with data.
The assignment talks about this at some length, but I don't see where it says that they are required.
yes, I suppose they very well may not be required so I may not need them, but I will change it so a few have type 0.
"Total". I see it's in your Student struct, and from the assignment, it's not to be written to any file, but only computed.
I don't see why it would need to be in the struct then, and I'm removing it.
More later.
Here we go: Not everything, but it's a good start. We need your new transaction data, when you're ready.
Code:/*Modifications of a program posted by needHelpBad, by Adak */ #include <stdio.h> #include <stdlib.h> #include <string.h> struct Student { int key; char name [7]; int test1, test2, test3; int hwCount; int hwPoints; } dummy = {-1, "", 0, 0, 0, 0, 0}, * aStudent; //this is the weirdest bit of code I've seen in awhile. What is up with it? FILE * infile, * recordFile, * transFile; int openFiles ( ) { if ((infile = fopen ("seq_data.txt", "r")) == NULL) { fprintf (stderr, "Error in opening input file\n"); return 1; } if ((transFile = fopen ("seq_tran.txt", "r")) == NULL) { fprintf (stderr, "Error in opening transaction file\n"); return 1; } if ((recordFile = fopen ("seq_recs.txt", "w+b")) == NULL) { fprintf (stderr, "Error in opening record file\n"); return 1; } return 0; } void closeFiles ( ) { fclose (infile); fclose (recordFile); fclose(transFile); } struct Student * getOneStudentFromData ( ) { struct Student * aStudentPtr; int scanned, theId, theTest1, theTest2, theTest3, theHwCount, theHwPoints; char theName [7]; char cr; fscanf (infile, "%d%s%d%d%d%d%d%c", &theId, theName, &theTest1, &theTest2, &theTest3, &theHwCount, &theHwPoints, &cr); if(theId > 0) { 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); } else aStudentPtr = &dummy; 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 (void) { static int recordsSeen = 0; struct Student * theStudentPtr; theStudentPtr = (struct Student *) malloc (sizeof (struct Student)); do { theStudentPtr = getOneStudentFromData ( ); if(theStudentPtr->key == -1) { theStudentPtr = &dummy; recordsSeen--; } 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)); putchar('\n'); do { fread (myStudentPtr, sizeof (struct Student), 1, recordFile); if (myStudentPtr->key != -1) { printf ("%d\t%s\t%d\t%d\t%d\t%d\t%d\n", myStudentPtr->key, myStudentPtr->name, myStudentPtr->test1, myStudentPtr->test2, myStudentPtr->test3, myStudentPtr->hwCount, myStudentPtr->hwPoints); recordsSeen++; } } while (myStudentPtr->key != -1); return recordsSeen; } int main ( ) { int i, numRecords, gar; for(i = 0; i < 10; i++) putchar('\n'); if (openFiles ( )) return 1; numRecords = 0; numRecords = readDataAndWriteRecords ( ); printf ("\n %d records read from data file\n", numRecords); fseek (recordFile, 0, SEEK_SET); numRecords = 0; numRecords = readRecordsAndPrintData ( ); printf ("\n %d records read from record file\n", numRecords); closeFiles ( ); printf("\n Program Complete - Press Enter When Ready"); gar = getchar(); gar++; return 0; }
Last edited by Adak; 05-23-2008 at 01:34 AM.
Question to a more experienced programmer: Shouldn't these while loops which include malloc at the top of the loop, also include free at the bottom of the while loop, to stop memory leaking?
Also, isn't there something wrong with dummy having an extra pointer?
Last edited by Adak; 05-23-2008 at 01:25 AM.
in the function
readDataAndWriteRecords you DO have a memory leak - malloced memory before do loop is leaked...
maybe you have other leaks... I haven't read the code till the end
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
thank you so much so far for all the help and assistance guys, really.
so how would I go about fixing the loop in the readDataAndWriteRecords function
I don't know enough about it to see the problem, maybe I am missing something easy..
here is the new transactions code that was asked for:
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 0 106 4 Dave 5 107 14 Rich 3 108 2 Mike 1 109 4 Tony 0 110 5 Rob 2 111 -4 Sonny 3 112 4 Luke 4 113 3 April 2 114 9 Sheryl 4 115 4 Tom 5 116 18 Lamont 0 117 3 Steve 2 118 1 Jake 3 119 3 Austin 0 0 0
bump.. ^^
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler