从C程序中使用命令行转换输入 [英] convert the input from c program using command line

查看:93
本文介绍了从C程序中使用命令行转换输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了下面的C code,它可以从终端输出走线的投入。

I have written the following c code that can take line inputs from terminal and output that.

    #include <stdio.h>
    static char string[200];
    int main(){
      printf("Enter your input lines:" );
      fgets(string,200,stdin);
      printf("%s\n",string);
      return 0;
}

当我通过这个程序获得输入线,I输出上terminal.I想让命令行上一个特殊的命令选项,如 -u -l <​​/ code>,这将改变我的输入线路的所有字符(通过C程序),以大写字母分别为小写。

As I get the input lines through this program, I output that on terminal.I want to make a special command option on command line such as -u or -l that will change all characters in my input lines(through c program) to uppercase and lower case respectively.

EDIT1:
我没有在Windows提示以下内容(而不是Linux虽然我最后的打算是Linux):

I did the following in windows prompt(not linux although my final intend is linux):

#include <stdio.h>
  char string[200];
    int main(int argc, char *argv[]){
      printf("Enter your input lines:" );
      fgets(string,200,stdin);
      int i =0;
     for(;i<argc ;i =i+1 ){ 

      if (argv[i] == "-u"){ 
        toupper(string);
       }
       if(argv[i] == "-l"){
       tolower(string);
       }
       else
       printf("Invalid command line option.") ;
     }

      printf("%s\n",string);
      return 0;
}

EDIT2:

This is my latest one:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
  static char line[100];
  char a ='-u';
  char b = '-l';
    int main(int argc, char *argv[]){

      printf("Enter your input lines: \n" );
      fgets(line,100,stdin);
      int i =1;

     for(;i<argc ;i =i+1 ){ 
        if(argv[i] == a)
     { 

        printf("%s\n\n",toupper(line));

       }
      else if(argv[i] == b){

       printf("%s\n\n",tolower(line));


       }
       else{
       printf("Invalid command line option.") ;
       }

     }
        printf("\n%s\n",line);

      return 0;
}

这一次运行和执行,但不给大写或当我输入小写-u或在提示-l。

This one runs and executes but does not give uppercase or lowercase when I type -u or -l in the prompt.

推荐答案

行政, TOUPPER tolower的被发现在的#include&LT;&文件ctype.h GT; 不是&LT;&string.h中GT; 。 (一个即时错误)此外, TOUPPER tolower的在一个单一的字符的在操作时间,而不是整个的字符串的。你的下一个错误将是编译器告诉你,(例如,你正试图调用 TOUPPER / tolower的的char * 代替的字符

Chief, toupper and tolower are found in #include <ctype.h> not <string.h>. (one immediate error) Further, toupper and tolower operate on a single character at a time, not an entire character string. Your next error will be the compiler telling you that (e.g. you are attempting to call toupper/tolower with char * instead of char.

在C,你不能比较的字符串的有 == ,你只能比较的字符的(一次一个时间)。比较字符串,则必须使用 STRCMP STRNCMP (或循环每个字符和比较每个)。但是,您可以简单地检查每一个命令行参数的第二个字符为'U'L(例如: 如果(的argv [I] [1] =='U')

In C, you cannot compare strings with ==, you can only compare characters (one at a time). To compare strings, you must use strcmp or strncmp (or loop over each character and compare each). You can however, simply check the second character of each command line argument for 'u' or 'l' (e.g. if (argv[i][1] == 'u'))

每当你读取输入,你的必须验证的你实际收到的是你所期待,例如而(与fgets(字符串,MAXC,标准输入))。如果什么用户通过生成 EOF ,例如取消输入 CTRL + D (在Linux上)或 CTRL + Z (上windoze)?

Whenever you read input, you must validate you actually received what you were expecting, e.g. while (fgets (string, MAXC, stdin)). What if the user canceled input by generating an EOF, e.g. ctrl+d (on Linux) or ctrl+z (on windoze)?

把这些拼在一起,看来你是打算做类似下面,让您输入(或读取)多条线路,并根据参数 -u (转换为大写)或 -l <​​/ code>(转换成小写)。它将两者都做,如果你给这两个 -u -l <​​/ code>。

Putting those pieces together, it appears you were intending to do something like the following that will allow you to input (or read) multiple lines and depending on the arguments -u (convert to upper-case) or -l (convert to lower-case). It will do both if you give both -u and -l.

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

#define MAXC 200

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

    char string[MAXC] = "";
    size_t i = 1;                   /* note 1 to skip argv[0] */
    int lower = 0, upper = 0;       /* upper/lower cvt flags  */

    for (; (int)i < argc; i++) {
        if (argv[i][1] == 'u')      /* you can only compare a char with == */
            upper = 1;
        else if (argv[i][1] == 'l')
            lower = 1;
        else
            fprintf (stderr, "error: invalid option '%s'\n", argv[i]);
    }

    printf ("enter input lines [ctrl+d (ctrl+z on windoze) to end]\n");

    while (fgets (string, MAXC, stdin)) {  /* fgets reads/includes '\n' */

        printf ("string : %s", string);
        size_t len = strlen (string);

        if (upper) {    /* you can only compare a char with == */
            for (i = 0; i < len; i++)
                if (islower (string[i]))
                    string[i] = toupper (string[i]);
            printf (" upper : %s", string);
        }

        if (lower) {
            for (i = 0; i < len; i++)
                if (isupper (string[i]))
                    string[i] = tolower (string[i]);
            printf (" lower : %s", string);
        }
    }

    printf ("\nthat's all folks....\n");

    return 0;
}

示例使用/输出

$ ./bin/upperlower -u -l
enter input lines [ctrl+d (ctrl+z on windoze) to end]
The Quick Brown Fox Jumps Over A Lazy Dog.
string : The Quick Brown Fox Jumps Over A Lazy Dog.
 upper : THE QUICK BROWN FOX JUMPS OVER A LAZY DOG.
 lower : the quick brown fox jumps over a lazy dog.

that's all folks....

看一下,然后告诉我知道,如果你有任何问题。

Look it over and let me know if you have any questions.

这篇关于从C程序中使用命令行转换输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆