C++ 如何将数据文件作为参数传递到c++;?
使用此代码,我可以读取数据文件并将其放入数组中。但现在我想在一个函数中转换这段代码,该函数将文件作为参数。有人知道我怎么做吗C++ 如何将数据文件作为参数传递到c++;?,c++,C++,使用此代码,我可以读取数据文件并将其放入数组中。但现在我想在一个函数中转换这段代码,该函数将文件作为参数。有人知道我怎么做吗 int main(int argc, char const *argv[]){ if (argc < 2) { cerr << "input the name of file\n"<< endl; } string program_name = argv[0]; ifstream input(argv[1]); ve
int main(int argc, char const *argv[]){
if (argc < 2)
{
cerr << "input the name of file\n"<< endl;
}
string program_name = argv[0];
ifstream input(argv[1]);
vector<vector<double> > v;
if (input)
{
string line;
int i = 0;
while (getline(input, line))
{
if (line[0] != '#')
{
v.push_back(vector<double>());
stringstream split(line);
double value;
while (split >> value)
{
v.back().push_back(value);
}
}
}
}
for (int i = 0; i < v.size(); i++)
{
for (int j = 0; j < v[i].size(); j++)
cout << v[i][j] << '\t';
cout << endl;
}
int main(int argc,char const*argv[]){
如果(argc<2)
{
cerr值)
{
v、 后退()。向后推(值);
}
}
}
}
对于(int i=0;i
void my_function(const std::string& filename,
vector< vector<double> >& v)
{
//...
}
void my_函数(const std::string和filename,
矢量&v)
{
//...
}
根据您的问题,函数以字符串形式接收文件名,并传递向量
另一种方法是将文件作为流传递:
void somebody_function(std::istream& input_file,
vector< vector< double > >& v)
{
//...
}
void someone_函数(std::istream&input_文件,
向量>&v)
{
//...
}
像这样的东西
void my_function(const std::string& filename,
vector< vector<double> >& v)
{
//...
}
void my_函数(const std::string和filename,
矢量&v)
{
//...
}
根据您的问题,函数以字符串形式接收文件名,并传递向量
另一种方法是将文件作为流传递:
void somebody_function(std::istream& input_file,
vector< vector< double > >& v)
{
//...
}
void someone_函数(std::istream&input_文件,
向量>&v)
{
//...
}
@Fredifqh:如果我的答案有帮助,请点击复选标记。顺便说一句,我不是Louen。嗨,Thomas,我修改了代码。但代码不起作用。这是我第一次使用此链接。我想给你看新代码,但我不知道应该放在哪里。如果问题与你发布的问题不同,请发布新问题。在你之前发布新代码时,请使用调试器找出代码执行不正确的地方。@Fredifqh:如果我的答案有帮助,请单击复选标记。顺便说一句,我不是Louen。嗨,Thomas,我修改了代码。但是代码不起作用。这是我第一次使用此链接。我想给你看新代码,但我不知道应该放在哪里。如果问题与您发布的问题不同,请发布新问题。发布新代码之前,请使用调试器找出代码未正确执行的位置。