3 registered members (AndrewAMD, VoroneTZ, dpn),
1,346
guests, and 10
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
WritePrivateProfileString
#409809
10/22/12 15:20
10/22/12 15:20
|
Joined: Nov 2011
Posts: 139 India
Yashas
OP
Member
|
OP
Member
Joined: Nov 2011
Posts: 139
India
|
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
|
|
|
Re: WritePrivateProfileString
[Re: Yashas]
#409819
10/22/12 16:48
10/22/12 16:48
|
Joined: Sep 2012
Posts: 74 Niedersachsen, Germany
PriNova
Junior Member
|
Junior Member
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:
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: Yashas]
#409873
10/23/12 17:46
10/23/12 17:46
|
Joined: Jan 2002
Posts: 4,225 Germany / Essen
Uhrwerk
Expert
|
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: Yashas]
#409952
10/25/12 12:01
10/25/12 12:01
|
Joined: Jan 2002
Posts: 4,225 Germany / Essen
Uhrwerk
Expert
|
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:
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
OP
Member
|
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!
|
|
|
Re: WritePrivateProfileString
[Re: Yashas]
#409956
10/25/12 13:50
10/25/12 13:50
|
Joined: Oct 2011
Posts: 1,082 Germany
Ch40zzC0d3r
Serious User
|
Serious User
Joined: Oct 2011
Posts: 1,082
Germany
|
What about using WritePrivateProfileStringA since you are using chars as parameter and not LWChars 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
Expert
|
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. 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. 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...
|
|
|
|