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对象没有收集任何数据,代码出现了故障。与。txt文件相比,打开。csv文件是否需要采取额外的步骤?
下面是实际执行输入的代码: 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.csvn";
}
既然我在这里,我可以建议代码重组吗?大约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;
}
相关文章:
- 如何将 txt 文件中的行分隔为组件C++
- 正在将csv文件读取为双精度矢量
- 在CMakeLists.txt的安装功能中使用.cmake文件有什么用
- 一种在C++中读取TXT配置文件的简单方法
- 如何修复此iFile以将.txt输出到控制台
- 无法打开.txt文件
- CMakeLists.txt中的命名空间表示法
- Arduino从Txt读取整数
- 如何使用CLion在Mac上创建一个新的.txt文件
- 如何拆分文件中.txt字母并使用c ++使用数组进行扑克?
- 使用fstream对txt文件中的数字进行循环
- Visual Studio在尝试读取resource.txt文件时崩溃
- 更正 CMakeList.txt 用于谷歌测试的文件?
- 从Excel复制后,将.txt或.csv导入C++时丢失十进制值
- 尝试将 txt 文件转换为 csv
- QTableView 输出另存为 .csv 或 .txt
- TXT或CSV到C++映射
- C++ 操作方法:从.txt或.csv文件导入数值变量
- Visual Studio Express 2012中的c++程序不能使用.csv,但可以使用.txt
- 读取txt文件的CSV值