我正在用C ++实验CGI。我知道有些库可以处理基本的东西,但是为了知道最新情况,我一直在尝试使用字符串数据类型解析stdin --->使用'=和&'标记化然后push_back进入向量。在后一步,我收到分段错误。下面给出的是我使用cin>>的程序。获取用户输入等等..
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <string.h>
using namespace std;
int main()
{
vector <string> stuff(0);
vector<string>::iterator it;
char* bufferchar;
string buffer;
char str[size];
cout<<"Content-type: text/html\n\n"
<<"<html>"
<<"<head>"
<<"<title>CGI SCRIPT</title>"
<<"</head>"
<<"<body>"
fgets(str,20,stdin); //20 is expect size of content from html form
puts(str);
cout<<"<p>Now to break this guy apart";
int x=0;
bufferchar = strtok(str,"&=");
buffer+=bufferchar;
stuff.push_back(buffer);
while(bufferchar!=NULL){
cout<<bufferchar<<'\n'<<'\n';
bufferchar=strtok(NULL,"&=");
buffer+=bufferchar;
stuff.push_back(buffer);
}
cout<<"<br>ok does the vector iterate ?";
for (it=stuff.begin();it!=stuff.end();++it){
cout<<*it;
cout<<"<br> ok man, next <br><br>";
}
cout<<"</body>";
cout<<"</html>";
}