isspace的C ++版本(将代码转换为C到C ++)

时间:2010-10-21 17:33:17

标签: c++ ifstream

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

id是第一个数字(2515,1676,279),名称是第一个“空格”(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 ++ CODE

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();
}

2 个答案:

答案 0 :(得分:9)

来自std::isspace()

#include <locale>。 (或者,更完全限定,template <typename T> bool std::isspace(T c, const std::locale& loc)。确切的声明可能因编译器而异。)

答案 1 :(得分:3)

我不知道C89标准库中没有任何可用于C ++的内容。 (对于C99来说,它是不同的,它是在当前的C ++标准之后发布的并且包含一些类似complex的东西 - 它们永远不会进入C ++标准。)

通常,只需添加C标头,将.h后缀替换为c前缀(<ctype.h>变为<cctype><string.h>变为<cstring>并且记住大多数标识符(排除宏)需要std::前缀。

特别是,包括<cctype>std::isspace(ch)在C ++中有效,因为它在C中有效。