Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个结构
int Hour int Min int Sec
我正在从文件中读取字符串。文件的文本如下:23:21:30 12:32:54
我将整行拆分为单独的时间,然后我将第二次拆分为结构。我正在做
structure[x].Hour = atoi(token);
然后我需要检查structure[x].Hour是否不是 alpha。
if(isalpha(structure[x].Hour)){//DO Something}
不管用。有任何想法吗?
您并没有真正提出问题或告诉我们您有什么问题,但无论如何我都会帮助您...
你正在做的事情是错误的顺序。如果您atoi使用非数字字符串调用,它将返回0. 然后检查整数0是否不是字母,当然不是。您需要以相反的顺序执行此操作:首先检查您是否没有非数字字符串(您必须在循环中执行此操作);然后将其转换为整数。
atoi
0
这实际上可以在单个标准库函数调用中完成,但不是aoti没有那种支持,而是使用strtol函数。
aoti
strtol
该strtol函数具有内置的错误检查功能,并支持帮助您确定整个字符串是否已转换。