WritePrivateProfileString is not adding property to the end
Asked Answered
N

2

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?

Neb answered 12/7, 2012 at 12:15 Comment(1)
They don't support multiline items. You'll see this when you try and read it back,Oxalate
F
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.

Fuzzy answered 12/7, 2012 at 12:31 Comment(5)
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,Neb
@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.Fuzzy
i am afraid i cannot do this,Neb
@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.Fuzzy
I'm looking for a c# wrapper of this project (Simpleini). Anybody? :DProthrombin
T
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.

Tranquilize answered 12/7, 2012 at 23:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.