Advertisement
Guest User

Untitled

a guest
Feb 9th, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. // ConsoleApplication19.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <stdio.h>
  6. #include "string.h"
  7. #include "stdlib.h"
  8.  
  9. typedef struct
  10. {
  11. char firstName[50];
  12. char lastName[50];
  13. long id;
  14. char english, french, maths, philosophy;
  15. } result;
  16.  
  17. // array of pointers to 'result' structures - assuming that there is 100 or less records in the data file
  18. result *results[100];
  19.  
  20. // number of records read from the file
  21. int numResults = 0;
  22.  
  23. // read file and populate the results
  24. // you will need to use malloc to allocate a new 'result' structure for each record read from the file
  25. // the *result pointer returned by malloc will be stored in the next member of the array *results[]
  26. int readFile(char *fileName);
  27.  
  28. // set all the pointers in *results[] to NULL before reading in the file
  29. void initialiseResults();
  30.  
  31. // function to print an individual member of the *results[] array
  32. void printResult(result *aResult);
  33.  
  34. int main()
  35. {
  36. char fileName[] = "D:\\results.txt";
  37. int i = 0;
  38.  
  39.  
  40. void initialiseResults();
  41.  
  42. if (!readFile(fileName))
  43. {
  44. printf("File could not be opened !!\n");
  45. return 0;
  46. }
  47. printf("%*s\t", 12, "First Name");
  48. printf("%*s\t", 12, "Second Name");
  49. printf("ID\tEnglish\tMaths\tFrench\tPhilosophy\n");
  50. printf("%*s\t", 12, "==========");
  51. printf("%*s\t", 12, "===========");
  52. printf("===\t=======\t=====\t======\t==========\n");
  53.  
  54.  
  55.  
  56. while (results[i] != NULL)
  57. {
  58. printResult(results[i]);
  59. i++;
  60. }
  61.  
  62. return 0;
  63. }
  64.  
  65. void printResult(result *aResult)
  66. {
  67. printf("%*s\t", 12, aResult->firstName);
  68. printf("%*s\t", 12, aResult->lastName);
  69. printf("%li\t%c\t%c\t%c\t%c\n", aResult->id, aResult->english, aResult->maths, aResult->french, aResult->philosophy);
  70. }
  71.  
  72. void initialiseResults()
  73. {
  74. int i = 0;
  75. while (i < 100)
  76. {
  77. results[i] = NULL;
  78. i++;
  79. }
  80. }
  81.  
  82. int readFile(char *fileName)
  83. {
  84. int i = 0;
  85. FILE *fptr;
  86. char buffer[100];
  87.  
  88. fptr = fopen(fileName, "r");
  89. if (fptr == NULL)
  90. {
  91. printf("Error opening file\n");
  92. return 0;
  93. }
  94. fgets(buffer, 100, fptr);
  95.  
  96. while(!feof(fptr))
  97. {
  98. results[i] = (result*)malloc(sizeof(result));
  99. fscanf(fptr, "%li\t%s\t%s\t%c\t%c\t%c\t%c\n", &results[i]->id, &results[i]->lastName, &results[i]->firstName, &results[i]->english, &results[i]->maths, &results[i]->french, &results[i]->philosophy);
  100. i++;
  101. }
  102. return 1;
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement