简单的解析字符串有关问题
简单的解析字符串问题
有:~qwe=234~ere=345~ter=tyt~454=rter
怎么解析出来:
得到:qwe=234
ere=345
ter=tyt
454=rter
~~~~~~~~~~~~
急用!!
------解决方案--------------------
int main()
{
ifstream infile;
string filename;
cout < < "Please enter the file name: ";
cin > > filename;
infile.open(filename.c_str());
string line;
getline(infile, line, '\n ');
infile.close();
vector <string> wordsOfLine;
string::size_type pos = 0, prev_pos =0;
string word;
while ((pos = line.find_first_of( '~ ', pos)) != string::npos)
{
word = line.substr(prev_pos, pos - prev_pos);
prev_pos = ++pos;
wordsOfLine.push_back(word);
}
wordsOfLine.push_back(line.substr(prev_pos, pos - prev_pos));
}
------解决方案--------------------
for(vector <string> ::const_iterator cit = wordsOfLine.begin(); cit != wordsOfLine.end(); ++ cit)
{
cout < < *cit < < endl;
}
即可以显示出
呵呵^_^,okokok
------解决方案--------------------
#include <string.h>
#include <stdio.h>
char string[] = "~qwe=234~ere=345~ter=tyt~454=rter;
char seps[] = "~ ";
char *token;
int main( void )
{
token = strtok( string, seps );
while( token != NULL )
{
printf( " %s\n ", token );
token = strtok( NULL, seps );
}
}
------解决方案--------------------
木知道lz想怎么弄的说
------解决方案--------------------
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
char str_tmp[128],str1[128],str2[128],str3[128],str4[128];
cin> > setw(128)> > str_tmp;
char *str_ptr[4]={
str1,str2,str3,str4
};
int i=0,n=0,m=0;
while(str_tmp[i]== '~ ')
{
while((str_tmp[++i]!= '~ ')&&str_tmp[i]!= '\0 ')
{
*(str_ptr[n]+m)=str_tmp[i];
m++;
}
*(str_ptr[n]+m+1)= '\0 ';
n++;
m=0;
}
for(int n=0;n <4;n++)
cout < <str_ptr[n] < <endl;
system( "pause ");
return 0;
}
WinXP+SP2 Dev-Cpp 4.9.9.2环境下调试通过
------解决方案--------------------
C/C++ 不是这样滴 ...
楼主你那个是啥?
PHP么?
------解决方案--------------------
最多只能根据串解析出一些“关系”:
#include <map>
#include <string>
#include <iostream>
#include <cstdlib>
using namespace std;
int main(int argc, char *argv[])
{
string line( "~qwe=234~ere=345~ter=tyt~454=rter ");
map <string, string> strmap;
map <string, string> ::iterator it;
string str, pre, suf;
string::size_type index=0;
有:~qwe=234~ere=345~ter=tyt~454=rter
怎么解析出来:
得到:qwe=234
ere=345
ter=tyt
454=rter
~~~~~~~~~~~~
急用!!
------解决方案--------------------
int main()
{
ifstream infile;
string filename;
cout < < "Please enter the file name: ";
cin > > filename;
infile.open(filename.c_str());
string line;
getline(infile, line, '\n ');
infile.close();
vector <string> wordsOfLine;
string::size_type pos = 0, prev_pos =0;
string word;
while ((pos = line.find_first_of( '~ ', pos)) != string::npos)
{
word = line.substr(prev_pos, pos - prev_pos);
prev_pos = ++pos;
wordsOfLine.push_back(word);
}
wordsOfLine.push_back(line.substr(prev_pos, pos - prev_pos));
}
------解决方案--------------------
for(vector <string> ::const_iterator cit = wordsOfLine.begin(); cit != wordsOfLine.end(); ++ cit)
{
cout < < *cit < < endl;
}
即可以显示出
呵呵^_^,okokok
------解决方案--------------------
#include <string.h>
#include <stdio.h>
char string[] = "~qwe=234~ere=345~ter=tyt~454=rter;
char seps[] = "~ ";
char *token;
int main( void )
{
token = strtok( string, seps );
while( token != NULL )
{
printf( " %s\n ", token );
token = strtok( NULL, seps );
}
}
------解决方案--------------------
木知道lz想怎么弄的说
------解决方案--------------------
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
char str_tmp[128],str1[128],str2[128],str3[128],str4[128];
cin> > setw(128)> > str_tmp;
char *str_ptr[4]={
str1,str2,str3,str4
};
int i=0,n=0,m=0;
while(str_tmp[i]== '~ ')
{
while((str_tmp[++i]!= '~ ')&&str_tmp[i]!= '\0 ')
{
*(str_ptr[n]+m)=str_tmp[i];
m++;
}
*(str_ptr[n]+m+1)= '\0 ';
n++;
m=0;
}
for(int n=0;n <4;n++)
cout < <str_ptr[n] < <endl;
system( "pause ");
return 0;
}
WinXP+SP2 Dev-Cpp 4.9.9.2环境下调试通过
------解决方案--------------------
C/C++ 不是这样滴 ...
楼主你那个是啥?
PHP么?
------解决方案--------------------
最多只能根据串解析出一些“关系”:
#include <map>
#include <string>
#include <iostream>
#include <cstdlib>
using namespace std;
int main(int argc, char *argv[])
{
string line( "~qwe=234~ere=345~ter=tyt~454=rter ");
map <string, string> strmap;
map <string, string> ::iterator it;
string str, pre, suf;
string::size_type index=0;