question

HolgerFarr-2881 avatar image
0 Votes"
HolgerFarr-2881 asked HolgerFarr-2881 commented

Error in WritePrivateProfileString function when just writing space characters

Hello,

when using the following code to 10x write a single space into an ini-file, the corresponding entry in that file contains 10 spaces.

 #include <windows.h>
 #include <cassert>
 int main()
 {
     for(int i = 0; i < 10; ++i)
     {
         const BOOL bRet = WritePrivateProfileStringA("Section", "SingleSpace", " ", "C:\\TEMP\\iniFileTest.ini");
         assert(bRet);
     }
     //const BOOL bRet = WritePrivateProfileStringA("Section", "SingleSpace", "Hello World", "C:\\TEMP\\iniFileTest.ini");
     //assert(bRet);
     return 0;
 }

When you uncomment lines 10 and 11 in the code sample and run the program again the corresponding "SingleSpace" entry in the ini-file contains "Hello World", followed by 10 spaces. (Provided that you deleted the "SingleSpace"-key in the ini-file before, otherwise its 20 spaces.)
I found no possibility to remove that trailing spaces using WritePrivateProfileString except deleting the whole key and inserting it again. But this will change the order in the ini file.

So it looks that WritePrivateProfileString has a problem with trailing spaces. Any chance that this "unexpected" behavior will be fixed? Or is there a different workaround? (Unfortunately it is not an option to write the key-value pairs to registry or use a different format (like XML or JSON) for storing them.)
For my application I actually use a trim-right function before writing to the ini-file but when there are already trailing spaces in an entry they will remain forever (or until the user removes them by hand).

The code sample was run with Visual Studio Professional 2017 version 15.9.39 using the corresponding C++ compiler and latest Windows-SDK version (10.0.17763.0).

Best regards
Holger Farr










windows-api
· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

If your objective in passing a space to WritePrivateProfileStringA is to enter the key in the file without a corresponding value then you can accomplish the same thing by passing an empty string ("") instead of a space.

0 Votes 0 ·

Well indeed that was the intention when the code in our application was changed about 10 years ago. (The corresponding comment for that change says that there was a problem with GetPrivateProfileString function reading empty values, so a space was written instead of an empty entry.)

But recently someone asked why there are a lot of spaces in his ini file and I found that WritePrivateProfileString does not remove trailing spaces. So when you by mistake write a value with a trailing space into the ini-file, e.g.

 WritePrivateProfileStringA("Section", "SingleSpace", "Hello World ", "C:\\TEMP\\iniFileTest.ini");

your ini-file will grow by one byte each time you write that string.

As the current version of GetPrivateProfileString function has no problem handling empty values, for our application it is okay to just trim the values before writing them to the ini file (and tell the customer to delete the trailing spaces by hand).

Best regards
Holger Farr

0 Votes 0 ·

It seems that GetPrivateProfileString will strip the trailing blanks that are contained in the .ini file when it returns the value.

IMHO it is highly unlikely that Microsoft will change the behavior of the WritePrivateProfileString and GetPrivateProfileString functions.

0 Votes 0 ·
Show more comments

1 Answer

XiaopoYang-MSFT avatar image
0 Votes"
XiaopoYang-MSFT answered HolgerFarr-2881 commented

You can pass NULL in lpString of WritePrivateProfileString to delete the key pointed to by the lpKeyName parameter and then write again. It‘s documented for WritePrivateProfileString to not delete existing data.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Yes I know, but that changes the order of the key-value pairs inside the ini file.

Best regards
Holger Farr

0 Votes 0 ·