C++ - 错误 E2285:找不到“tolower(char *)”的匹配项在函数 parseInput(fstream &) 中
给出以下代码:
void parseInput(fstream &inputFile) {
const int LENGTH = 81;
char line[LENGTH];
while(!inputFile.fail()) {
inputFile.getline(line,LENGTH);
line = tolower(line);
cout << line << endl;
}
}
编译后我收到此错误:
错误 E2285:找不到匹配项 对于 'tolower(char *)' 中 函数parseInput(fstream &)
我知道它返回一个 int,但不是 int[],这是否意味着我应该获取输入字符到字符而不是使用 getline ?有没有办法将整行转换为更低?预先感谢大家的帮助!
Given the following code:
void parseInput(fstream &inputFile) {
const int LENGTH = 81;
char line[LENGTH];
while(!inputFile.fail()) {
inputFile.getline(line,LENGTH);
line = tolower(line);
cout << line << endl;
}
}
upon compiling I'm getting this error:
Error E2285 : Could not find a match
for 'tolower(char *)' in
function parseInput(fstream &)
I know it returns an int, but not an int[], does that mean instead of using getline i should get input character to character? is there a way to convert the whole line to lower? Thanks in advance for everyone's help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
Hi tolower 函数输入参数必须是 char 而不是 char*,但是如果使用 std,则可以使用 string 和 std:transform 使字符串小写
Hi tolower function input parametr must be char not char*, but if you use std you can use string and std:transform to make string lower case
独立的
tolower
函数只接受一个int
,并且int
需要严格非负或EOF
,否则行为未定义。存在另一个版本的tolower
,但它只是一个模板。这两个事实使得它们很难轻松、安全地与transform
一起使用。C++ 还在其
ctype
方面提供了tolower
,您可以在此处使用它,但是整个代码表明您对数组和点不熟悉,因此也许您应该开始使用 < code>std::string 和易于使用的算法?查看
boost::string_algo
的 大小写转换。The standalone
tolower
function only accepts oneint
, and theint
needs to be strictly nonnegative orEOF
, otherwise behavior is undefined. Another version oftolower
exists, which however is a template. Both of these facts make it difficult to use them withtransform
easily and safely.C++ also provides
tolower
in itsctype
facet, which you can use hereHowever the whole code shows you aren't familiar with arrays and points, so maybe you should start using
std::string
and easy to use algorithms? Look intoboost::string_algo
's case conversions.嗯,这里的三个答案设法
tolower
错误地使用。其参数必须为非负数或特殊的 EOF 值,否则为未定义行为。如果你的都是ASCII字符,那么这些代码都将是非负的,所以在这种特殊情况下,可以直接使用它。但是,如果存在任何非 ASCII 字符,例如挪威语“blåbærsyltetøy”(蓝莓果酱),那么这些代码很可能为负数,因此有必要将参数转换为无符号
char
类型。此外,对于这种情况,C 区域设置应设置为相关区域设置。
例如,您可以将其设置为用户的默认区域设置,该区域设置由空字符串作为
setlocale
的参数表示。示例:
使用
std::transform
执行此操作的示例:有关使用 C++ 级别语言环境而不是 C 语言环境的示例,请参阅 Johannes 的回答。
干杯&呵呵,
Hm, three answers here managed to use
tolower
incorrectly.Its argument must be non-negative or the special
EOF
value, otherwise Undefined Behavior. If all you have are ASCII characters then the codes will all be non-negative, so in that special case, it can be used directly. But if there's any non-ASCII character, like in Norwegian "blåbærsyltetøy" (blueberry jam), then those codes are most likely negative, so casting the argument to unsignedchar
type is necessary.Also, for this case, the C locale should be set to the relevant locale.
E.g., you can set it to the user's default locale, which is denoted by an empty string as argument to
setlocale
.Example:
Example of instead doing this using
std::transform
:For an example of using the C++ level locale stuff instead of C locale, see Johannes' answer.
Cheers & hth.,
您可以简单地执行以下循环将字符串逐个字符转换为小写:
编辑: 此外,以下代码甚至更简单(假设字符串以 null 结尾)。
You can simply perform the following loop to convert a string, character-by-character, to lower case:
Edit: furthermore the following code is even easier (assuming the string is null terminated).
是和否。
您可以获得整行,但使用 for 循环将其逐个字符打印(并使用 tolower)。
yes and no.
you can get a whole line but print it char after char (and using tolower) using a for loop.
tolower()
一次只能处理一个字符。你应该做类似的事情tolower()
only work on one character at a time. You should do something like