]> Creatis software - demoSorina.git/commitdiff
Added a second argument for the file name master
authorSorina Pop <sorina.pop@creatis.insa-lyon.fr>
Thu, 19 Nov 2020 16:43:56 +0000 (17:43 +0100)
committerSorina Pop <sorina.pop@creatis.insa-lyon.fr>
Thu, 19 Nov 2020 16:43:56 +0000 (17:43 +0100)
hello.c

diff --git a/hello.c b/hello.c
index 14944549b83950d0a84b9b6d03a019e5b061ca43..a5e257371e2b0e352092c4df4311932aa29c9c8b 100644 (file)
--- a/hello.c
+++ b/hello.c
@@ -2,27 +2,23 @@
 #include <stdlib.h>
 
 int main(int argc, char *argv[]) {
-
-        /* This is a comment in C */
-
-        printf("This is a C program\n");
-       FILE *f = fopen("hello.txt", "w");
-       if (f == NULL){
-           printf("Error opening file!\n");
-           exit(1);
-       }
-
-       if (argc>1) {
-               printf("Hello %s !\n", argv[1]);
-               fprintf(f, "Hello %s !\n",  argv[1]);
-       }else{
-               printf("Hello world !\n");
-               fprintf(f, "Hello world !\n");
-       }
-
-               /* print some text */
-       fclose(f);
-        printf("This is the end of my C program\n");
-       exit(0);
+       if (argc == 3) {
+                /*Opening the file provided as a second argument*/
+                FILE *f = fopen(argv[2], "w");
+                if (f == NULL){
+                  printf("Error opening file!\n");
+                  exit(1);
+                }
+                /*Saying hello to the name provided as a first argument*/
+                printf("Hello %s !\n", argv[1]);
+                fprintf(f, "Hello %s !\n",  argv[1]);
+                fclose(f);
+        }else{
+                printf("This is a C program expecting 2 arguments: the name to say hello to and the output file name\n");
+                printf("You have not provided the correct number of arguments\n");
+                exit(1);
+        }
+        exit(0);
 
 }
+