Browse Source

Added file reading demo

Joshua Bicking 8 năm trước cách đây
mục cha
commit
e5130f2058
2 tập tin đã thay đổi với 34 bổ sung0 xóa
  1. 28 0
      src/fileReadTest.c
  2. 6 0
      src/wordList

+ 28 - 0
src/fileReadTest.c

@@ -0,0 +1,28 @@
+#define _GNU_SOURCE
+#include <stdio.h>
+#include <stdlib.h>
+
+int main(){
+    FILE *fp = fopen("wordList", "r");
+    //int wordCount = 3; //TODO remove temp declaration
+    int wordCount =0;
+    int c;
+    while ( (c=fgetc(fp)) != EOF)
+        if (c == '\n')
+            wordCount++;
+
+    rewind(fp);
+
+    printf("%d newlines\n", wordCount);
+
+
+    char words[wordCount][15];
+
+    int j=0;
+    while (fgets(words[j], sizeof(words[j]), fp))
+            j++;
+    
+    for(int i=0; i<wordCount; i++)
+        printf("%d: %s",i,words[i]);
+
+}

+ 6 - 0
src/wordList

@@ -0,0 +1,6 @@
+hello
+goodbye
+panda
+atest
+anothertest
+howmanywords