希望从命令行解析文件名并检查其正确性,例如(1)总长度,(2)预期扩展名,(3)'_'位置和其他输入值。
顺序如下:
$check.exe input_file L2A30000_0102051303042026_0001.dat
字符串
它应该检查输出文件(L2A30000_0102051303042026_0001.dat)是否按应有的方式键入(不是按精确值,而是按类型和长度)。
// Function to check if a string consists of digits
int isNumeric(const char *str) {
while (*str) {
if (!isdigit(*str)) {
return 0; // Not a digit
}
str++;
}
return 1; // All characters are digits
}
int main(int argc, char *argv[]) {
// Check if the correct number of command line arguments is
provided
if (argc != 3) {
printf("Usage: %s inputfile outputfile\n", argv[0]);
return 1;
}
// Extract the output file name from the command line arguments
const char *outputFileName = argv[2];
// Define the expected format
char asciiChar1, numChar1, asciiChar2, numChar2, numChar3[5],
underscore1, numChar4[17], underscore2, numChar5[5],
numChar6[4], extension[4];
int result = sscanf(outputFileName,
"%c%c%c%c%4[0-9]%c%16[0-9]%c%1[0-9]%3[0-9]_%3[0-9]%4[.dat]",
&asciiChar1, &numChar1, &asciiChar2,
&numChar2, numChar3, &underscore1, numChar4, &underscore2,
numChar5, numChar6, extension);
// Debugging print statement
printf("Debug: sscanf result: %d\n", result);
printf("Debug: asciiChar1: %c\n", asciiChar1);
printf("Debug: numChar1: %c\n", numChar1);
printf("Debug: asciiChar2: %c\n", asciiChar2);
printf("Debug: numChar2: %c\n", numChar2);
printf("Debug: numChar3: %s\n", numChar3);
printf("Debug: underscore1: %c\n", underscore1);
printf("Debug: numChar4: %s\n", numChar4);
printf("Debug: underscore2: %c\n", underscore2);
printf("Debug: numChar5: %s\n", numChar5);
printf("Debug: numChar6: %s\n", numChar6);
printf("Debug: extension: %s\n", extension);
// Check if the extracted values match the expected format
if (result != 12 || !isalpha(asciiChar1) || !isdigit(numChar1) ||
!isalpha(asciiChar2) || !isdigit(numChar2) ||
strlen(numChar3) != 4 || !isNumeric(numChar3) ||
strlen(numChar4) != 16 || !isNumeric(numChar4) ||
strlen(numChar5) != 4 || !isNumeric(numChar5) ||
strlen(numChar6) != 3 || !isNumeric(numChar6) ||
strlen(extension) != 3 || strcmp(extension, ".dat") != 0) {
printf("Error: Output file format is incorrect.\n");
return 1;
}
// If all checks pass, the output file format is correct
printf("Output file format is correct.\n");
return 0;
}
型
命令行输入:
.\check.exe inputfile L2A30000_0102051303042026_0001.dat
型
这是我得到的输出:
Debug: sscanf result: 9
...
Debug: numChar5: 0001
Debug: extension:
Error: Output file format is incorrect.
型
这是我期待的输出:
Debug: extension:.dat
型
这部分不工作。其他部分正常。想检查extension
是否是.dat
文件名。如果不是,它将打印错误消息并退出。
2条答案
按热度按时间sq1bmfud1#
我建议您在格式字符串和匹配参数中引入一些额外的白色空间,并沿着沿着这些行:
字符串
所以直到第二个下划线都是正确的。然后你需要一个数字(
char numChar5[5]
)但这与变量的大小不匹配。(char numChat6[4]
)这是好的。然后是第三个下划线,这是不是在输入中。3个没有匹配参数的数字。“%4[.dat]”这会导致缓冲区溢出,因为扩展变量是char extension[4]
。总共12个格式指令和11个参数,这是未定义的行为。您可以通过硬编码固定字符串来简化它:
型
示例运行:
型
另一种方法是通过一个小的解释器
is_valid_format2()
解析文件名is_valid_format()
:型
3lxsmp7m2#
考虑一个简化:
"%[]"
限制有效输入。保存到char
数组中。"%n"
以保存扫描偏移并确定字符串结束是否成功。asciiChar1
的名称更改为alphaChar1
,因为代码在此处查找A-Z和a-z。请注意,扫描长度正确的唯一方法是所有
[]
字符串成功扫描到其最大宽度,并且下一个要扫描的字符是 *null字符 *。这大大简化了测试。
字符串
输出
型