sscanf,snprintf

从一个字符串中读进与指定格式相符的数据.
例子:
#define COMPRESS_PET "%d,%d,%d,%d,%d,%d,%ld,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d"
int nID = 0;
int nRoleID = 0;
int nSpeciesID = 0;
int nGrade = 0;
int nCharacter = 0;
int nExp = 0;
time_t nBirthday = 0;
int nHp = 0;
int nGrowRate = 0;
int nHpLearn = 0;
int nAttackLearn = 0;
int nDefenseLearn = 0;
int nSuperAttackLearn = 0;
int nSuperDefenseLearn = 0;
int nSpeedLearn = 0;
int nActiveSkillOne = 0;
int nActiveSkillOneCount = 0;
int nActiveSkillTwo = 0;
int nActiveSkillTwoCount = 0;
int nActiveSkillThree = 0;
int nActiveSkillThreeCount = 0;
int nActiveSkillFour = 0;
int nActiveSkillFourCount = 0;
int nPassiveSkillOne = 0;
int nPassiveSkillTwo = 0;
int nPassiveSkillThree = 0;
int nPassiveSkillFour = 0;
int nPlace = 0;
int nMaxHp = 0;
int nAttack = 0;
int nDefense = 0;
int nSuperAttack = 0;
int nSuperDefense = 0;
int nSpeed = 0;
sscanf((*iter).c_str(),COMPRESS_PET, &nID, &nRoleID, &nSpeciesID, &nGrade,
&nCharacter, &nExp, &nBirthday, &nHp, &nGrowRate, &nHpLearn,
&nAttackLearn, &nDefenseLearn, &nSuperAttackLearn, &nSuperDefenseLearn,
&nSpeedLearn, &nActiveSkillOne, &nActiveSkillOneCount, &nActiveSkillTwo,
&nActiveSkillTwoCount, &nActiveSkillThree, &nActiveSkillThreeCount,
&nActiveSkillFour, &nActiveSkillFourCount, &nPassiveSkillOne,
&nPassiveSkillTwo, &nPassiveSkillThree, &nPassiveSkillFour, &nPlace,
&nMaxHp, &nAttack, &nDefense, &nSuperAttack, &nSuperDefense, &nSpeed);

snprintf

int snprintf(char *str, size_t size, const char *format, ...);
将可变个参数(...)按照format格式化成字符串,然后将其复制到str中
(1) 如果格式化后的字符串长度 < size,则将此字符串全部复制到str中,并给其后添加一个字符串结束符('\0');
(2) 如果格式化后的字符串长度 >= size,则只将其中的(size-1)个字符复制到str中,并给其后添加一个字符串结束符('\0')
函数返回值:若成功则返回欲写入的字符串长度,若出错则返回负值。
例子:
char *pBuffer = GetSQLBuffer();
snprintf(pBuffer,GetSQLBufferSize() - 1, SQL_UPDATE_ROLE_PET, GetNum(), GetIDs().c_str(), GetDetails().c_str(),GetRoleID());
char szSQL[1024] = {0};
snprintf(szSQL, 1023, SQL_QUERY_ROLE_PET_BINARY, GetRoleID());

posted @ 2011-10-14 19:04  byfei  阅读(50)  评论(0编辑  收藏  举报