C++ C++;-错误E2285:找不到与';托洛尔(char*)和#x27;函数内解析输入(fstream&;)

C++ C++;-错误E2285:找不到与';托洛尔(char*)和#x27;函数内解析输入(fstream&;),c++,compiler-errors,tolower,C++,Compiler Errors,Tolower,给定以下代码: void parseInput(fstream &inputFile) { const int LENGTH = 81; char line[LENGTH]; while(!inputFile.fail()) { inputFile.getline(line,LENGTH); line = tolower(line); cout << l

给定以下代码:

 void parseInput(fstream &inputFile) {
        const int LENGTH = 81;
        char line[LENGTH];

        while(!inputFile.fail()) {
            inputFile.getline(line,LENGTH);
            line = tolower(line);
            cout << line << endl;
        }
    }
void parseInput(fstream&inputFile){
常数int长度=81;
字符行[长度];
而(!inputFile.fail()){
getline(行,长度);
线路=较低(线路);

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

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

您只需执行以下循环即可将字符串逐个字符转换为小写:

const int lineLen = strlen( line );
for ( int i = 0; i < lineLen; i++ )
{
     line[i] = static_cast< char >( ::tolower( static_cast< unsigned char >( line[i] ) ) );
}
tolower()
一次只能处理一个字符

for(int i = 0; i < LENGTH; ++i)
  line[i] = tolower(line[i]);
for(int i=0;i
是和否。
你可以得到一整行,但是一个字符接一个字符地打印出来(使用tolower)使用for循环。

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

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

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

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

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

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

此外,对于这种情况,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;
}

使用C++级别的区域设置代替C语言环境的例子,请参阅Johannes的回答< /P>


Cheers&hth.,

这也适用于
char*
std::transform(line,line+strlen(line),::tolower);
@blakejc70没问题,只要问一下:)注意,(a)
tolower
在命名空间
std
中,应该是限定的,(b)还有一个locale
std::tolower
函数,所以
(int(*)(int))std::tolower
可能需要用于消除函数的歧义。通过传递
“\x80”
,可以轻松利用此不安全代码。在许多系统上,ctype函数使用查找表,
-128
将读取该查找表的内存。例如,在我的系统上,
islower(-22222)
因错误使用
tolower而与SIGSEGV.-1崩溃(其参数必须为非负或EOF)。谢谢,这正是我所担心的,不容易解决的大问题,非常感谢!-1因错误使用tolower(其参数必须为非负或EOF).是的,我想我必须遍历字符串才能这样做,我感谢您的时间和帮助。-1对于错误使用
tolower
(其参数必须为非负或EOF).将行为未定义的程序更改为格式错误的程序b@Goz哦,等等,我发布了那个评论?我想我按下了取消按钮:)不管怎样,那应该是“行为”,但我删除了它,因为“不良行为”听起来像胡说八道,但我确实让“b”独自一人。我肯定没有睡过觉或15个小时。你真的可以看到第一个效果!@Goz:我只是有点缺乏勇气,我不想听起来像是在抨击。所以我只是指出了主要的技术错误。毕竟,速度是次要的问题(正确性第一!),并且通常支持非ASCII字符(使用
setlocale
)第三个问题,或者根本不关心,取决于上下文;例如,我认为代码原样适用于Windows ANSI Western,带有非国家但非ASCII字符,如欧元符号“€”。干杯,阅读错误消息,思考被投诉的代码。返回值显然不是问题,因为
tolower
返回一个int,您分配给一个int-谁说了关于
int*
?参数是问题:它说它找不到
tolower(char*)
,因为这就是您试图调用的-即,您正在传递一个
char*
(实际上是一个衰减的
char[]
),并且该版本的
tolower
不存在-正如其他人所指出的,您希望传递
char
。我们使用循环或
std::transform
为数组中的每个
char
重复该过程。+1这个答案很好,因为它显然是正确的。也请参考Boost.:-)但我认为你应该讨论你第一句话的后果。我也觉得代码令人憎恶。不是你的错,我觉得这里的标准库设计令人憎恶,导致无法阅读的GobbleDeBook,这需要时间来创建和解密…所以,发布我自己更简单的答案。干杯,
#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;
}
#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;
}