请教个getline读取文件的问题

czbever 2011-02-24 06:15:13
我用getline读取dat文件的时候, 为什么一直退不出循环? 我代码是这样的:
BOOL GetIPS(std::ifstream& in, TiXmlDocument* xml_myDocument, TiXmlElement* xml_RootElement, std::tr1::unordered_set<std::string> & set)
{
std::string line = "";

while (std::getline(in, line) && !in.eof())
{
//std::cout << line << std::endl;

size_t pos = line.find("=");
if (pos == std::string::npos)
continue;

std::string sub_ips = line.substr(pos + 1, line.length() - pos - 1);
size_t begin_Pos = 0;
size_t found_Pos = 0;
size_t length = sub_ips.length();
while (found_Pos != std::string::npos)
{
found_Pos = sub_ips.find(";", begin_Pos, 1);
std::string ip = sub_ips.substr(begin_Pos, found_Pos - begin_Pos);

if(ip.compare("127.0.0.1") == 0)
continue;

std::cout << ip << std::endl;
set.insert(ip);
begin_Pos = found_Pos + 1;
found_Pos = sub_ips.find(";", begin_Pos, 1);

}


}
return TRUE;
}
...全文
288 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
czbever 2011-02-26
  • 打赏
  • 举报
回复
恩 是的 我自己也检查出来了 是continue那句的问题,谢谢!
雪人2015 2011-02-26
  • 打赏
  • 举报
回复
继续加油!go!
赵4老师 2011-02-25
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 rwjlqn 的回复:]
最好别用getline函数 它的实现最后把回车换行都给弄丢了 移植性不好! 可以考虑fgets
[/Quote]
英雄所见略同!
rwjlqn 2011-02-25
  • 打赏
  • 举报
回复
最好别用getline函数 它的实现最后把回车换行都给弄丢了 移植性不好! 可以考虑fgets
雪人2015 2011-02-24
  • 打赏
  • 举报
回复
我晕,知道一个逻辑问题。刚才测试的时候用a.txt内容只有一行,没问题,原来一行的时候,第一次读取就到了文件末尾,所以直接没进for循环。

现在我改用多行出问题了:
a=127.0.0.1;192.168.1.1;192.168.1.10;
b=127.0.0.1;192.168.1.1;192.168.1.10;
c=127.0.0.1;192.168.1.1;192.168.1.10;
d=127.0.0.1;192.168.1.1;192.168.1.10;

原来你的

while (found_Pos != std::string::npos)
{
found_Pos = sub_ips.find(";", begin_Pos, 1);
std::string ip = sub_ips.substr(begin_Pos, found_Pos - begin_Pos);

if(ip.compare("127.0.0.1") == 0)
continue;

std::cout << ip << std::endl;
begin_Pos = found_Pos + 1;
found_Pos = sub_ips.find(";", begin_Pos, 1);
}
}



如果sub_ips="127.0.0.1";
那么此时就永远出不去了,因为
if(ip.compare("127.0.0.1") == 0)
continue;
但是这句没有改变begin的值,所以新的搜索从头开始搜,还是搜到="127.0.0.1",然后又continue了。

所以解决办法是:

if(ip.compare("127.0.0.1") == 0)
{
begin_Pos = found_Pos + 1;
continue;
}


测试的a.txt内容如下:
a=127.0.0.1;192.168.1.1;192.168.1.10;
a=127.0.0.1;192.168.1.1;192.168.1.10;
a=127.0.0.1;192.168.1.1;192.168.1.10;
a=127.0.0.1;192.168.1.1;192.168.1.10;

输出如下:
192.168.1.1
192.168.1.10
192.168.1.1
192.168.1.10
192.168.1.1
192.168.1.10
end
雪人2015 2011-02-24
  • 打赏
  • 举报
回复
为什么我用没有问题呢?
这是我挪用你的主要代码:

#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
string line;
std::ifstream in(L"a.txt");
while (std::getline(in, line) && !in.eof())
{
//std::cout << line << std::endl;

size_t pos = line.find("=");
if (pos == std::string::npos)
continue;

std::string sub_ips = line.substr(pos + 1, line.length() - pos - 1);
size_t begin_Pos = 0;
size_t found_Pos = 0;
size_t length = sub_ips.length();
while (found_Pos != std::string::npos)
{
found_Pos = sub_ips.find(";", begin_Pos, 1);
std::string ip = sub_ips.substr(begin_Pos, found_Pos - begin_Pos);

if(ip.compare("127.0.0.1") == 0)
continue;

std::cout << ip << std::endl;
begin_Pos = found_Pos + 1;
found_Pos = sub_ips.find(";", begin_Pos, 1);
}
}
cout<<"end";
std::cin.get();
return 0;
}


czbever 2011-02-24
  • 打赏
  • 举报
回复
调试过了 不是里面while的问题 是外层那层
pathuang68 2011-02-24
  • 打赏
  • 举报
回复
单步调试一下。
bdmh 2011-02-24
  • 打赏
  • 举报
回复
看看是哪个while出不来,是found_Pos != std::string::npos吗,调试一下便知

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧