2

I am writing some properties in a ini file using WritePrivateProfileString function and everything works fine but when I add text with multiple lines, there is a problem.

Here is the code and output.

WritePrivateProfileString(_T("General"), _T("Name"), OLE2CT(text), FilePath);

Output:

[General]
Name=mytext

.

text = address\nstreet\nhouse
WritePrivateProfileString(_T("General"), _T("Address"), OLE2CT(text), FilePath);

Output:

[General]
Name=mytext
Address=address
street
house

But when after adding a multiple line item, i add another item, instead of adding this to end it adds new line just after Address line

text = city
WritePrivateProfileString(_T("General"), _T("City"), OLE2CT(text), FilePath);

Output:

[General]
Name=mytext
Address=address
City=city
street
house

but the output should be

[General]
Name=mytext
Address=address
street
house
City=city

What is problem with my code?

0xC0000022L
  • 20,597
  • 9
  • 86
  • 152
fhnaseer
  • 7,159
  • 16
  • 60
  • 112

2 Answers2

4

I strongly recommend you read up on your problems on Michael Kaplan's blog.

If you absolutely have to use INI files, don't use the deprecated Win32 API functions you are using right now. They are buggy and bugs sure won't get fixed anymore as they are deprecated by now.

Instead use SimpleIni a very decent cross-platform implementation of an INI reader/writer for C++.

Microsoft (as a whole) seems to be unsure whether they prefer registry or other mechanisms for storing configuration data. At some point it was INI files, then it was the registry (to me a superior mechanism) and then it seemed to shift towards XML and other file-based storage mechanisms. It's certainly your use-case that will define what you need, but consider all the words of caution about using these deprecated functions and look at least for an alternative mechanism of working with INI file if you have to.

0xC0000022L
  • 20,597
  • 9
  • 86
  • 152
  • i am working on very old code, there are many calls to this function, by going to something else will require more effort, isnt there any other solution, like by using this new item is added to end instead of last item next line, – fhnaseer Jul 12 '12 at 12:48
  • @Faisal Hafeez: I have worked on very old code a lot myself. What I am doing in such a case most of the time is to write an adapter function and use the powers of the preprocessor to morph the code into the shape I want to have it. Here you could write your own version of `WritePrivateProfileString` (name it something else though), based on SimpleIni and then use `#define` to point your code globally to your function. You may have to redefine `WritePrivateProfileString` *before* and then again *after* including `windows.h` to prevent collisions. – 0xC0000022L Jul 12 '12 at 12:57
  • i am afraid i cannot do this, – fhnaseer Jul 12 '12 at 13:00
  • @Faisal Hafeez: well, I'm afraid then you are on your own. The problem with these functions is that they have been abandoned. The only reason they still exist is for backward compatibility and they are more likely to go out in the future than some other deprecated APIs. The reason being that these functions have been abused as an easy route to get INI parsing, though they were originally not meant for that. Sorry, but you are on your own. I stand by my answer. – 0xC0000022L Jul 12 '12 at 13:06
  • I'm looking for a c# wrapper of this project (Simpleini). Anybody? :D – Pedro77 Jun 21 '14 at 22:29
2

Well seeing that is not the proper format for an INI file for the API functions, what do you expect?

The format for an ini file is:

[section]
item1=item1text
item2=item2text
...

[anothersection]
item1=item1text
item2=item2text
...

If you want to use the ini API calls, then you must adhere to the format. You want city, street, and house to be a part of the City item? Then put them all on the "same line" and use a separator that you can later parse for each field. You could use a comma, pipe, or anything else that won't be part of the text.

Gunner
  • 5,780
  • 2
  • 25
  • 40