C++读txt文件(由CSV文件转换成的txt)
txt中的数据以,分隔
txt示例:
代码如下:
int main()
{
//txth_num:txt的行数
//9:txt的列数
vector <vector <double> >arr(txth_num, vector<double>(9));
string filepath = "D:/.../***.txt";
readTxt0(filepath, arr);
...
}
void readTxt(string file, vector<vector<double>>& arr)
{
ifstream ifs(file, ios::in);
if (!ifs.is_open())
{
cout << "文件打开失败" << endl;
return;
}
for (int i = 0; i < txth_num; i++)
{
for (int j = 0; j < txtl_num; j++)
{
ifs >> arr[i][j];
ifs.get();
}
}
ifs.close();
}