【问题标题】:C++ version of isspace (Convert code to C to C++)isspace 的 C++ 版本(将代码转换为 C 到 C++)
【发布时间】:2010-10-21 17:33:16
【问题描述】:

我正在将代码从 C 转换为 C++。我目前正在使用 C 函数 isspace,当使用 ifstream 时,C++ 等效项是什么?具体while (!isspace(lineBuffer[l]))

id 是第一个数字(2515、1676、279),name 是第一个“空格”(ABC、XYZ、FOO)之后的一组字母。

NewList.Txt 示例

2515    ABC 23.5    32  -99 1875.7  1  
1676    XYZ 12.5    31  -97 530.82  2  
279  FOO 45.5    31  -96  530.8  3  

C 代码

void converter::updateNewList(){
    FILE* NewList;
    char lineBuffer[100];
    char* id = 0;
    char* name = 0;

    int l = 0;
    int n;

    NewList = fopen("NewList.txt","r");
    if (NewList == NULL){
        std::cerr << "Error in reading NewList.txt\n";
        exit(EXIT_FAILURE);
    } 

    while(!feof(NewList)){
        fgets (lineBuffer , 100 , NewList); // Read line    
        l = 0;
        while (!isspace(lineBuffer[l])){
            l = l + 1;
        }

        id = new char[l];
        switch (l){
            case 1: 
                n = sprintf (id, "%c", lineBuffer[0]);
                break;
            case 2:
                n = sprintf (id, "%c%c", lineBuffer[0], lineBuffer[1]);
                break;
            case 3:
                n = sprintf (id, "%c%c%c", lineBuffer[0], lineBuffer[1], lineBuffer[2]);        
                break;
            case 4:
                n = sprintf (id, "%c%c%c%c", lineBuffer[0], lineBuffer[1], lineBuffer[2],lineBuffer[3]);
                break;
            default:
                n = -1;
                break;
        }
        if (n < 0){
            std::cerr << "Error in processing ids from NewList.txt\n";
            exit(EXIT_FAILURE);
        }

        l = l + 1;
        int s = l;
        while (!isspace(lineBuffer[l])){
            l = l + 1;
        }
        name = new char[l-s];
        switch (l-s){
            case 2:
                n = sprintf (name, "%c%c", lineBuffer[s+0], lineBuffer[s+1]);
                break;
            case 3:
                n = sprintf (name, "%c%c%c", lineBuffer[s+0], lineBuffer[s+1], lineBuffer[s+2]);
                break;
            case 4:
                n = sprintf (name, "%c%c%c%c", lineBuffer[s+0], lineBuffer[s+1], lineBuffer[s+2],lineBuffer[s+3]);
                break;
            default:
                n = -1;
                break;
        }
        if (n < 0){
            std::cerr << "Error in processing short name from NewList.txt\n";
            exit(EXIT_FAILURE);
        }


        ids_.push_back ( std::string(id) );
        names_.push_back(std::string(name));
    }

    bool isFound = false;
    for (unsigned int i = 0; i < siteNames_.size(); i ++) {
        isFound = false;
        for (unsigned int j = 0; j < names_.size(); j ++) {
            if (siteNames_[i].compare(names_[j]) == 0){
                isFound = true;
            }
        }
    }

    fclose(NewList);
    delete [] id;
    delete [] name;
}

C++ 代码

void converter::updateNewList(){
    int l = 0;
    char c;

    std::ifstream radesNewList ("NewList.txt",std::ios::in|std::ios::ate);
    NewList.seekg(std::ios_base::beg);

    std::string line;
    while(!NewList.eof()){
        l = 0;
        getline(radesNewList,line);

        for (size_t i=0; i<line.length(); ++i){
            c=line[i];
            bool isWhiteSpace = std::isspace(c, std::locale("C")); 
            if (isWhiteSpace){
                l = i+1;
                break;
            }       
        }

        ids_.push_back (line.substr(0,l));

        int s = l;
        for (size_t i=l; i<line.length(); ++i){
            c=line[i];
            bool isWhiteSpace = std::isspace(c, std::locale("C")); 
            if (isWhiteSpace){
                l = i+1;
                break;
            }       
        }
        names_.push_back(line.substr(s,l-s));
        line.clear();
    }

    NewList.close();
}

【问题讨论】:

  • 您应该提供一个正在转换的代码示例。

标签: c++ ifstream


【解决方案1】:

std::isspace() 来自#include &lt;locale&gt;。 (或者,更全称,template &lt;typename T&gt; bool std::isspace(T c, const std::locale&amp; loc)。确切的声明可能因编译器而异。)

【讨论】:

  • 你能举个例子吗?
  • 像使用 C 的 isspace() 一样使用它。 const std::locale&amp; 参数的适当值取决于您在做什么(例如,根据用户查找空格,或根据讲希腊语的马其顿人查找空格,或根据 ANSI 查找空格。)说出您的原因重新寻找空白,正确的std::locale 应该通过。
  • @Jonathan:不完全是您使用 C 的方式 isspace。对于 C 函数,如果要测试的值是 char,则需要将其强制转换为无符号类型。对于 C++ 模板,您只需传入 char,该值为负数是有效的。
  • 编译器之间的声明如何变化?
  • 所以它是机器可读/机器生成的输入?在这种情况下,您可能会期望输入格式正确——它不会有任何普通 C++ 程序员不知道的奇怪的玻利维亚空格。您可能需要“C”语言环境,它可以避免任何实际的本地化:bool isWhiteSpace = std::isspace(theCharacter, std::locale("C")); 这个示例当然可以进一步优化,但这样做是留给读者的练习。 ;) 请注意,“C”语言环境适用于空格,但可能不适用于字母、数字等。
【解决方案2】:

我不知道 C89 标准库中有任何内容不适用于 C++。 (C99 则不同,它是在当前 C++ 标准之后发布的 并且包含一些东西,例如 complex,它永远不会成为 C++ 标准。)

通常,只需包含 C 标头,用 c 前缀替换 .h 后缀(&lt;ctype.h&gt; 变为 &lt;cctype&gt;&lt;string.h&gt; 变为 &lt;cstring&gt;)并记住大多数标识符(排除宏)需要一个 std:: 前缀。

特别是,包括 &lt;cctype&gt;std::isspace(ch) 在 C++ 中的工作方式与在 C 中的工作方式一样。

【讨论】:

    猜你喜欢
    • 2012-05-14
    • 1970-01-01
    • 1970-01-01
    • 2010-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多