Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- StudentRec.c a simple student record demo.
- This program:
- 1- Prints the current working directory.
- 2- Asks user to specify the file name containing the records of students.
- 3- Reads the record from the file.
- 4- Prints each record with statistics
- Each line in the file is a none-empty comma-separated values containing a
- student name and three float numbers in the range [0.00, 100.0] indicating the
- result of three exams for each students. Example:
- John Smith,71,68,92
- Paul Hunter,69,73,82
- Mary McDana,81,83,90
- There are variuos constants in the program which could be altered to change the
- limits of the program. For example, MAX_STUDENT_RECS indicates the maximum number
- of strudent records which is defined to be 20. You may alter it to any desirable
- value from 1 to any positive valid int value.
- Tested with Visual Studio .Net 2008 Standard C, Quincy 2005 and Dev C/C++ 4.9
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <direct.h>
- #include <string.h>
- /*
- The size of buffers to hold directory path.
- (The maximum number of characters allowed for a directory (folder) path
- */
- #define MAX_PATH_LENGTH 2048
- /*
- The maximum length of file name.
- */
- #define MAX_FILENAME_LENGTH 256
- /*
- The maximum length of each record line.
- */
- #define MAX_LINE_LENGTH 256
- /*
- The maximum number of student records.
- */
- #define MAX_STUDENT_RECS 20
- /*
- A constant used as null character. The char representation, ' \ 0 '
- has an issue when published in a blog, causeing the code to produce
- compile error.
- */
- const char NULL_CHAR = (char) 0;
- /*
- A struct record definition. Each student record will be saved in one
- variable of student_rec type. (Actually an array of this).
- */
- struct student_rec
- {
- /* The name of student */
- char name [MAX_LINE_LENGTH];
- float exam1; /* the result of exam1 */
- float exam2; /* the result of exam2 */
- float exam3; /* the result of exam3 */
- };
- /*
- The array holding all the student records.
- */
- struct student_rec records [MAX_STUDENT_RECS];
- /*
- The main program. We have not used the command-line arguments in this demo.
- */
- int main(int argc, char *argv[])
- {
- /* A buffer to hold the current working directory. */
- char buffer [MAX_PATH_LENGTH];
- /* The string to hold the filename of record file. */
- char filename [MAX_FILENAME_LENGTH];
- /* The String to hold each record line in loop. */
- char line [MAX_LINE_LENGTH];
- /* A string to hold the caption for record statistics. */
- char stat [MAX_FILENAME_LENGTH];
- /* used to trim the new-line characters off the line. */
- int len;
- /* The file interface to work with record file. */
- FILE* fp;
- /* a pointer to tokenize each record field. */
- char *pTok;
- /* The primary delimiter list to tokenize the fields of each record. */
- const char *delim = ",";
- /* The alternative delimiter list to tokenize the fields of each record. */
- /* (used for last token/field). */
- const char *delimLast = "\r\n";
- /* variables to hold the sums of each exams results. */
- float exam1, exam2, exam3;
- /* count : the total number of student records. */
- /* n : misc for loop variable. */
- int count, n;
- /* gets the current working directory, and prints it. */
- _getcwd(buffer, MAX_PATH_LENGTH);
- printf ("Current Working Directory: %s\n", buffer);
- /* reads the file name from keyboard. */
- printf ("Enter File name: ");
- fgets(filename, MAX_FILENAME_LENGTH - 1, stdin);
- /* trims the appnded new-line at the end. */
- len = strlen (filename);
- if (filename [len - 1] == '\n')
- filename [len - 1] = NULL_CHAR;
- /* echo back the read file name. */
- printf ("Filename: %s\n", filename);
- /* opens the record file: */
- if ((fp = fopen (filename, "r")) == NULL)
- {
- /* prints the error in std error device if failed. */
- fprintf (stderr, "*** Cannot open file %s for input.\n", filename);
- system ("PAUSE");
- return 1; /* exits with error status 1 */
- }
- /* reads all the records up to a maximum number. */
- for (count = 0; count < MAX_STUDENT_RECS; )
- {
- /* reads the next line; if no more lines, breaks. */
- if (fgets (line, MAX_LINE_LENGTH - 1, fp) == NULL)
- break;
- /* obtins the first token/field (name) */
- pTok = strtok (line, delim);
- /* If no token, continues (reading next line) */
- if (pTok == NULL || *pTok == NULL_CHAR || *pTok == '\n')
- continue;
- /* if a record, copies the first field/token into name field of the
- next student record */
- /* element in array. */
- strcpy (records [count].name, pTok);
- /* obtain the next token/field (exam1) */
- pTok = strtok(NULL, delim);
- /* It shouldn't be a null or empty record. */
- if (pTok == NULL || *pTok == NULL_CHAR || *pTok == '\n')
- {
- /* If it is, skips this line with a warning. */
- fprintf (stderr, "*** Warning! Skipping invalid record; name : %s\n",
- records [count].name);
- continue;
- }
- /* converts exam1 to a float number and save it in record element. */
- records [count].exam1 = atof (pTok);
- /* obtain the next token/field (exam2) */
- pTok = strtok(NULL, delim);
- /* It shouldn't be a null or empty record. */
- if (pTok == NULL || *pTok == NULL_CHAR || *pTok == '\n')
- {
- /* If it is, skips this line with a warning. */
- fprintf (stderr, "*** Warning! Skipping invalid record; name : %s\n",
- records [count].name);
- continue;
- }
- /* converts exam2 to a float number and save it in record element. */
- records [count].exam2 = atof (pTok);
- /* obtain the final token/field (exam3) */
- pTok = strtok(NULL, delimLast);
- /* It shouldn't be a null or empty record. */
- if (pTok == NULL || *pTok == NULL_CHAR || *pTok == '\n')
- {
- /* If it is, skips this line with a warning. */
- fprintf (stderr, "*** Warning! Skipping invalid record; name : %s\n",
- records [count].name);
- continue;
- }
- /* converts exam3 to a float number and save it in record element. */
- records [count].exam3 = atof (pTok);
- /* because all lines may not be a valid record,
- we increment count here, not in for loop. */
- count++;
- }
- /* Now, displaying the records, and accumulating the results exam by
- exam for all students: */
- /* Initially, all 0. */
- exam1 = exam2 = exam3 = 0.0f;
- /* Displaying all records */
- for (n = 0; n < count; n++)
- {
- printf ("%30s\t%6.2f\t%6.2f\t%6.2f\t%6.2f\n", records [n].name,
- records [n].exam1, records [n].exam2, records [n].exam3,
- (records [n].exam1 + records [n].exam2 + records [n].exam3) / 3.0f);
- /* summing up results. */
- exam1 += records [n].exam1;
- exam2 += records [n].exam2;
- exam3 += records [n].exam3;
- }
- /* prints the stat. */
- sprintf(stat, "%d records, Average:", count);
- printf ("%30s\t%6.2f\t%6.2f\t%6.2f\n", stat, exam1 / (float) count,
- exam2 / (float) count, exam3 / (float) count);
- system ("PAUSE");
- return 0; /* exist with status OK. */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement