Shanix

Untitled

Mar 15th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.47 KB | None | 0 0
  1. /**
  2. * TODO: this
  3. */
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <ctype.h>
  9.  
  10.  
  11. /* This function will read a line of text from the given file, returning it as a string stored in a dynamically allocated array. It will stop reading when it encounters a newline or the end-of-file. If it can't read anything before reaching the end-of-file, it will return NULL.
  12.  
  13. Internally, this funciton will use a resizable array. It will allocate a small array to hold the line of text. As it reads characters, it will grow the array as necessary to be able to hold the whole line. */
  14. char *readLine( FILE *fp )
  15. {
  16. char c = 0;
  17.  
  18. int capacity = 5;
  19. char *line = malloc( capacity );
  20.  
  21. //null terminating string
  22. for ( int i = 0; i < capacity; i++ ) {
  23. line[i] = '\0';
  24. }
  25. int index = 0;
  26.  
  27. while ( fscanf( fp, "%c", &c ) == 1 ) {
  28. if ( c == EOF ) {
  29. return NULL;
  30. }
  31. if ( c == '\n' ) {
  32. return line + '\0';
  33. } else {
  34. line[index] = c;
  35. index++;
  36. if ( index == capacity ) {
  37. capacity *= 2;
  38. line = (char *)realloc(line, capacity);
  39.  
  40. //getting those null terminators in
  41. for ( int i = index; i < capacity; i++ ) {
  42. line[i] = '\0';
  43. }
  44. }
  45. }
  46. }
  47.  
  48. return NULL;
  49.  
  50. }
  51.  
  52. /* This funciton will read the text of an entire file, storing it as a dynamically allocated array of pointers to dynamically allocated strings. The function will open the file with the given name, read all its lines, return a pointer to the array of char pointers holding the contents of the file's lines (thus the pointer-to-pointer-to-char return type) and fill in the pass-by-reference count parameter with the number of lines read from the file.
  53.  
  54. Internally, this function will use a resizable array of char pointers to store the contents of the file. As it reads additional lines from the file, it will need to be able to resize its array of pointers to be able to keep pointer to all the lines. It can use the readLine() function to read the text of each line of the file.*/
  55. char **readLines( const char *fileName, int *count )
  56. {
  57.  
  58. FILE *dictionaryFile = fopen( fileName, "r" );
  59. char *c = readLine( dictionaryFile );
  60.  
  61. int capacity = 5;
  62. char **lines = malloc( capacity * sizeof(char *) );
  63. int index = 0;
  64.  
  65. while ( c != NULL || c[0] != EOF || !isspace( c[0] ) ) {
  66. c[ strlen( c ) ] = '\0';
  67. lines[index] = c;
  68. index++;
  69. ( *count )++;
  70. if ( index == capacity ) {
  71. capacity *= 2;
  72. lines = (char **)realloc(lines, capacity * sizeof(char *));
  73. }
  74. c = readLine( dictionaryFile );
  75. if ( c == NULL ) {
  76. break;
  77. }
  78. if ( c[0] == '\n' ) {
  79. return NULL;
  80. }
  81. }
  82. fclose( dictionaryFile );
  83. return lines;
  84. }
  85.  
  86.  
  87. /* This function will free the memory for the type of two-dimensional array returned from readLines(). Its parameters are an array of pointers to strings and a count of the number of strings in the array (just like what readLines gives you). It will need to free the memory for each string, then free the array of pointers. */
  88. void freeLines( char **lines, int count )
  89. {
  90. for ( int i = count; i >= 0; i-- ) {
  91. free( lines[i] );
  92. }
  93. free( lines );
  94. }
Add Comment
Please, Sign In to add comment