C++ - 错误 E2285:找不到“tolower(char *)”的匹配项在函数 parseInput(fstream &) 中

发布于 2024-10-06 04:02:43 字数 533 浏览 0 评论 0原文

给出以下代码:

 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

淡淡離愁欲言轉身 2024-10-13 04:02:43

Hi tolower 函数输入参数必须是 char 而不是 char*,但是如果使用 std,则可以使用 string 和 std:transform 使字符串小写

std::string data = “MyStrData”; 
std::transform(data.begin(), data.end(), data.begin(), ::tolower);

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

std::string data = “MyStrData”; 
std::transform(data.begin(), data.end(), data.begin(), ::tolower);
人生戏 2024-10-13 04:02:43

独立的tolower函数只接受一个int,并且int需要严格非负或EOF,否则行为未定义。存在另一个版本的tolower,但它只是一个模板。这两个事实使得它们很难轻松、安全地与 transform 一起使用。

C++ 还在其 ctype 方面提供了 tolower ,您可以在此处使用它

std::ctype<char> const& c = std::use_facet< std::ctype<char> >(std::locale());
c.tolower(line, line + std::strlen(line));

,但是整个代码表明您对数组和点不熟悉,因此也许您应该开始使用 < code>std::string 和易于使用的算法?查看 boost::string_algo大小写转换

The standalone tolower function only accepts one int, and the int needs to be strictly nonnegative or EOF, otherwise behavior is undefined. Another version of tolower exists, which however is a template. Both of these facts make it difficult to use them with transform easily and safely.

C++ also provides tolower in its ctype facet, which you can use here

std::ctype<char> const& c = std::use_facet< std::ctype<char> >(std::locale());
c.tolower(line, line + std::strlen(line));

However 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 into boost::string_algo's case conversions.

半葬歌 2024-10-13 04:02:43

嗯,这里的三个答案设法tolower错误地使用。

其参数必须为非负数或特殊的 EOF 值,否则为未定义行为。如果你的都是ASCII字符,那么这些代码都将是非负的,所以在这种特殊情况下,可以直接使用它。但是,如果存在任何非 ASCII 字符,例如挪威语“blåbærsyltetøy”(蓝莓果酱),那么这些代码很可能为负数,因此有必要将参数转换为无符号 char 类型。

此外,对于这种情况,C 区域设置应设置为相关区域设置。

例如,您可以将其设置为用户的默认区域设置,该区域设置由空字符串作为 setlocale 的参数表示。

示例:

#include <iostream>
#include <string>           // std::string
#include <ctype.h>          // ::tolower
#include <locale.h>         // ::setlocale
#include <stddef.h>         // ::ptrdiff_t

typedef unsigned char   UChar;
typedef ptrdiff_t       Size;
typedef Size            Index;

char toLowerCase( char c )
{
    return char( ::tolower( UChar( c ) ) );     // Cast to unsigned important.
}

std::string toLowerCase( std::string const& s )
{
    using namespace std;
    Size const      n   = s.length();
    std::string     result( n, '\0' );

    for( Index i = 0;  i < n;  ++i )
    {
        result[i] = toLowerCase( s[i] );
    }
    return result;
}

int main()
{
    using namespace std;
    setlocale( LC_ALL, "" );                    // Setting locale important.
    cout << toLowerCase( "SARAH CONNER LIKES BLÅBÆRSYLTETØY" ) << endl;
}

使用 std::transform 执行此操作的示例:

#include <iostream>
#include <algorithm>        // std::transform
#include <functional>       // std::ptr_fun
#include <string>           // std::string
#include <ctype.h>          // ::tolower
#include <locale.h>         // ::setlocale
#include <stddef.h>         // ::ptrdiff_t

typedef unsigned char   UChar;

char toLowerCase( char c )
{
    return char( ::tolower( UChar( c ) ) );     // Cast to unsigned important.
}

std::string toLowerCase( std::string const& s )
{
    using namespace std;
    string          result( s.length(), '\0' );

    transform( s.begin(), s.end(), result.begin(), ptr_fun<char>( toLowerCase ) );
    return result;
}

int main()
{
    using namespace std;
    setlocale( LC_ALL, "" );                    // Setting locale important.
    cout << toLowerCase( "SARAH CONNER LIKES BLÅBÆRSYLTETØY" ) << endl;
}

有关使用 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 unsigned char 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:

#include <iostream>
#include <string>           // std::string
#include <ctype.h>          // ::tolower
#include <locale.h>         // ::setlocale
#include <stddef.h>         // ::ptrdiff_t

typedef unsigned char   UChar;
typedef ptrdiff_t       Size;
typedef Size            Index;

char toLowerCase( char c )
{
    return char( ::tolower( UChar( c ) ) );     // Cast to unsigned important.
}

std::string toLowerCase( std::string const& s )
{
    using namespace std;
    Size const      n   = s.length();
    std::string     result( n, '\0' );

    for( Index i = 0;  i < n;  ++i )
    {
        result[i] = toLowerCase( s[i] );
    }
    return result;
}

int main()
{
    using namespace std;
    setlocale( LC_ALL, "" );                    // Setting locale important.
    cout << toLowerCase( "SARAH CONNER LIKES BLÅBÆRSYLTETØY" ) << endl;
}

Example of instead doing this using std::transform:

#include <iostream>
#include <algorithm>        // std::transform
#include <functional>       // std::ptr_fun
#include <string>           // std::string
#include <ctype.h>          // ::tolower
#include <locale.h>         // ::setlocale
#include <stddef.h>         // ::ptrdiff_t

typedef unsigned char   UChar;

char toLowerCase( char c )
{
    return char( ::tolower( UChar( c ) ) );     // Cast to unsigned important.
}

std::string toLowerCase( std::string const& s )
{
    using namespace std;
    string          result( s.length(), '\0' );

    transform( s.begin(), s.end(), result.begin(), ptr_fun<char>( toLowerCase ) );
    return result;
}

int main()
{
    using namespace std;
    setlocale( LC_ALL, "" );                    // Setting locale important.
    cout << toLowerCase( "SARAH CONNER LIKES BLÅBÆRSYLTETØY" ) << endl;
}

For an example of using the C++ level locale stuff instead of C locale, see Johannes' answer.

Cheers & hth.,

邮友 2024-10-13 04:02:43

您可以简单地执行以下循环将字符串逐个字符转换为小写:

const int lineLen = strlen( line );
for ( int i = 0; i < lineLen; i++ )
{
     line[i] = static_cast< char >( ::tolower( static_cast< unsigned char >( line[i] ) ) );
}

编辑: 此外,以下代码甚至更简单(假设字符串以 null 结尾)。

_strlwr( line );

You can simply perform the following loop to convert a string, character-by-character, to lower case:

const int lineLen = strlen( line );
for ( int i = 0; i < lineLen; i++ )
{
     line[i] = static_cast< char >( ::tolower( static_cast< unsigned char >( line[i] ) ) );
}

Edit: furthermore the following code is even easier (assuming the string is null terminated).

_strlwr( line );
盛夏尉蓝 2024-10-13 04:02:43

是和否。
您可以获得整行,但使用 for 循环将其逐个字符打印(并使用 tolower)。

yes and no.
you can get a whole line but print it char after char (and using tolower) using a for loop.

阳光①夏 2024-10-13 04:02:43

tolower() 一次只能处理一个字符。你应该做类似的事情

for(int i = 0; i < LENGTH; ++i)
  line[i] = tolower(line[i]);

tolower() only work on one character at a time. You should do something like

for(int i = 0; i < LENGTH; ++i)
  line[i] = tolower(line[i]);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文