如何在C中设置变量类型char数组的格式说明符的宽度?

时间:2021-04-07 22:53:02

I have a function --

我有一个功能 -

bool DataPersistence::persistUIDetails (const char *data, quint32 len)
{
  TLOG_FUNC_ENTER();
  bool retVal = true;

  char tmp[len+1];
  strncpy (tmp, data, len);

  //Working on persisting the UI details.
  char type[len];
  char uiVers[len];
  char mwVers[len];
  char ts[len];

  sscanf (tmp, "%s %s %s %s", type, uiVers, mwVers, ts);

}

I want to put a maximum width for this format specifier. How can I do so in C?

我想为此格式说明符设置最大宽度。我怎么能在C中这样做?

Something like %255S

像%255S这样的东西

1 个解决方案

#1


5  

Use sprintf to print the format string:

使用sprintf打印格式字符串:

char formatString[256];
sprintf(formatString, "%%%ds %%%ds %%%ds %%%ds", len, len, len, len);

Then use it in sscanf:

然后在sscanf中使用它:

sscanf (tmp, formatString, type, uiVers, mwVers, ts);

#1


5  

Use sprintf to print the format string:

使用sprintf打印格式字符串:

char formatString[256];
sprintf(formatString, "%%%ds %%%ds %%%ds %%%ds", len, len, len, len);

Then use it in sscanf:

然后在sscanf中使用它:

sscanf (tmp, formatString, type, uiVers, mwVers, ts);