1
0

answer05.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "pa05.h"
  5. /*
  6. * Read a file of integers.
  7. *
  8. * Arguments:
  9. *
  10. * filename: the name of a file that contains a list of integers (one
  11. * per line)
  12. *
  13. * numInteger: pointer to store the number of integers in the
  14. * file. You need to update the value stored at the address which is
  15. * the pointer's value. If the function fails to update the value, it
  16. * is considered reading the file has failed.
  17. *
  18. * Returns:
  19. *
  20. * a array of integers from the file, or NULL if *any* error is
  21. * encountered.
  22. *
  23. * Hint: use fscanf
  24. *
  25. * You do NOT know how many integers are in the file until you have
  26. * read it. Once you know how many integers there are, you can modify
  27. * the "numberOfIntegers" variable. (Note that this is a pointer, not
  28. * an integer) You must allocate memory to store the integers.
  29. *
  30. * Once memory is allocated to store the integers, you will need to
  31. * re-read the values from the file. To do this, you must reset the
  32. * file pointer to the beginning of the file using the function
  33. * "fseek".
  34. *
  35. * You should not use rewind (if you have learned it somewhere). The
  36. * difference of rewind and fseek is that rewind does not tell you
  37. * whether it fails.
  38. *
  39. * One way to read integeres is to use the "fscanf" function. It is
  40. * easier than reading one character at a time by using fgetc.
  41. *
  42. * You must use malloc in this function.
  43. *
  44. * Some old books encourage readers to type cast in front of malloc,
  45. * something like
  46. *
  47. * int * ptr = (int *) malloc(sizeof(int) * size);
  48. *
  49. * Type cast is no longer needed and in fact is discouraged now. You
  50. * should *NOT* type cast malloc. It is discouraged even though it is
  51. * not wrong.
  52. *
  53. * You will receive zero if you allocate a large array whose size is
  54. * independent of the number of integers in the file. For example, if
  55. * you write this
  56. *
  57. * int array[10000];
  58. *
  59. *
  60. */
  61. int * readInteger(char * filename, int * numInteger)
  62. {
  63. return NULL;
  64. }
  65. /* ----------------------------------------------- */
  66. /*
  67. * Read a file of strings. This is similar to readInteger. However,
  68. * each string is an array of characters. The function needs to
  69. * allocate memory for an array of strings, i.e., array of arrays of
  70. * characters. In other words, this is a two-dimensional array (thus,
  71. * char **).
  72. *
  73. * Arguments:
  74. *
  75. * filename: the name of a file that contains a list of strings (one
  76. * per line)
  77. *
  78. * numString: pointer to store the number of strings in the
  79. * file. You need to update the value stored at the address which is
  80. * the pointer's value. If the function fails to update the value, it
  81. * is considered reading the file has failed.
  82. *
  83. * Returns:
  84. *
  85. * a array of strings from the file, or NULL if *any* error is
  86. * encountered.
  87. *
  88. * Hint: use fgets.
  89. *
  90. * Remember that an array of strings is a two-dimensional array of
  91. * characters
  92. *
  93. * You do NOT know how many strings are in the file until you have
  94. * read it. Once you know how many strings there are, you can modify
  95. * the "numberOfStrings" variable. (Note that this is a pointer, not
  96. * an string) You must allocate memory to store the strings.
  97. *
  98. * Once memory is allocated to store the strings, you will need to
  99. * re-read the values from the file. To do this, you must reset the
  100. * file pointer to the beginning of the file using the function
  101. * "fseek".
  102. *
  103. * You should not use rewind (if you have learned it somewhere). The
  104. * difference of rewind and fseek is that rewind does not tell you
  105. * whether it fails.
  106. *
  107. * One way to read stringes is to use the "fscanf" function. It is
  108. * easier than reading one character at a time by using fgetc.
  109. *
  110. * You must use malloc in this function.
  111. *
  112. * Some old books encourage readers to type cast in front of malloc,
  113. * something like
  114. *
  115. * int * ptr = (int *) malloc(sizeof(int) * size);
  116. *
  117. * Type cast is no longer needed and in fact is discouraged now. You
  118. * should *NOT* type cast malloc. It is discouraged even though it is
  119. * not wrong.
  120. *
  121. * You will receive zero if you allocate a large array whose size is
  122. * independent of the number of strings in the file. For example, if
  123. * you write this
  124. *
  125. * char array[10000];
  126. *
  127. *
  128. */
  129. char * * readString(char * filename, int * numString)
  130. {
  131. return NULL;
  132. }
  133. /* ----------------------------------------------- */
  134. /*
  135. * print an array of integers, one integer per line
  136. */
  137. void printInteger(int * arrInteger, int numInteger)
  138. {
  139. }
  140. /* ----------------------------------------------- */
  141. /*
  142. * print an array of strings, one string per line
  143. *
  144. * Hint: printf("%s" ... can print a string
  145. */
  146. void printString(char * * arrString, int numString)
  147. {
  148. }
  149. /* ----------------------------------------------- */
  150. /*
  151. * release the memory occupied by the array of integers
  152. */
  153. void freeInteger(int * arrInteger, int numInteger)
  154. {
  155. }
  156. /* ----------------------------------------------- */
  157. /*
  158. * release the memory occupied by the array of strings
  159. *
  160. * Hint: an array of strings is a two-dimensional array of characters
  161. */
  162. void freeString(char * * arrString, int numString)
  163. {
  164. }
  165. /* ----------------------------------------------- */
  166. /*
  167. * Write integers to a file, one integer per line
  168. *
  169. * Arguments:
  170. *
  171. * filename: the name of a file.
  172. * arrInteger: an array of integers
  173. * numInteger: the number of integers
  174. *
  175. * Returns:
  176. * 1 if saving to the file successfully
  177. * 0 if *any* error is encountered
  178. *
  179. * Hint: use fprintf(... %d
  180. *
  181. */
  182. int saveInteger(char * filename, int * arrInteger, int numInteger)
  183. {
  184. return 0;
  185. }
  186. /* ----------------------------------------------- */
  187. /*
  188. * Write strings to a file, one string per line
  189. *
  190. * Arguments:
  191. *
  192. * filename: the name of a file.
  193. * arrString: an array of strings
  194. * numString: the number of strings
  195. *
  196. * Returns:
  197. * 1 if saving to the file successfully
  198. * 0 if *any* error is encountered
  199. *
  200. * Hint: use fprintf(... %s
  201. *
  202. */
  203. int saveString(char * filename, char * * arrString, int numString)
  204. {
  205. return 0;
  206. }
  207. /* ----------------------------------------------- */
  208. /*
  209. * sort an arry of integers by calling the built-in qsort function in
  210. * the C library. You need to provide the comparison function. Please
  211. * read the Linux manual about qsort
  212. *
  213. */
  214. void sortInteger(int * arrInteger, int numInteger)
  215. {
  216. }
  217. /* ----------------------------------------------- */
  218. /*
  219. * sort an arry of strings by calling the built-in qsort function in
  220. * the C library. You need to provide the comparison function. Please
  221. * read the Linux manual about qsort
  222. *
  223. * Hint: use strcmp in the comparison function
  224. *
  225. */
  226. void sortString(char * * arrString, int numString)
  227. {
  228. }