C++新手现遇棘手问题,向各位前辈求解.

wfyq 2003-10-20 11:59:48
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<mysql/mysql.h>
#include<string>

using namespace std;
int main()

{
MYSQL mysql;
MYSQL_RES * result;
MYSQL_ROW row;
string input(getenv("QUERY_STRING"));
string phone;
char *display;
char *p;

cout<<"Content-type:text/html"<<endl<<endl;
cout <<"<html>";

string temp=input;
p=strtok(temp.begin(),"&"); //编译时这一行有错,但我不知如何该.
phone.assign(p);
phone.erase(0,phone.find("=")+1);
mysql_init(&mysql);
mysql_real_connect(&mysql,NULL,"root",NULL,"test",0,0,0);
char query[255];
sprintf(query,"select * from test where='%s'", phone.c_str());
mysql_query(&mysql,query);
result=mysql_store_result(&mysql);

if(mysql_num_rows(result)==0)
{
display=new char[25];
strcpy(display,"phone not fonud!!Retry!!");
cout<<display<<endl;
exit(1);
}
while(row=mysql_fetch_row(result))
{
cout<<"<br>";
cout<<row[0]<<":"<<row[1]<<":"<<row[2]<<":";
cout<<row[3];
cout<<"</br>";
}
cout<<"</html>";
mysql_close(&mysql);
return 0;
}
...全文
30 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
Wolf0403 2003-10-22
  • 打赏
  • 举报
回复
strtoc(temp.c_str(), "&"); 试试?
wfyq 2003-10-22
  • 打赏
  • 举报
回复
改为: p=strtok(temp,"&");
错误信息~
cannot convert `std::string' to `char*' for argument `1' to `char*
strtok(char*, const char*)'
bosedom 2003-10-22
  • 打赏
  • 举报
回复
能不能把报错的信息贴出来,说不定不是这句的错,可能编译器判断错了。
wfyq 2003-10-22
  • 打赏
  • 举报
回复
不行,
还是那一行报错~~

wfyq 2003-10-22
  • 打赏
  • 举报
回复
感谢以上各位..

问题解决..

楼上的前辈是正确的....
bosedom 2003-10-22
  • 打赏
  • 举报
回复
p=strtok((char*)temp.c_str(),"&");
wfyq 2003-10-22
  • 打赏
  • 举报
回复
难道没有人知道吗??
我等了几天了?

帮帮忙吧.
bosedom 2003-10-21
  • 打赏
  • 举报
回复
直接打strtok(temp,"&");
fifo333 2003-10-21
  • 打赏
  • 举报
回复
不了解strtok函数,是不是MySQL提供的?
wfyq 2003-10-21
  • 打赏
  • 举报
回复
但是:把 p=strtok(temp.begin(),"&");
改为 p=strtok(temp,"&");
一样是无法通过.

怎样该才能通过???
fifo333 2003-10-21
  • 打赏
  • 举报
回复
temp.begin()应该是返回一个迭代器吧,可以看作指针使用。


#include <string>
#include <iostream>
#include <cstdlib>

using namespace std;

void proc()
{
string s("abcdefg");
cout<<"s: "<<s<<endl;

//cout<<s.begin()<<endl; //这句编译通不过

for(int i=0;i<s.size();++i){
char ch1=s[i];
char ch2=*(s.begin()+i);
cout<<"s["<<i<<"]"<<": "<<ch1<<"\t"
<<"*(s.begin()+"<<i<<"): "<<ch2<<endl;
}

}

int main()
{
proc();

system("pause");
return 0;

}
bosedom 2003-10-21
  • 打赏
  • 举报
回复
char *strtok( char *strToken, const char *strDelimit )
这是MSDN上的对这个函数的声明,第一个参数就是要一个字符串,而string类型的就是一个字符串,所以直接打就行了。
至于temp.begin(),我咋一看以为是一个iterator,但是你没有这要定义,如果你为这个string定义了iterator的话,begin函数返回的就是这个temp的首字符。
njuhuangmy 2003-10-21
  • 打赏
  • 举报
回复
temp.begin() 得到的 只是 temp 的首字符

而不是 temp 整个字符串

Andy84920 2003-10-21
  • 打赏
  • 举报
回复
string 也有 begin()么?
oyd 2003-10-21
  • 打赏
  • 举报
回复
temp.begin()返回的是一个叠代器
wfyq 2003-10-21
  • 打赏
  • 举报
回复
还是不太明白,
谁能帮我详细解释一下啊.

感激不尽...
panzhaoping 2003-10-21
  • 打赏
  • 举报
回复
楼上的那位说的不错
strtok本来是用在提炼字符串的
temp.begin()不是字符串了

64,282

社区成员

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

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