[英]Validating input and Word Count in C#
I have to do an C program which need to run in UBuntu system. 我必须做一个需要在UBuntu系统中运行的C程序。 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. ./recover是一个单词而./recover -d fat32.disk -i是4个单词。 also my program need to wait users input again and again. 我的程序也需要一次又一次地等待用户输入。 So I think it needs a while loop to access it. 所以我认为它需要一个while循环才能访问它。 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. 但是我很困惑,同时实现单词count和while循环。 Actually I complete the check condition work and other functions. 其实我完成了检查条件工作和其他功能。 Can anyone help me? 谁能帮我?
Take a look at Console.ReadLine()
and Console.ReadKey()
. 看看Console.ReadLine()
和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). 一种方法是使用作为Main函数参数的args数组(第一个条目始终是可执行文件的名称)。
Use Environment.GetCommandLineArgs()
same as before, the first entry is always the name of your executable. 使用与以前相同的Environment.GetCommandLineArgs()
,第一个条目始终是可执行文件的名称。
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(" ")
. 我想最简单的是string.Split("-")
提供的参数,这样你就可以在一个字符串中获得命令及其所有参数,之后通过执行string检查参数的数量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. edit2哦,我完全忘了一个命令可能依赖于另一个命令,所以我删除了foreach部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.