Thread: sequential file program

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    61

    sequential file program

    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, "&#37;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.
    Last edited by needhelpbad; 05-19-2008 at 08:03 PM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I can't even read your dang post without constant aggravation!

    Don't "break" the darn forum "table" by exceeding it's width - particularly inside code tags, where a wider font is used.

    Please edit your assignment portion so it all fits in one screen's width, and then you can at least have people read it without a ton of aggravation, first.

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    61
    heh my apologies, that should be a little easier to work with

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    This is a version of your program that will compile. Please see if it compiles on your system. Also, please go through your assignment requirements, and make a list of what requirements are met by which function or functions.

    I don't want to make a list of what functions work, and what functions don't - you can do it better than I can - so please make it up, and post it.

    Whatever code/pseudo-code I propose, you'll need to consult your lists and assignment sheet to see if it meets all the requirements. I won't do that for you, particularly since the assignment requirements are both detailed and plentiful.

    I'll have more time to look it over tomorrow. Any fixes to the code here THAT DOES COMPILE, please post it.

    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 (void) {
        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 ("%d records read from record file\n", numRecords);
        closeFiles ( );
    
       return 0;
    }

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    61
    just out of curiosity, what did you change that allowed this to compile? and I will look over it more now to see which functions are working and which aren't.

    Thanks for the help

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by needhelpbad
    just out of curiosity, what did you change that allowed this to compile? and I will look over it more now to see which functions are working and which aren't.

    Thanks for the help
    #1 - The language is case-sensitive, infile is not the same as inFile.
    Code:
    FILE * infile, * recordFile, *transFile;
    
    int openFiles ( ) {
        if ((infile = fopen ("data.txt", "r")) == NULL) {
    	fprintf (stderr, "Error in opening input file\n");
    	return 0;
        }
        ...
    }
    
    void closeFiles ( ) {
        fclose (infile);
        fclose (recordFile);
        fclose(transFile);
    }
    
    struct Student * getOneStudentFromData ( ) {
        ...
    
        fscanf (inFile, "&#37;d%s%d%d%d%d",
    	&theId, &theName, &theTest1, &theTest2, &theTest3,
    	&theHwCount, &theHwPoints);
        ...
    }
    #2 - You are missing a semicolon:
    Code:
    struct Transaction {
        int type;		
        int key;		
        int value;	
        char name [7];
    };
    
    
    int readDataAndWriteRecords (void) {
        ...
    }
    #3 - See anything wrong:
    Code:
    numRecords = readRecordsAndPrintData ( );
    printf ("
    printf ("%d records read from record file\n", numRecords);
    Those changes simply get it to a state that compiles. The output error/warning messages from just about any compiler will tell you what and where the problems are... unless you aren't even bothering to look at them.
    Last edited by hk_mp5kpdw; 05-20-2008 at 11:12 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    Registered User
    Join Date
    May 2008
    Posts
    61
    so here is the fixed version of the seqfile that I can come up with the fixed problems:

    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 ("%d records read from record file\n", numRecords);
        closeFiles ( );
    }
    for what works and doesnt work, im pretty sure im not having any issues loading the different files, but im still iffy on whether I even have the files set up right (ie the data file)

    obviously the close files works, for the struct of getOneStudentFromData, I think I am having an issue of connecting the pointers to the info from the data file, but not sure what to do or whats causing the issue.

    and with all that, i simply don't have a struct to interact with the transFile because I am not sure where to even start with it. So far I am greatly appreciative of the help though, thanks guys, really.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You have a transaction struct defined in the program. Wasn't it intended for use with the transaction file? You'll need to create an instance of that transaction struct, of course.

    I'm disappointed that you haven't done the bookkeeping type work for this program. I wanted you to share what each function did, to complete what part of the assignment, if any.

    If you're not sure what has been loaded from a file, I expect you to print the record, or field, onto the screen, and check. And not to come here and say "I'm not sure about ...<whatever>." You need to get sure, and get sure quickly. Same thing with output. You can, and should be, checking it.

    I need that assignment work done ASAP. That's what I use to design a program - from the top down, at first. Put the details of variables in another list, but the major things the program has to do, will be used to decide what functions we have will do what, and what other functions we still need.

    Put off the details, (list them, off to one side, but don't include them in the function code, for now). We want to focus on the big stuff, first).

  9. #9
    Registered User
    Join Date
    May 2008
    Posts
    61
    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).
    This part is covered in the beginning of the seqfile with

    struct Student {
    int key;
    char name [7];
    int test1, test2, test3;
    int hwCount;
    int hwPoints;
    int total;
    }
    dummy = {-1, "", 0, 0, 0, 0},
    * aStudent;


    Code:
    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.
    that is covered with the data.txt file:
    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

    Code:
    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.
    I have this simple struct in the seq file, but aside from this simple implementation, it goes no farther, this is obviously an issue.

    Code:
    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.
    This is my main issue I believe, where working this kind of coding into dealing with the transaction file just confuses me and I do not know where to start here.

    Code:
    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.
    This is completed

    Code:
    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.
    More transaction issues I have here.

    Code:
    6. Store a sentinel (key = 0, name = “”, scores all 0) at the end of the file, 
    to avoid some C/UNIX file read errors.
    This is in place as you can see from the data.txt

    Code:
    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.
    This is taken care of with:
    Code:
    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 ("%d records read from record file\n", numRecords);
        closeFiles ( );
    }
    Code:
    8. Reset the result file (fseek (filename, 0, SEEK_SET)).
    Taken care of.

    Code:
    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.
    This resulting part is covered with:
    Code:
    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;
    }



    I apologize ahead of time, I am sure with all these code boxes, it may be a little hard to follow or just look completely stupid in general, but I tried to do what you asked.

    //continued thanks for all of this.

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Change the names which have 7 letters, like "Michael", to something shorter, maybe "Mike". Reason is that there's no room for an end of string char: '\0' to elevate the name char's into a string.


    clear the page first with:
    Code:
    for(i = 0; i < 45; i++)
       putchar('\n');

    I mis-understood the assignment about transaction "type". I thought it was to work interactively, and would need a menu.

    That is not the case. Type in this case, does not mean "hitting keys on the keyboard".
    Last edited by Adak; 05-21-2008 at 08:13 AM.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Adak View Post
    Change the names which have 7 letters, like "Michael", to something shorter, maybe "Mike". Reason is that there's no room for an end of string char: '\0' to elevate the name char's into a string.
    Or better yet, use a longer array to store the string. Say 15 or so chars, which will work for most single and double first names.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I'm glad you're following this thread, Mats.

    The names array must be kept at 7, according to the assignment, though.

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Adak View Post
    I'm glad you're following this thread, Mats.

    The names array must be kept at 7, according to the assignment, though.
    Yes, you are right - I thought the data files where provided by the tutor or some such, in which case it would be incorrect to use names that are 7 characters long.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    Registered User
    Join Date
    May 2008
    Posts
    61
    okay so i changed all the problematic names in the data.txt to be 6 letters or less to allow the final zero bit for the string

    and is this how I am supposed to implement the clear page coding?

    Code:
    struct Transaction {
        int type;		
        int key;		
        int value;	
        char name [7];
    
       for(i = 0; i < 45; i++)
       putchar('\n');
    
    };
    Thanks

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by needhelpbad View Post
    okay so i changed all the problematic names in the data.txt to be 6 letters or less to allow the final zero bit for the string

    and is this how I am supposed to implement the clear page coding?

    Code:
    struct Transaction {
        int type;		
        int key;		
        int value;	
        char name [7];
    
       for(i = 0; i < 45; i++)
       putchar('\n');
    
    };
    Thanks
    Well, yes and no - you shouldn't put it in a struct like that - you should have it in the function where you want to clear the screen. [Although I personally prefer code that doesn't clear the screen so that you can see what went on in the past, e.g. what you have done so far, etc].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file help plz
    By dutrachr in forum C Programming
    Replies: 4
    Last Post: 04-18-2006, 11:41 AM
  2. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  3. fputs and sequential file
    By sballew in forum C Programming
    Replies: 5
    Last Post: 11-11-2001, 04:48 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM