TP 12 exo2: Basic file manipulations
[TD_C.git] / TP_12 / exo2 / exo2.c
diff --git a/TP_12/exo2/exo2.c b/TP_12/exo2/exo2.c
new file mode 100644 (file)
index 0000000..199d299
--- /dev/null
@@ -0,0 +1,30 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+int main() {
+    char ch;
+    char* fname;
+    FILE* fp;
+
+    printf("Enter the name of the file you wish to see\n");
+    int prt = scanf("%s", fname);
+    /* if (prt != 0) {
+        perror("Error entering the file name.\n");
+        exit(EXIT_FAILURE);
+    } */
+
+    fp = fopen(fname, "r"); /* read mode */
+    if (fp == NULL) {
+        perror("Error while opening the file.\n");
+        exit(EXIT_FAILURE);
+    }
+
+    printf("The content of %s file is :\n", fname);
+
+    while((ch = fgetc(fp)) != EOF)
+         printf("%c", ch);
+
+    fclose(fp);
+
+    return EXIT_SUCCESS;
+}