这是一个统计数据出现次数的函数,我们每次读取一行的7(#define NUM 7)个数据进行处理,进行多次循环直到读完所有数据……
使用文件流 ifstream 定义一个对象对数据进行读取……在结束使用该文件流时,关闭文件流 file.close();……
把这个函数写在这里了,方便日后用到时查找……
bool ttast(const char* fileName, int* ball, int ballCount) {
ifstream file;//文件流
int result[NUM];
int i = 0;
if (!fileName) {
char cc[64];
//cout << "输入文件名为空!" << strerror_s(cc, 64, errno) << endl;
//exit(1);
return false;
}
file.open(fileName);
if (file.fail()) {
char cc[64];
cout << "文件打开失败!" << strerror_s(cc, 64, errno) << endl;
//exit(1);
return false;
}
do {
i = 0;
for (i = 0; i < NUM; i++) {
file >> result[i];
if (file.eof()) {
break;
}
if (file.fail()) {
char cc[64];
//cout << "文件打开失败!" << endl;
cout << "读取文件失败,原因:" << strerror_s(cc, 64, errno) << endl;
break;
}
}
if (i == 0) {//记录正常结束
break;
}
if (i < NUM - 1) {
cout << "记录读到" << i << "个,预期读到7个!" << endl;
break;
}
for (i = 0; i < NUM; i++) {
cout << " " << result[i];
}
cout << endl;
for (i = 0; i < NUM; i++) {
int index = *(result + i) - 1;
if (index >= 0 && index < ballCount) {
*(ball + index) += 1;
}
}
} while (1);
//关闭文件
file.close();
return true;
}