返回信息流同一个循环在一个程序里用了两次(不同函数),第一个可以,第二个却进不了循环。把程序单独拿出来又可以运行。看了半天不明白那个地为啥会出错……疯了……
这是一条镜像帖。来源:北邮人论坛 / cpp / #87661同步于 2015/6/22
该镜像源已超过 30 天没有更新,可能在源站已被删除。
CPP机器人发帖
不知道哪出问题了
yj123
2015/6/22镜像同步13 回复
订阅后,新回复会通过你的通知中心匿名送达。
9 条回复
int DayHiveRecordCountHuanbiMonitorTask::getHiveRecordCountByDay(const std::string& monitoredDayValue)
{
char sqlStr[1024],result[1024];
FILE *f;
sprintf(sqlStr, "hive -S -e \"select count(*) from %s where record_day = '%s'\"", m_tableName.c_str(), monitoredDayValue.c_str());
_LOG_NOTICE("%s-- DayHiveRecordCountHuanbiTask::getHiveRecordCountByDay begin query by sql.[sql:%s]", name().c_str(), sqlStr);
setTag("hive");
f = popen(sqlStr,"r");
while(fgets(result, sizeof(result), f) != NULL)
{
_LOG_NOTICE("**-------%s---------**", result);
if(isdigit(result[0]))
{
pclose(f);
return atoi(result);
}
}
if(fgets(result,sizeof(result),f) == NULL)
_LOG_INFO("maybe is empty\n");
fgets(result,sizeof(result),f);
return atoi(result);
pclose(f);
return 0;
}
while语句进不去,fgets(result,sizeof(result),f) == NULL成立,但是我把这个函数单独拿出来运行while语句又是可以运行的,好奇怪……
【 在 nuanyangyang 的大作中提到: 】
: 贴代码
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <cctype>
int test1()
{
char sqlStr[1024],result[1024];
FILE *f;
sprintf(sqlStr, "hive -S -e \"select count(*) from ods_iread.mid_iread_logon where record_day = '20150621'\"");
f = popen(sqlStr,"r");
while(fgets(result, sizeof(result), f) != NULL)
{
if(isdigit(result[0]))
{
pclose(f);
return atoi(result);
}
}
printf("@@@@@@@@@ 174\n");
return -1;
}
int main()
{
printf("%d\n",test1());
return 0;
}
运行结果:
0
【 在 nuanyangyang 的大作中提到: 】
: 你是怎么“把这个函数单独拿出来运行while语句”的?“可以运行”的话,结果是什么?
另外,就算你输出0,读取真的成功了吗?试试把result原封不动地打出来,而不是用atoi。
atoi是个很老的函数,建议使用sscanf或者strtol。
能加一下QQ吗?感觉有点说不清楚……
【 在 nuanyangyang 的大作中提到: 】
: 另外,就算你输出0,读取真的成功了吗?试试把result原封不动地打出来,而不是用atoi。
: atoi是个很老的函数,建议使用sscanf或者strtol。