gpt4 book ai didi

Visual Studio Express 2012 中的 C++ 程序不能使用 .csv 但可以使用 .txt

转载 作者:行者123 更新时间:2023-11-28 07:34:29 25 4
gpt4 key购买 nike

为什么会发生这种情况?我以两个不同的名称保存了完全相同的文件。一个是 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;
}

关于Visual Studio Express 2012 中的 C++ 程序不能使用 .csv 但可以使用 .txt,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17014123/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com