C++ 该程序没有';t在输入文件内部继续读取它
以下是我的代码部分:C++ 该程序没有';t在输入文件内部继续读取它,c++,fileinputstream,C++,Fileinputstream,以下是我的代码部分: ifstream inFile; inFile.open("Product1.wrl"); ... if (!inFile.is_open()){ cout << "Could not open file to read" << endl; return 0; } else while(!inFile.eof()){ getline(inFile, line); cout << l
ifstream inFile;
inFile.open("Product1.wrl");
...
if (!inFile.is_open()){
cout << "Could not open file to read" << endl;
return 0;
}
else
while(!inFile.eof()){
getline(inFile, line);
cout << line << endl; //this statement only to chech the info stored in "line" string
if (line.find("PointSet"))
inFile >> Point1;
}
ifstream-infle;
填充打开(“Product1.wrl”);
...
如果(!infle.is_open()){
不能改变
到
我不知道为什么人们经常被eof()
咬伤,但他们确实如此
将getline
与>
混合是有问题的,因为>
将在流中留下一个'\n'
,因此下一个getline
将返回空的。将其更改为使用getline
if(line.find(“PointSet”)
也不是您想要的。find
返回字符串中的位置,或者std::string::npos
如果找不到它
而且,你可以改变
ifstream inFile;
inFile.open("Product1.wrl");
到
以下是一个显示以下内容的版本:
class Point
{
public:
int i, j;
};
template <typename CharT>
std::basic_istream<CharT>& operator>>
(std::basic_istream<CharT>& is, Point& p)
{
is >> p.i >> p.j;
return is;
}
int main()
{
Point point1;
std::string line;
while(std::getline(std::cin, line))
{
std::cout << line << '\n'; //this statement only to chech the info stored in "line" string
if (line.find("PointSet") != std::string::npos)
{
std::string pointString;
if (std::getline(std::cin, pointString))
{
std::istringstream iss(pointString);
iss >> point1;
std::cout << "Got point " << point1.i << ", " << point1.j << '\n';
}
else
{
std::cout << "Uhoh, forget to provide a line with a PointSet!\n";
}
}
}
}
类点
{
公众:
int i,j;
};
模板
标准::基本流和操作员>>
(标准:基本信息流和is、点和点)
{
is>>p.i>>p.j;
回报是;
}
int main()
{
第1点;
std::字符串行;
while(std::getline(std::cin,line))
{
std::cout point1;
std::如果我让它while(getline(infle,line))
它只读取第一行并停止。是的,代码正在工作,但仍然无法解决问题。我假设问题可能在文件夹中,但我无法想象是什么导致它出现这种行为。@Alexandrubarbosie我的代码有什么问题?您仍然反复看到相同的字符串吗?好的y、 我发现问题出在行中。find(“PointSet”)
它以某种方式返回一个int值!=0,这甚至可能是垃圾。而且我的重载运算符似乎也有点错误(但不确定)无论如何,谢谢你的帮助。@Alexandrubarbosie如果你读了我的帖子,我已经解释过这是一个问题。std::string::find
返回找到的字符串在原始字符串中的位置,或者返回特殊值std::string::npos
,如果找不到的话。我想我们至少不能幸运地检查它从该文件中读取一个完整的数据点集?编写的代码读取一整行,而不是检查其中某个地方的“点集”一词,立即忽略所有其他内容并将其丢弃,然后依靠提取运算符读取我们只能希望正确实现的非类型神秘对象。以及使用.eof()
肯定是错的。好吧,让我们从头开始。为什么是.eof()
错了吗?无论是什么点
重新加载,也不管操作符是如何重载的,因为程序没有到达那一行,因为它卡在无限循环中。是的,它会忽略它,因为我不需要它,这是目的之一。文件本身>200行。有关为什么.eof()
作为循环条件检查点几乎总是错误的。如果没有您要求的其余信息,我只能提供这些信息。
ifstream inFile;
inFile.open("Product1.wrl");
ifstream inFile ("Product1.wrl");
class Point
{
public:
int i, j;
};
template <typename CharT>
std::basic_istream<CharT>& operator>>
(std::basic_istream<CharT>& is, Point& p)
{
is >> p.i >> p.j;
return is;
}
int main()
{
Point point1;
std::string line;
while(std::getline(std::cin, line))
{
std::cout << line << '\n'; //this statement only to chech the info stored in "line" string
if (line.find("PointSet") != std::string::npos)
{
std::string pointString;
if (std::getline(std::cin, pointString))
{
std::istringstream iss(pointString);
iss >> point1;
std::cout << "Got point " << point1.i << ", " << point1.j << '\n';
}
else
{
std::cout << "Uhoh, forget to provide a line with a PointSet!\n";
}
}
}
}