{"id":1297,"date":"2016-01-18T15:29:53","date_gmt":"2016-01-18T14:29:53","guid":{"rendered":"http:\/\/www.amibroker.com\/kb\/?p=1297"},"modified":"2016-01-28T15:38:53","modified_gmt":"2016-01-28T14:38:53","slug":"how-to-fix-error-61-in-printfstrformat-calls","status":"publish","type":"post","link":"https:\/\/www.amibroker.com\/wordpress\/kb\/2016\/01\/18\/how-to-fix-error-61-in-printfstrformat-calls\/","title":{"rendered":"How to fix Error 61 in printf\/StrFormat calls"},"content":{"rendered":"
AmiBroker version 6.07 has introduced a strict check for correct string formatting in printf and StrFormat functions. These functions allow to specify the string followed by the list of arguments that will be inserted into the string in places, where %f, %g or %e specifiers are entered.<\/p>
This works the following way:<\/p>
<\/p>
It is important for the list of subsequent arguments to match the number of % specifiers in the string. In cases, where there is no match – AmiBroker will display Error 61 message. Strict check is required because Microsoft C runtime crashes if wrong parameters are passed. Passing on earlier version was dangerous because it would lead to random crashes now and then depending on machine configuration. <\/p>
There may be the following typical problems in the code:<\/p>
Example 1. Four % specifiers, five value arguments<\/strong> <\/p> In this example formatting string contains four % specifiers so AmiBroker expects four arguments coming later, but five are given instead (too many). Example 2. Five % specifiers, four value arguments<\/strong><\/p> In this example formatting string contains five % specifiers so AmiBroker expects five arguments coming later, but four are given instead (too few). <\/p> Example 3. Incorrectly coded percent (%) sign <\/strong><\/p> In this example user wanted to print just % (percent sign), but used % (wrong) instead of %% (correct specifier of literal percent sign). <\/p> The example 3 requires special attention, as it is a common mistake. Due to the fact that % sign is a special character, we need to use %% in our string if we want to print % sign in the output string.<\/p>","protected":false},"excerpt":{"rendered":" AmiBroker version 6.07 has introduced a strict check for correct string formatting in printf and StrFormat functions. These functions allow to specify the string followed by the list of arguments that will be inserted into the string in places, where %f, %g or %e specifiers are entered.This works the following way:It is important for the […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2,25],"tags":[53,89,90],"_links":{"self":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/1297"}],"collection":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/comments?post=1297"}],"version-history":[{"count":2,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/1297\/revisions"}],"predecessor-version":[{"id":1303,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/1297\/revisions\/1303"}],"wp:attachment":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/media?parent=1297"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/categories?post=1297"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/tags?post=1297"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}<\/span>\/\/ WRONG - too many value arguments
<\/span>Title <\/span>= <\/span>StrFormat<\/span>( <\/span>"Open: %g, High: %g, Low: %g, Close: %g"<\/span>, <\/span>Open<\/span>, <\/span>High<\/span>, <\/span>Low<\/span>, <\/span>Close<\/span>, <\/span>Volume <\/span>);
<\/span>\/\/ CORRECT
<\/span>Title <\/span>= <\/span>StrFormat<\/span>( <\/span>"Open: %g, High: %g, Low: %g, Close: %g"<\/span>, <\/span>Open<\/span>, <\/span>High<\/span>, <\/span>Low<\/span>, <\/span>Close <\/span>)<\/code><\/span>\/\/ WRONG - %.0f specifier does not have a matching argument
<\/span>Title <\/span>= <\/span>StrFormat<\/span>( <\/span>"Open: %g, High: %g, Low: %g, Close: %g, Volume: %.0f"<\/span>, <\/span>Open<\/span>, <\/span>High<\/span>, <\/span>Low<\/span>, <\/span>Close <\/span>);
<\/span>\/\/ CORRECT
<\/span>Title <\/span>= <\/span>StrFormat<\/span>( <\/span>"Open: %g, High: %g, Low: %g, Close: %g, Volume: %.0f"<\/span>, <\/span>Open<\/span>, <\/span>High<\/span>, <\/span>Low<\/span>, <\/span>Close<\/span>, <\/span>Volume <\/span>)<\/code><\/span>\/\/ WRONG - to show the % sign in the output we need to use %%
<\/span>Title <\/span>= <\/span>StrFormat<\/span>( <\/span>"Close: %g (%.1f%)"<\/span>, <\/span>Close<\/span>, <\/span>SelectedValue<\/span>( <\/span>ROC<\/span>( <\/span>Close<\/span>, <\/span>1<\/span>) ) );
<\/span>\/\/ CORRECT - you should use %% to print actual percent sign
<\/span>Title <\/span>= <\/span>StrFormat<\/span>( <\/span>"Close: %g (%.1f%%)"<\/span>, <\/span>Close<\/span>, <\/span>SelectedValue<\/span>( <\/span>ROC<\/span>( <\/span>Close<\/span>, <\/span>1<\/span>) ) )<\/code>