How can I process command line arguments in C?

[亡魂溺海] 提交于 2019-12-25 00:14:35

问题


I wanted to know how it would be possible to get other input through the command line? I want to look for "-w" and a number, so it would look like "-w60" and "-s". This input is given through the command line, so it would look like this:


c:\Users\Username\Desktop> wrapfile.exe -w5 -s test.txt

Output should look like this:

Hello  
,  
this  
is a  
test

What the -w5 and -s mean is:

-w5 = width (can only display 5 characters at a time)

-s = spacing (Include spacing, so fit as many whole words as you can)

I want to create a function that scans for these two characters, and if anyone knows how to format the output so it does what it needs to do, that would also be helpful.

I'm just a wee bit confused, I've been working on this program for a while and I just want to learn how these things can be scanned and used properly.

Here is my current code that takes in an unlimited amount of text files from the command line:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{

    int l = 1;
    while(l != argc)
    {
        FILE *fp;    

        fp = fopen(argv[l], "rb");
        l++;

        if (fp != NULL) 
        {
        int i = 1;
        do
        {
            i = fgetc(fp);   
            printf("%c",i);
            printf(" ");
        }
        while(i!=-1);
        fclose(fp);
        }
        else
        {
        printf("Error.\n");
        }
    }
}

/*

void scanningForWS(int argc, char **argv)
{
}

*/

回答1:


If you pass -w5 -s test.txt to your program your argv's are:

argv[0] = "wrapfile.exe" 
argv[1] = "-w5" 
argv[2] = "-s" 
argv[3] = "test.txt"

So:

int l = 1;
fp = fopen(argv[l], "rb");

is not what you want for sure.

For illustration purposes... in order to print to a "restricted" width you can do something like this:

char * h = "this is a string longer than width"; // you'd get this from your file
int width = argv[1][2] - '0'; // you wouldn't hardcode this...
int count;

for(count = 0; count < strlen(h); count++){
    if((count % width) < width - 1)
        printf("%c", str[count];
    else
        printf("%c\n", str[count];
}



回答2:


I find getopt cumbersome to use. Writing your own tests is not too difficult. For example:

#include <string.h>
#include <stdio.h>

int main(int argc, char **argv) {
   int haveSpacing = 0;
   int haveWidth = 0;
   FILE *fp = 0;
   while (*++argv) {
      if (!strcmp(*argv, "-s")) { // check for -s switch
         haveSpacing = 1;
      }
      else if (sscanf(*argv, "-w%d", &haveWidth) == 1) { // check for -wxx
      }
      else if (**argv == '-') { // reject anything else beginning with "-"
         printf("invalid switch %s\n", *argv);
         return 1;
      }  
      else if (argv[1]) { // filenaname must be last arg, so arg[1] must be NULL
         printf("invalid arg %s\n", *argv);
         return 1;
      }
      else if (!(fp = fopen(*argv, "rb"))) { // open last arg, the filename
         perror(*argv);
         return 1;
      }
   }
   if (!fp) {
      printf("missing filename\n");
      return 1;
   }

   // ...
   return 0;
}


来源:https://stackoverflow.com/questions/13766985/how-can-i-process-command-line-arguments-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!