fileReadTest.c 534 B

12345678910111213141516171819202122232425262728
  1. #define _GNU_SOURCE
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. int main(){
  5. FILE *fp = fopen("wordList", "r");
  6. //int wordCount = 3; //TODO remove temp declaration
  7. int wordCount =0;
  8. int c;
  9. while ( (c=fgetc(fp)) != EOF)
  10. if (c == '\n')
  11. wordCount++;
  12. rewind(fp);
  13. printf("%d newlines\n", wordCount);
  14. char words[wordCount][15];
  15. int j=0;
  16. while (fgets(words[j], sizeof(words[j]), fp))
  17. j++;
  18. for(int i=0; i<wordCount; i++)
  19. printf("%d: %s",i,words[i]);
  20. }