WritePrivateProfileString

Posted By: Yashas

WritePrivateProfileString - 10/22/12 15:20

Code:
void RegisterUser()
{
	char * profile_doc;
	STRING * username = str_create((LoginPrompt_Name->pstring)[0]);
	profile_doc = str_cat((str_create("Users\\Accounts\\")),(str_cat(username,str_create(".txt"))));
	if(file_exists(str_create(profile_doc)))
	{
		pan_setdigits(LoginPan,1,20,100,"Profile already exists!",MenuFont,0,freevalue);
		snd_play(Error,menuvolume,menubalance);
		return;
	}
	else
	{
		file_open_write(str_create(profile_doc));
		STRING * txt;
		WritePrivateProfileString("Profile","Password",(LoginPrompt_Pass->pstring),profile_doc);
		
		WritePrivateProfileString("DOB","day","0",profile_doc);
		WritePrivateProfileString("DOB","month","0",profile_doc);
		WritePrivateProfileString("DOB","year","0",profile_doc);
		
		str_for_int(txt,sys_day);
		WritePrivateProfileString("DOC","day",txt,profile_doc);
		str_for_int(txt,sys_month);
		WritePrivateProfileString("DOC","month",txt,profile_doc);
		str_for_int(txt,sys_year);
		WritePrivateProfileString("DOC","year",txt,profile_doc);

		WritePrivateProfileString("Profile","BPI","0",profile_doc);
		WritePrivateProfileString("Profile","XP","0",profile_doc);
		
		WritePrivateProfileString("Profile","TotalGamesPlayed","0",profile_doc);
		
		WritePrivateProfileString("Game","ARecentGame","0",profile_doc);
		WritePrivateProfileString("Game","BRecentGame","0",profile_doc);
		WritePrivateProfileString("Game","CRecentGame","0",profile_doc);

		WritePrivateProfileString("Field","FieldAttention","0",profile_doc);
		WritePrivateProfileString("Field","FieldMem","0",profile_doc);
		WritePrivateProfileString("Field","FieldArth","0",profile_doc);
		WritePrivateProfileString("Field","FieldFlex","0",profile_doc);
		WritePrivateProfileString("Field","FieldSpeed","0",profile_doc);
		WritePrivateProfileString("Field","BPIFieldAttention","0",profile_doc);
		WritePrivateProfileString("Field","BPIFieldMem","0",profile_doc);
		WritePrivateProfileString("Field","BPIFieldArth","0",profile_doc);
		WritePrivateProfileString("Field","BPIFieldFlex","0",profile_doc);
		WritePrivateProfileString("Field","BPIFieldSpeed","0",profile_doc);
		WritePrivateProfileString("Field","MostPlayedField","0",profile_doc);
		WritePrivateProfileString("Field","SelectedField","0",profile_doc);
	}
}



When I execute the function, all go fine except the data that I write never gets written.

Windows shows the file is 0KB. Whats wrong with my WritePrivateProfileString??(windows.h - Windows API Function)

Thanks & Regards
Posted By: Yashas

Re: WritePrivateProfileString - 10/22/12 15:24

I referred that function from http://msdn.microsoft.com/en-us/library/windows/desktop/ms725501(v=vs.85).aspx

laugh
Posted By: PriNova

Re: WritePrivateProfileString - 10/22/12 16:48

Hi Yasha,

you have to close the file after reading/writing.

in your code the function pointer of file_open_write() should be stored in a variable for later accessing it to close the file:

Code:
var filehandle = file_open_write(str_create(profile_doc)); //opens your file and store if succed the handler into filehandel.
...
file_close(filehandle); // closes the file after write executions

Posted By: Yashas

Re: WritePrivateProfileString - 10/23/12 17:07

Is it so that all information is written only after I file_close??
Posted By: Uhrwerk

Re: WritePrivateProfileString - 10/23/12 17:46

The information may be written previously, but there is no guarantee about this, as the data to be written may be buffered by different mechanisms. Only after closing the file you can guarantee to a certain degree that data is actually written to the harddisk.
Posted By: Yashas

Re: WritePrivateProfileString - 10/25/12 03:40

Complete Code is here - http://pastebin.com/1k7NdWZe - Expires after a month from today!
Posted By: Uhrwerk

Re: WritePrivateProfileString - 10/25/12 12:01

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.
Posted By: Yashas

Re: WritePrivateProfileString - 10/25/12 12:59

I know Visual C++ very well with Windows API.WritePrivateProfileString opens a file itself then closes itself.I had plans adding some extra code before WritePrivateProfileString ,hence I file_open n closed it.I had started a post at MSDN Forum, replies wer that WritePrivateProfileString works only with 16bit Applications.


I need to write my own ini lib I suppose as none exist.
Is there any easier way??

Also I tried wat you told,I removed file_open and file_close still failed.
Thanks! laugh
Posted By: Ch40zzC0d3r

Re: WritePrivateProfileString - 10/25/12 13:50

What about using WritePrivateProfileStringA since you are using chars as parameter and not LWChars laugh
Also you shouldnt use the lite-c strings... convert them to char too!
Posted By: Uhrwerk

Re: WritePrivateProfileString - 10/25/12 13:51

There are several other flaws in your code.

1. In Line 70 you create a memory leak. The string created in the expression is irrevertably lost.
2. The same is true for the string in the expression in line 72.
3. You have a vagabonding pointer in line 79. Always initialize your pointers!
4. You cannot pass a STRING* to a function that expects the type LPTSTR in line 83.
5. temp contains a random address and hence line 83 will overwrite random memory.
6. Line 84: temp is still not initialized so you can't pass it to str_to_int.
7. Problems 5 to 8 are also true for the next blocks of code.
8. Another two memory leaks in line 167 and 168.
9. The problem I already described above. You cannot have a file open and access it with the windows API at the same time as in line 176ff.

You should start simpler. Try to do a small example where a user can enter one line of text and write that to an ini file. If that works continue to expand your code.

Originally Posted By: Yashas
I had plans adding some extra code before WritePrivateProfileString ,hence I file_open n closed it.
Thats perfectly fine. Use the windows API to edit the ini file. Then open the file and manipulate it as you see fits. Just don't open the file WHILE using the Windows API. Remember to clear the cache before opening the file.

Originally Posted By: Yashas
I had started a post at MSDN Forum, replies wer that WritePrivateProfileString works only with 16bit Applications.
That's nonsense. It works perfectly fine with Lite-C.
Posted By: WretchedSid

Re: WritePrivateProfileString - 10/26/12 02:04

Sometimes I wish this forum was like Stack Overflow just so that I can upvote Uhrwerks comments and give him the reputation he deserves frown
(Yes, no constructive content in this post. Just some good old bootlicking)
Posted By: Yashas

Re: WritePrivateProfileString - 10/26/12 10:55

@Uhrwerk I have not reviewed the code yet, I just use some random numbers,silly errors because this is my scale application not the final.

I would correct those those when I make it.
Thanks!!

And "That's nonsense. It works perfectly fine with Lite-C."
Could you give me an simple example of code becaz when ever I try I can't get it right.

Now, I have started off with my own INI Lib(currently able to find a item in the section and return its value laugh )
Posted By: Uhrwerk

Re: WritePrivateProfileString - 10/26/12 14:17

Originally Posted By: JustSid
Sometimes I wish this forum was like Stack Overflow just so that I can upvote Uhrwerks comments and give him the reputation he deserves frown

Wow. Thank you. crazy smile I guess I'll print that and read it whenever I need motivation.

Originally Posted By: Yashas
Could you give me an simple example of code becaz when ever I try I can't get it right.

Code:
#include <acknex.h>
#include <windows.h>

STRING* ini_file = "";

void main()
{
	str_cpy(ini_file,work_dir);
	str_cat(ini_file,"\\test.ini");
	if (WritePrivateProfileString("MySection","MyKey","MyValue",ini_file->chars))
		printf("That was an easy one.");
	else
		error("Oh dear, something went wrong!");
}


The only pitfall here is that you need to have the necessary privileges to write to the directory where you create the ini file. Note that depending on your operating system you don't have these rights for every folder.
Posted By: Yashas

Re: WritePrivateProfileString - 10/26/12 15:45

Thanks laugh
+1 if there was a feature for reputation laugh
© 2024 lite-C Forums