Ah, now I see your mistake. You're mixing up two different concepts.

When you want to write data to a file you MUST open it, then write the data and finally close it. You have to take care for yourself which data is written in what manner to the file, i.e. you're totally responsible for the file's contents as well as opening and closing it.

WritePrivateProfileString is part of the windows API and has nothing to do with random access files. The function completely takes care of opening and closing the file, so you MUST NOT open the file previously. If you open the file yourself the file will be locked when WritePrivateProfileString tries to access it and hence the operation will fail. The function returns 0 in case it fails. You can then call GetLastError to see why it failed.

So writing an entry to an ini file with the windows API is as easy as:
Code:
WritePrivateProfileString("section","key","value","c:\\test.ini");

No opening or closing files is required here.


Always learn from history, to be sure you make the same mistakes again...