1

I'm using var args in a file i/o function. It works fine unless the string which is wanting to be outputted contains a % and there are no additional arguments. E.g. the string <name px=100% /> would cause the issue, as an example. The code is roughly as follows:

void OutV(CString s, va_list args)
{
    CString formatted;
    formatted.FormatV(str, args);

    // Do stuff with formatted....
}

Is there a way that I could make it so that having a % symbol does not cause a problem. Ideally I'd like it inside of the OutV function above so that it only has to be in one place.

I can't do a simple replace all % with %% as then if there is a %d in there then that still needs to be substituted as an integer.

Luke
  • 560
  • 6
  • 26

1 Answers1

1

Make sure your literal format strings escape the percent sign by doubling it: %%.

You might also want to consider using something like

OutV(CString("%s"), "<name px=100% />");

UPDATE

I can't do a simple replace all % with %% as then if there is a %d in there then that still needs to be substituted.

You need to piece things together more carefully. e.g.,

OutV(CString("%s%d%s"), "<name px=", 100, "% />");
wilx
  • 17,697
  • 6
  • 59
  • 114
  • I've edited the OP: "I can't do a simple replace all % with %% as then if there is a %d in there then that still needs to be substituted" – Luke Dec 08 '14 at 14:52