sequential file program

This is a discussion on sequential file program within the C Programming forums, part of the General Programming Boards category; I also have a question or two about the code you posted earlier: in this struct: Code: struct Student * ...

  1. #46
    Registered User
    Join Date
    May 2008
    Posts
    61
    I also have a question or two about the code you posted earlier:

    in this struct:
    Code:
    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);
    you have int scanned, but thats the only place I ever see 'scanned' in the whole code, what is it for?

    and what does the char cr refer to?

  2. #47
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Scanned can be deleted. I just used it for a bit to help figure out why the original code didn't work. Cr pulls out the newline at the end of the record. (cr = carriage return)

  3. #48
    Registered User
    Join Date
    May 2008
    Posts
    61
    ahh okay that makes sense.

    does this coding look right or along the right lines?

    Code:
    case 3:
    printf("Enter a value for test 3");
    scanf("%d", &theTest3);
    writeRecord(&theTest3);
    
    //or maybe
    
    writeRecord(theStudentPtr->test3);
    break;

  4. #49
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Nope.

    We are getting the data from a file, not from the user entering it at the keyboard. Also, the data for the test must be written out into exactly the right spot, either in the database file, or the records file (I'll look at this more closely later today).

    I'm not sure exactly how this should go, just yet. Above is my thinking just now. May not be right.
    Last edited by Adak; 06-02-2008 at 06:53 AM.

  5. #50
    Registered User
    Join Date
    May 2008
    Posts
    61
    Absolutely right. I have no idea why I was trying to get the users input info when there is blatantly a data file waiting to be used.

    Would I still need this switch menu (with the user selecting which switch to use) if we are just dealing with the data record, or is the data going to determine which switch to use via the type (which as I type this, I am sure that's the correct answer.. I think)

    What I am unsure is how to retrieve the data from the file for the switch to read and analyze, then send to the record file, putting it in the correct place as well.

    I am sure it has to have something along the lines of:
    Code:
       fread (theStudentPtr, sizeof (struct Student), 1, infile);
    but I wouldn't know what to change to have it to the specifications I need per each switch.
    Last edited by needhelpbad; 06-02-2008 at 11:16 AM.

  6. #51
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The user (in this case) won't select anything. It will read the data from the transaction file, and go right to the switch case logic.

    File should be read like the data file, except using the transaction struct, instead of the student struct, unless you see a need to change it.

    I've set aside some time this afternoon to thinking this through, and the program is on the other computer, so I've got to get over there to see if my thinking is OK, or not.

    I'll have some answers later today.

  7. #52
    Registered User
    Join Date
    May 2008
    Posts
    61
    that sounds great Adak, I continually need to thank you for the amount of time, patience, and gratitude you're giving me, so thanks again.

    for ease of changing the data, such as the test1, test2, test3, should i change 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	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
    to something 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	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
    Last edited by needhelpbad; 06-02-2008 at 01:39 PM.

  8. #53
    Registered User
    Join Date
    May 2008
    Posts
    61
    also, since we are supposed to use the transaction struct instead of the student one, would we use theStudentPtr, or aStudentPtr for it? or maybe even neither?

    Code:
       fread (aStudentPtr, sizeof (struct Transaction), 1, infile);
    and if persay it was even what I just posted, how would I make it search for just say the test3 score, and not all of it?

  9. #54
    Nub SWE
    Join Date
    Mar 2008
    Location
    Dallas, TX
    Posts
    133
    A few things that I've noticed.

    You need function prototypes declared at the top of your source.
    Code:
    int readDataAndWriteRecords(void);
    Student* getOneStudentFromData(void);
    void writeRecord(Student *theStudentPtr);
    int readRecordsAndPrintData(void);
    int openFiles(void);
    void closeFiles(void);
    You can tell the code not to read certain characters from an input file or from command line input by using the * in your scanf() calls, like this:
    Code:
    fscanf(dataFile, "%d%s%d%d%d%d%d%*c", &theId, theName, &theTest1, &theTest2,
    		&theTest3, &theHwCount, &theHwPoints);
    This will save you the hassle of having to create a 'cr' variable to get rid of your carriage returns.

    I would recommend defining a constant NAME_LENGTH at the top of your source to get rid of your magic numbers, like so:
    Code:
    #define NAME_LENGTH		7
    This will allow you to change the numerical constant in one place, instead of seven or eight, if you ever decide to change the maximum allowable length of your name strings. It also makes your code easier to read.

    In C, you don't need to cast what malloc() returns. It is handled for you.
    Code:
    	aStudentPtr = malloc(sizeof(Student));

  10. #55
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    It would be good to have function prototypes, they're not required in this case, though.

    The name_length as a define is a good idea, but the magic number 7 can't change - it's required by the assignment.

    In my early version of C, I *do* have to cast the pointer from malloc. It will not compile otherwise.

    The * trick in fscanf() is new to me, but that part of the program works perfectly, already. I wouldn't change it. We have a last function to go and it's done, and I was supposed to work on it yesterday, but *decided* to get sick, instead.

    I'll try it again, today.

    Thanks for your input, JDGATX.

  11. #56
    Nub SWE
    Join Date
    Mar 2008
    Location
    Dallas, TX
    Posts
    133
    My version would not compile without the prototypes, so I was throwing that in there.

    I didn't have any real substance to offer up, just cosmetic changes to better the code.

  12. #57
    Registered User
    Join Date
    May 2008
    Posts
    61
    Yea thanks for joining in the thread JD.

    Just out of curiosity since Adak says we don't need it, but what does the benefit of setting those prototypes do for the functions?

    and that indeed is a nice trick to use the asterisk in that case with the fscanf. Thanks.

  13. #58
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by needhelpbad View Post
    Yea thanks for joining in the thread JD.

    Just out of curiosity since Adak says we don't need it, but what does the benefit of setting those prototypes do for the functions?

    and that indeed is a nice trick to use the asterisk in that case with the fscanf. Thanks.
    You need prototypes so that the compiler knows what types the functions' parameters and return values are.

    --
    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. #59
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Function prototypes are always a good thing, and usually required. Not so here, because we have all the functions before the main() function, and our compilers aren't the newest.

    Almost all the changes now will be in the readAndWriteData function. Only the total variable and some constraint checking will be put into readRecordsAndPrintData function.

    I've made several additions, but it's not done yet. It's a little dicey because the new logic needs to be "shoe horned" into the old.

    I'll post everything up, tomorrow pm.

    I haven't even looked at your new transaction file. I'll leave whatever that may be, up to you.

  15. #60
    Registered User
    Join Date
    May 2008
    Posts
    61
    the transaction file hasn't changed; its still the same.
    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	
    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 I changed was the data file, which now 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	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
    and don't we still need to work on the changes inside the switches function so that it reads from the data file instead of the user input? I am sure thats a quick and easy fix though, so it might as well be just the readAndWriteData function.

Page 4 of 6 FirstFirst 123456 LastLast
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, 12:41 PM
  2. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 04: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, 11:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21