Gamestudio Links
Zorro Links
Newest Posts
Blobsculptor tools and objects download here
by NeoDumont. 03/28/24 03:01
Issue with Multi-Core WFO Training
by aliswee. 03/24/24 20:20
Why Zorro supports up to 72 cores?
by Edgar_Herrera. 03/23/24 21:41
Zorro Trader GPT
by TipmyPip. 03/06/24 09:27
VSCode instead of SED
by 3run. 03/01/24 19:06
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
5 registered members (Nymphodora, AndrewAMD, TipmyPip, Quad, Imhotep), 824 guests, and 4 spiders.
Key: Admin, Global Mod, Mod
Newest Members
sakolin, rajesh7827, juergen_wue, NITRO_FOREVER, jack0roses
19043 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 1 of 2 1 2
WritePrivateProfileString #409809
10/22/12 15:20
10/22/12 15:20
Joined: Nov 2011
Posts: 139
India
Yashas Offline OP
Member
Yashas  Offline OP
Member

Joined: Nov 2011
Posts: 139
India
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


Keep smiling laugh
http://translation.babylon.com/ - Translate many languages
Re: WritePrivateProfileString [Re: Yashas] #409810
10/22/12 15:24
10/22/12 15:24
Joined: Nov 2011
Posts: 139
India
Yashas Offline OP
Member
Yashas  Offline OP
Member

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

laugh


Keep smiling laugh
http://translation.babylon.com/ - Translate many languages
Re: WritePrivateProfileString [Re: Yashas] #409819
10/22/12 16:48
10/22/12 16:48
Joined: Sep 2012
Posts: 74
Niedersachsen, Germany
P
PriNova Offline
Junior Member
PriNova  Offline
Junior Member
P

Joined: Sep 2012
Posts: 74
Niedersachsen, Germany
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


Re: WritePrivateProfileString [Re: PriNova] #409871
10/23/12 17:07
10/23/12 17:07
Joined: Nov 2011
Posts: 139
India
Yashas Offline OP
Member
Yashas  Offline OP
Member

Joined: Nov 2011
Posts: 139
India
Is it so that all information is written only after I file_close??


Keep smiling laugh
http://translation.babylon.com/ - Translate many languages
Re: WritePrivateProfileString [Re: Yashas] #409873
10/23/12 17:46
10/23/12 17:46
Joined: Jan 2002
Posts: 4,225
Germany / Essen
Uhrwerk Offline
Expert
Uhrwerk  Offline
Expert

Joined: Jan 2002
Posts: 4,225
Germany / Essen
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.


Always learn from history, to be sure you make the same mistakes again...
Re: WritePrivateProfileString [Re: Uhrwerk] #409931
10/25/12 03:40
10/25/12 03:40
Joined: Nov 2011
Posts: 139
India
Yashas Offline OP
Member
Yashas  Offline OP
Member

Joined: Nov 2011
Posts: 139
India
Complete Code is here - http://pastebin.com/1k7NdWZe - Expires after a month from today!

Last edited by Yashas; 10/25/12 03:46.

Keep smiling laugh
http://translation.babylon.com/ - Translate many languages
Re: WritePrivateProfileString [Re: Yashas] #409952
10/25/12 12:01
10/25/12 12:01
Joined: Jan 2002
Posts: 4,225
Germany / Essen
Uhrwerk Offline
Expert
Uhrwerk  Offline
Expert

Joined: Jan 2002
Posts: 4,225
Germany / Essen
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...
Re: WritePrivateProfileString [Re: Uhrwerk] #409954
10/25/12 12:59
10/25/12 12:59
Joined: Nov 2011
Posts: 139
India
Yashas Offline OP
Member
Yashas  Offline OP
Member

Joined: Nov 2011
Posts: 139
India
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


Keep smiling laugh
http://translation.babylon.com/ - Translate many languages
Re: WritePrivateProfileString [Re: Yashas] #409956
10/25/12 13:50
10/25/12 13:50
Joined: Oct 2011
Posts: 1,082
Germany
C
Ch40zzC0d3r Offline
Serious User
Ch40zzC0d3r  Offline
Serious User
C

Joined: Oct 2011
Posts: 1,082
Germany
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!

Last edited by Ch40zzC0d3r; 10/25/12 13:51.
Re: WritePrivateProfileString [Re: Yashas] #409957
10/25/12 13:51
10/25/12 13:51
Joined: Jan 2002
Posts: 4,225
Germany / Essen
Uhrwerk Offline
Expert
Uhrwerk  Offline
Expert

Joined: Jan 2002
Posts: 4,225
Germany / Essen
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.


Always learn from history, to be sure you make the same mistakes again...
Page 1 of 2 1 2

Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1