繁体   English   中英

Visual Studio Express 2012中的C ++程序不适用于.csv,但适用于.txt

[英]C++ Program in Visual Studio Express 2012 not working with .csv but does work with .txt

有什么理由会发生这种情况吗? 我有使用两个不同名称保存的相同文件。 一个是test.csv,另一个是text.txt。 内容是相同的。 当我使用ifstream对象打开text.txt时,我的代码按预期运行,并将数据解析为一个对象。 当我打开test.csv时,ifstream对象从未收集任何数据,并且代码出现故障。 打开.csv(而不是.txt)时,是否需要采取任何额外的步骤?

这是实际执行输入的代码:

    Team::Team(ifstream& fin)
    {
string temp;
stringstream convert;

//Get team #
getline(fin, temp, ',');
idNumber = stringToInt(temp);

//Get team letter
getline(fin, temp, ',');
idLetter = temp[0];

//Get team name
getline(fin, name, ',');

//Get team type
getline(fin, type, ',');

//Get team rating
getline(fin, temp, ',');
rating = stringToDouble(temp);

//Get team notes
getline(fin, notes, ',');

//Get toss info
getline(fin, temp, ',');
canToss = stringToBool(temp);
getline(fin, tossType, ',');

//Get female info
getline(fin, temp, ',');
hasFemales = stringToBool(temp);
getline(fin, temp, ',');
femaleNumber = stringToInt(temp);
getline(fin, temp, ',');
femaleRating = stringToDouble(temp);

//Get Auto info
getline(fin, temp, ',');
hasAuto = stringToBool(temp);
getline(fin, autoType, ',');
getline(fin, temp, ',');
autoScore = stringToInt(temp);

//Get Drive Info
getline(fin, temp, ',');
driveMotors = stringToInt(temp);
getline(fin, temp, ',');
driveRatio = stringToDouble(temp);
getline(fin, driveType, ',');

//Get hang info
getline(fin, temp, ',');
canHang = stringToBool(temp);
getline(fin, hangType, ',');

//Get stash info
getline(fin, temp, ',');
canStash = stringToBool(temp);

//Get lift indo
getline(fin, temp, ',');
liftMotors = stringToInt(temp);
getline(fin, temp, ',');
liftRatio = stringToDouble(temp);
getline(fin, liftType, ',');

//Get competition info
getline(fin, temp, ',');
driverSkills = stringToInt(temp);
getline(fin, temp, ',');
programmingSkills = stringToInt(temp);
getline(fin, temp, ',');
ranking = stringToInt(temp);
getline(fin, temp, ',');
wins = stringToInt(temp);
getline(fin, temp, ',');
ties = stringToInt(temp);
getline(fin, temp, ',');
losses = stringToInt(temp);
getline(fin, temp);
SPs = stringToInt(temp);
    }

如果所有读取操作均未提取任何内容,则很可能是您没有成功打开文件,尽管可能有很多原因,但第一步是要检测到它。 您是否测试文件成功打开? 您可以执行以下操作:

if (ifstream fin("test.csv")) {
    Team team(fin);
    ⋮
} else {
    std::cerr << "Failed to open test.csv\n";
}

在此期间,我是否可以建议代码重组? 大约80%的代码是维护代码,因此可视化处理的清晰度和速度应该是您的编码样式中的关键考虑因素:

getline(fin, temp     , ','); idNumber          = stringToInt   (temp);
getline(fin, temp     , ','); idLetter          = temp[0];
getline(fin, name     , ',');
getline(fin, type     , ',');
getline(fin, temp     , ','); rating            = stringToDouble(temp);
getline(fin, notes    , ',');

getline(fin, temp     , ','); canToss           = stringToBool  (temp);
getline(fin, tossType , ',');

getline(fin, temp     , ','); hasFemales        = stringToBool  (temp);
getline(fin, temp     , ','); femaleNumber      = stringToInt   (temp);
getline(fin, temp     , ','); femaleRating      = stringToDouble(temp);

getline(fin, temp     , ','); hasAuto           = stringToBool  (temp);
getline(fin, autoType , ',');
getline(fin, temp     , ','); autoScore         = stringToInt   (temp);

//Get Drive Info
getline(fin, temp     , ','); driveMotors       = stringToInt   (temp);
getline(fin, temp     , ','); driveRatio        = stringToDouble(temp);
getline(fin, driveType, ',');

//Get hang info
getline(fin, temp     , ','); canHang           = stringToBool  (temp);
getline(fin, hangType , ',');

//Get stash info
getline(fin, temp     , ','); canStash          = stringToBool  (temp);

//Get lift indo
getline(fin, temp     , ','); liftMotors        = stringToInt   (temp);
getline(fin, temp     , ','); liftRatio         = stringToDouble(temp);
getline(fin, liftType , ',');

//Get competition info
getline(fin, temp     , ','); driverSkills      = stringToInt   (temp);
getline(fin, temp     , ','); programmingSkills = stringToInt   (temp);
getline(fin, temp     , ','); ranking           = stringToInt   (temp);
getline(fin, temp     , ','); wins              = stringToInt   (temp);
getline(fin, temp     , ','); ties              = stringToInt   (temp);
getline(fin, temp     , ','); losses            = stringToInt   (temp);

作为一项额外的改进,您可以进一步重构代码,以避免太多的读取操作:

Team::Team(ifstream& fin)
{
    string temp;
    getline(fin, temp);
    const char * ctemp = temp.c_str();
    std::vector<char> buf(ctemp, ctemp + temp.count() + 1);

    char * lasts, * s = buf.data();
    auto next = [&]{
        char * result = strtok_r(s, ",", &lasts);
        assert(result); // …or throw
        s = NULL;
        return result;
    };

    idNumber          = stringToInt   (next());
    idLetter          =                next()[0];
    name              =                next();
    type              =                next();
    rating            = stringToDouble(next());
    ⋮
    programmingSkills = stringToInt   (next());
    ⋮
}

最后,您甚至可能想要使用一个方便的提取器类:

class CsvLine {
public:
    CsvLine(const std::string & line) {
        const char * temp = line.c_str();
        buf_.assign(temp, temp + line.length() + 1));
        s_ = buf_.data();
    }

    CsvLine & operator>>(string & x) { x =                next() ; return *this; }
    CsvLine & operator>>(char   & x) { x =               *next() ; return *this; }
    CsvLine & operator>>(int    & x) { x = stringToInt   (next()); return *this; }
    CsvLine & operator>>(bool   & x) { x = stringToBool  (next()); return *this; }
    CsvLine & operator>>(double & x) { x = stringToDouble(next()); return *this; }

private:
    std::vector<char> buf_;
    char * s_, * lasts_;

    char * next() {
        char * result = strtok_r(s_, ",", &lasts_);
        assert(result); // …or throw
        s_ = NULL;
        return result;
    };
};

Team::Team(ifstream& fin)
{
    string line;
    getline(fin, line);
    CsvLine(line)
        >> idNumber >> idLetter >> name >> type >> rating >> notes
        >> canToss >> tossType
        ⋮
        >> driverSkills >> programmingSkills >> ranking >> wins >> ties >> losses;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM