解析一个字符串 [英] parsing a string

查看:110
本文介绍了解析一个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串:


" FL:1234ABCD:3:FileName With Spaces.txt \ n"


我想把'':''分隔的值读成变量。我试图使用这样的sscanf




sscanf(" FL:%s:%d:%s \ n",lGuid,& ; lID,lFileName);


但是lGUID只会继续,直到找到空格或\0。我需要一种方式

将'':''设置为分隔符,并且lFileName的方式甚至可以读取含有空格的


基本上,具有可指定的speparators /终结符的sscanf版本是我需要的b $ b。有什么可以帮助我吗?目前,我正在跳过

冒号并使用凌乱的memcpy()。


谢谢

Allan

I have a string like:

"FL:1234ABCD:3:FileName With Spaces.txt\n"

and I want to read the values separated by '':'' into variables. I tried to
use sscanf like this:

sscanf("FL:%s:%d:%s\n", lGuid, &lID, lFileName);

but the lGUID just continues until whitespace or \0 is found. I need a way
to set the '':'' as a separator, and a way for lFileName to be readable even
with whitespace in it.
Basically, a version of sscanf with specifiable speparators/terminators is
what I require. Is there anything to help me? At the moment, I am skipping
colons and using memcpy() which is messy.

Thanks
Allan

推荐答案

" Allan Bruce" <人***** @ TAKEAWAYf2s.com>在消息中写道

新闻:c5 ********** @ news.freedom2surf.net ...
"Allan Bruce" <al*****@TAKEAWAYf2s.com> wrote in message
news:c5**********@news.freedom2surf.net...
我有一个字符串:

" FL:1234ABCD:3:FileName With Spaces.txt \ n"

我希望将'':''分隔的值读入变量。我试图像这样使用sscanf:

sscanf(" FL:%s:%d:%s \ n",lGuid,& lID,lFileName);

但是lGUID只会继续,直到找到空格或\0。我需要一种
方式来设置'':''作为分隔符,以及lFileName甚至可以读取内容空间的方式。
基本上,一个版本sscanf与可指定的speparators /终结符是我需要的。有什么可以帮助我吗?目前,我正在跳过
冒号并使用凌乱的memcpy()。
I have a string like:

"FL:1234ABCD:3:FileName With Spaces.txt\n"

and I want to read the values separated by '':'' into variables. I tried to
use sscanf like this:

sscanf("FL:%s:%d:%s\n", lGuid, &lID, lFileName);

but the lGUID just continues until whitespace or \0 is found. I need a
way
to set the '':'' as a separator, and a way for lFileName to be readable even
with whitespace in it.
Basically, a version of sscanf with specifiable speparators/terminators is
what I require. Is there anything to help me? At the moment, I am
skipping
colons and using memcpy() which is messy.




这是怎么回事?


#include< string>

#include< sstream>

using namespace std;


int main()

{

string s =" FL:1234ABCD:3:FileName With Spaces.txt \\\
" ;;

string FileName;

int ID;

string GUID;

istringstream stream(s);

stream.ignore(3); // ignore" FL:"

getline(stream,GUID,'':'');

stream>> ID;

stream.ignore(); //在id之后忽略'':''
getline(stream,FileName,''\ n'');


返回0;

}



How''s about something like this?

#include <string>
#include <sstream>
using namespace std;

int main()
{
string s = "FL:1234ABCD:3:FileName With Spaces.txt\n";
string FileName;
int ID;
string GUID;
istringstream stream(s);
stream.ignore(3); // ignore "FL:"
getline(stream, GUID, '':'');
stream >> ID;
stream.ignore(); // ignore the '':'' after id
getline(stream, FileName, ''\n'');

return 0;
}


Allan Bruce< al ***** @ TAKEAWAYf2s.com>写道:
Allan Bruce <al*****@TAKEAWAYf2s.com> wrote:
我有一个字符串:

" FL:1234ABCD:3:FileName With Spaces.txt \ n"

和我想将'':''分隔的值读入变量。我试图像这样使用sscanf:

sscanf(" FL:%s:%d:%s \ n",lGuid,& lID,lFileName);

但是lGUID只会继续,直到找到空格或\0。我需要一种方法
将'':''设置为分隔符,以及lFileName甚至可以读取内容空间的方式。
基本上,sscanf的一个版本可以指定speparators / terminators是我需要的。有什么可以帮助我吗?目前,我正在跳过
冒号并使用凌乱的memcpy()。


const std :: string str =" FL:1234ABCD:3:FileName With Spaces.txt \ n";

std :: istringstream iss (str);


std :: string FL;

std :: getline(iss,FL,'':'');

if(FL!=" FL" ||!iss.good())throw invalid_input(str);


std :: string GUID;

std :: getline(iss,GUID,'':'');

if(!isValid(GUID)||!iss.good())throw invalid_input(str);


int ID;

iss>> ID;

如果(!iss.good())抛出invalid_input(str);


char ch =''\ 0'';

iss>> ch;

if(ch!='':''||!iss.good())抛出invalid_input(str);


std :: string fname;

std :: getline(iss,fname);

if(!iss.eof())throw invalid_input(str);

谢谢


HTH,

Allan
I have a string like:

"FL:1234ABCD:3:FileName With Spaces.txt\n"

and I want to read the values separated by '':'' into variables. I tried to
use sscanf like this:

sscanf("FL:%s:%d:%s\n", lGuid, &lID, lFileName);

but the lGUID just continues until whitespace or \0 is found. I need a way
to set the '':'' as a separator, and a way for lFileName to be readable even
with whitespace in it.
Basically, a version of sscanf with specifiable speparators/terminators is
what I require. Is there anything to help me? At the moment, I am skipping
colons and using memcpy() which is messy.
const std::string str = "FL:1234ABCD:3:FileName With Spaces.txt\n";
std::istringstream iss( str );

std::string FL;
std::getline( iss, FL, '':'' );
if( FL!="FL" || !iss.good() ) throw invalid_input(str);

std::string GUID;
std::getline( iss, GUID, '':'' );
if( !isValid(GUID) || !iss.good() ) throw invalid_input(str);

int ID;
iss >> ID;
if( !iss.good() ) throw invalid_input(str);

char ch = ''\0'';
iss >> ch;
if( ch!='':'' || !iss.good() ) throw invalid_input(str);

std::string fname;
std::getline( iss, fname );
if( !iss.eof() ) throw invalid_input(str);
Thanks
HTH,
Allan



Schobi


-
Sp******@gmx.de 永远不会被阅读

我是Schobi at suespammers dot org


有时编译器比人们更合理。

Scott Meyers


Schobi

--
Sp******@gmx.de is never read
I''m Schobi at suespammers dot org

"Sometimes compilers are so much more reasonable than people."
Scott Meyers




" Allan Bruce" <人***** @ TAKEAWAYf2s.com>在消息中写道

新闻:c5 ********** @ news.freedom2surf.net ...

"Allan Bruce" <al*****@TAKEAWAYf2s.com> wrote in message
news:c5**********@news.freedom2surf.net...
我有一个字符串:

" FL:1234ABCD:3:FileName With Spaces.txt \ n"

我希望将'':''分隔的值读入变量。我试图像这样使用sscanf:

sscanf(" FL:%s:%d:%s \ n",lGuid,& lID,lFileName);

但是lGUID只会继续,直到找到空格或\0。我需要一个
的方式来设置'':''作为分隔符,并且lFileName的一种方式甚至可以读取其中有空格。
基本上,一个版本的sscanf可以指定speparators / terminators是我需要的。有什么可以帮助我吗?目前,我是
跳过冒号并使用memcpy()这很乱。

感谢
Allan
I have a string like:

"FL:1234ABCD:3:FileName With Spaces.txt\n"

and I want to read the values separated by '':'' into variables. I tried to
use sscanf like this:

sscanf("FL:%s:%d:%s\n", lGuid, &lID, lFileName);

but the lGUID just continues until whitespace or \0 is found. I need a way to set the '':'' as a separator, and a way for lFileName to be readable even
with whitespace in it.
Basically, a version of sscanf with specifiable speparators/terminators is
what I require. Is there anything to help me? At the moment, I am skipping colons and using memcpy() which is messy.

Thanks
Allan



感谢帮助人员 - 我想要一些较短的手,并发现了一些关于sscanf的事情。现在我只需要使用:


sscanf(xiBuffer," FD:%[^:]%s:%[^:]%d:%d",lGUID,& ; lLastPktFlag,

& lDataLength);


和它有效的待遇

Allan



Thanks for the help guys - I wanted something shorter hand, and found out
some things about sscanf. Now I just need to use:

sscanf(xiBuffer, "FD:%[^:]%s:%[^:]%d:%d", lGUID, &lLastPktFlag,
&lDataLength);

and it works a treat
Allan


这篇关于解析一个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆