【发布时间】:2020-08-01 02:45:07
【问题描述】:
我正在尝试开展一个夏季模拟项目,但我似乎无法启动文件 I/O。这是我的代码:
std::string line;
std::ifstream F;
int M = 0; // Model Parameter //
if (F.open (fname)) // Error 1
std::cout << "Parsing File...";
else {
std::cout << "***Parsing - ERROR!***" << std::endl;
std::cout <<"Invalid filename passed to Parsing::ReadData" << std::endl;
std::cout << "fname = " << fname << " : Can't open file!" << std::endl;
throw 2;
}
std::getline (F, line);
std::vector<std::string> *result;
boost::split(result, line, boost::is_any_of(":")); // Errors 2 and 3
// Read Number of Metabolites
M = std::atoi(result[1]); // Error 4
std::cout << "M = " << M << std::endl;
F.close();
到目前为止,从我在网上阅读的内容来看,这对我来说似乎很简单。让我知道我是否应该提供任何澄清。我在编译和执行时遇到了所有这些错误:
In file included from test_parsing.cpp:11:
./Parsing_Engine.h:72:6: error: value of type 'void' is not contextually
convertible to 'bool'
if (F.open (fname))
^~~~~~~~~~~~~~
./Parsing_Engine.h:85:2: error: use of undeclared identifier 'boost'
boost::split(result, line, boost::is_any_of(":"));
^
./Parsing_Engine.h:85:29: error: use of undeclared identifier 'boost'
boost::split(result, line, boost::is_any_of(":"));
^
./Parsing_Engine.h:88:22: error: implicit instantiation of undefined template
'std::__1::vector<std::__1::basic_string<char>,
std::__1::allocator<std::__1::basic_string<char> > >'
M = std::atoi(result[1]);
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/iosfwd:216:28: note:
template is declared here
class _LIBCPP_TEMPLATE_VIS vector;
对于 F.open 错误,我不明白为什么 void 没有被解释为错误。对于 Boost 错误,我不明白为什么会发生这种情况,因为我使用 brew 安装了库。我不明白结果的隐式实例化是什么意思,尽管我认为如果我能得到提升,这个错误就会消失。
关于我做错了什么的任何提示?另外,抱歉 - 我不确定这是否是用于 stackoverflow 帖子的 TMI。
【问题讨论】:
-
std::ifstream::open没有返回任何内容,你确定你不是指std::ifstream::is_open?如果文件已打开并与流对象相关联,则后者返回true。您还可以通过std::ios::good检查流的错误标志。 -
我想这绝对是我的意思。我假设 C++ 的 .open() 会像 C 的 fopen 一样工作,它会返回一个 FILE 指针或 NULL。