简体   繁体   中英

Validating input and Word Count in C#

I have to do an C program which need to run in UBuntu system. For different number of words for command I input will give different print outs. Moreover, specific words in specific positions would also give different print outs for example:

root@linux:~# ./recover
Usage: ./recover -d [device filename] [other arguments]
-i Print boot sector information
-l List all the directory entries
-r filename [-m md5] File recovery with 8.3 filename
-R filename File recovery with long filename

root@linux:~# ./recover -d fat32.disk -i
Number of FATs = 2
Number of bytes per sector = 512
Number of sectors per cluster = 8
Number of reserved sectors = 32
Number of allocated clusters = 1000
Number of free clusters = 8000

Now The code i have already done:

int main () {
    int argc;
    char *argv[50];
    char deviceFilename[512];
    char recoverFilename[512];
    int i, j;
    while (1)
    {

        if (argc < 4 || argc == 6 || argc > 7)
        {
            printUsage(argv[0]);
        }
        else if (strcmp("-i", argv[3]) == 0 || strcmp("-l", argv[3]) == 0)
        {
            if (argc > 4)
                printUsage(argv[0]);
            else if (argc == 4 && (strcmp("-i", argv[3]) == 0))
            {
                strcpy(deviceFilename, argv[2]);
                if (initDisk(deviceFilename))
                {
                    printInfo();
                    fclose(fp);
                }
                else
                    printUsage(argv[0]);
            }
            else if (argc == 4 && (strcmp("-l", argv[3]) == 0))
            {
                strcpy(deviceFilename, argv[2]);
                if (initDisk(deviceFilename))
                {
                    listRootDir();
                    fclose(fp);
                }
                else
                    printUsage(argv[0]);
            }
        }
        else if (strcmp("-r", argv[3]) == 0)
        {
            if (argc == 4)
                printUsage(argv[0]);
            else
            {
                if (argc == 7 && strcmp("-m", argv[5]) == 0)
                {
                    strcpy(deviceFilename, argv[2]);
                    strcpy(recoverFilename, argv[4]);
                    printUsage(argv[0]);
                }
                else if (argc == 5)
                {
                    strcpy(deviceFilename, argv[2]);
                    strcpy(recoverFilename, argv[4]);
                    if (initDisk(deviceFilename))
                    {
                        if (checkMatch(argv[4]) == 1)
                            recover(argv[4]);
                        fclose(fp);
                    }
                    else
                        printUsage(argv[0]);
                }
                else
                    printUsage(argv[0]);
            }
        }
        else
        {
            printUsage(argv[0]);
        }
    }
    return 1;
}

./recover is one word and ./recover -d fat32.disk -i are 4 words. also my program need to wait users input again and again. So I think it needs a while loop to access it. I know little about the implementation to get input from users. However I confused to implement the word count and also the while loop at the same time. Actually I complete the check condition work and other functions. Can anyone help me?

Take a look at Console.ReadLine() and Console.ReadKey() . For input parameters there are two ways to access them.

  • One way is to use the args array that is a parameter of your Main function (first entry is always the name of your executeable).

  • Use Environment.GetCommandLineArgs() same as before, the first entry is always the name of your executable.

Something like this should do:

string currentInputToHandle = string.Join("", Environment.GetCommandLineArgs());   
do
{        
    HandleEnteredArgument(currentInputToHandle);        
    currentInputToHandle = Console.ReadLine();
}while(currentInputToHandle.ToLowerInvariant() != "exit".ToLowerInvariant())

edit

You should probably restructure your sanitation stuff. I guess the easiest would be to string.Split("-") the supplied arguments, that way you'd have the command and all of it's parameters in one string, after that check for the amount of arguments by doing a string.Split(" ") . That should enhance the readability of your code by a great margin.

edit2 oh, I totally forgot that one command could depend on the other command, so I removed the foreach part.

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