简体   繁体   中英

How to execute C code which requires strings and file names as input through Terminal?

I am newbie in C language and I am writing code which works with files. But I cannot execute it through Terminal, as I will be needing to pass strings and file names through terminal commands. Ps: I couldnt find proper solution googling myself, sorry if this question was asked before.

Here is my code:

     #include <stdio.h>
     #include <string.h>
     #include<stdlib.h>
     #define N 102
     #define n 21

     void replace(char *s, int l, int r, char *s2);

     int main(int argc, const char * argv[]) {
         if (argc!=5) {
             fprintf(stderr, "Wrong input format!\n");
             exit(1);
         }
         FILE *fp1, *fp2;
         fp1=fopen(argv[2], "r");
         fp2=fopen(argv[4], "w");
         if (fp1==NULL || fp2==NULL) {
              fprintf(stderr, "Wrong input!\n");
              exit(1);
         }
         char s1[n], s2[n];
         strcpy(s1, argv[1]);
         strcpy(s2, argv[3]);
         char line[N], line2[N];
         int l,r, ok;
         while (fgets(line, N, fp1)) {
             int i=0;
             while (line[i]!='\0') {
                 l=r=0;
                 ok=1;
                 int k=0, j=i;
                 while(j<sizeof(s1)){
                     if (line[j]!=s1[k]) {
                         ok=0;
                     }
                     k++;
                 }
                 if (ok) {
                     replace(line, i, j, s2);
                 }
                 i++;
             }
             fputs(line, fp2);
         }

         while (fgets(line2, N, fp2)) {
             puts(line2);
         }
         return 0;
     }

     void replace(char *s, int l, int r, char *s2){
         char line[N], line2[N];
         strncat(line, s, l);
         strcat(line, s2);
         int k=0, j=r+1;
         while (s[j]!='\0') {
             line2[k]=s[j];
             k++;
             j++;
         }
         strcat(line, line2);
         strcpy(s, line);
     }

In your terminal you can compile you code with the command gcc myfile.c (check the man of gcc if you need flags).

Once it is compiled, you can execute the executive file, which is named a.out (if you didn't rename it), by typing ./a.out . This will execute your program.

If you need arguments in your main you can simply type : ./a.out myArgv1 myArgv2 , and so on. Your arguments can be strings, filenames, ect... This works for Linux and MacOS.

First of all, you have to check that you have installed gcc (C compiler). Write: gcc --version and you should see your version. Once you are sure about having gcc, compile your program:

gcc yourfile.c -o nameofexecutable Note: With that option you can choose whatever the name you want for your executable file.

Finally, execute your file with the desired arguments.

./nameofexecutable argument1 argument2 argument3 Note: Obviously you can introduce 0, 1, 2 or whatever list of arguments.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM