Thread: sequential file program

  1. #16
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Code:
    for(i = 0; i < 45; i++)
       putchar('\n');
    Delete the above code, for now.

  2. #17
    Registered User
    Join Date
    May 2008
    Posts
    61
    ok, deleted. lol :]

  3. #18
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Can you correct the transaction file? The lines I've highlighted need some re-working, so there is just ONE transaction, per line.

    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
    I didn't see your corrections to the names so I've done my own: Michael is now Mike, and Melissa is now Melisa.

  4. #19
    Registered User
    Join Date
    May 2008
    Posts
    61
    ah, sorry for not showing the changes, but here is both the data and trans files changed:

    data.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
    transactions.txt =
    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

  5. #20
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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.

  6. #21
    Registered User
    Join Date
    May 2008
    Posts
    61
    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.

  7. #22
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    "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, "&#37;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.

  8. #23
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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.

  9. #24
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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

  10. #25
    Registered User
    Join Date
    May 2008
    Posts
    61
    thank you so much so far for all the help and assistance guys, really.

  11. #26
    Registered User
    Join Date
    May 2008
    Posts
    61
    so how would I go about fixing the loop in the readDataAndWriteRecords function

  12. #27
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by needhelpbad View Post
    so how would I go about fixing the loop in the readDataAndWriteRecords function
    What do YOU think? Do you understand the concept of memory leaks?

    --
    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.

  13. #28
    Registered User
    Join Date
    May 2008
    Posts
    61
    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

  14. #29
    Registered User
    Join Date
    May 2008
    Posts
    61
    bump.. ^^

  15. #30
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by needhelpbad View Post
    bump.. ^^
    Instead of annoying people with senceless bumping - write some code and show some progress. Do not wait for people to do your work...
    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

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