[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 358
  • Last Modified:

How WriteProfileString() works ?

I need to do something similiar to GetProfileString()/WriteProfileString(), etc. I found out this group of functions works quite efficient. So I want to know internally how it works. I guess the key point is what is the best way to insert or delete a line(s) in the middle of a file.

Thanks,


Mike
0
mikechen
Asked:
mikechen
  • 4
  • 3
1 Solution
 
nietodCommented:
There is no way to insert into a file.  If you write to the middle of a file it overwrite what was already there.

In this day and age, which many meg of RAM available, most programs just handle text files as character arrays in memory.   when you need to insert in the file you just use memcpy() or a similar function to shift back the characters after the insertion point and then copy in the new data.  Finally, when the changes are done, all the data is written out to disk.
0
 
nietodCommented:
I should have mentioned that delete works the same, but in reverse, in this case you shift the data forward and "record" the new length.  Then when you go to write to disk, you write all the data and then set the file length to the shorter length.  (I would just create one generic write-to-disk procedure that wrote all the data and set the file length, this would be used regardless  of whethor or not the file length increased or decreased.)

FYI before memory was plentiful, we used to insert/delete in a text file by copying to a new file.  We would read the file up to insert/delete point in small blocks (like 256 bytes) and write it to a new file.  Then we would write the new data to the file, then we would read and copy the rest of the file in small blocks.  Then we would delete the old file and rename the new file to its name.  This is probably more efficient, but it does have some drawbacks in a multi-tasking system.
0
 
mikechenAuthor Commented:
Thank you for your response. Nietod.
Do you think when WriteProfileString() is called, it actually delete/rename win.ini ? We had this function in Windows 3.0. At that time, memory was quite expense. And are you sure in Windows9x and NT, WriteProfileString() is done as what you described ?
Thanks,

Mike


0
NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

 
nietodCommented:
I can't say that it is exactly as I say, but I can say that it is very likely that all versions of WriteProfileString() are entirely memory based.  .INI files are small.  (An unusually large one might have 1000 lines that are 80 characters long, that is only about 8K--that is nothing for windows 3.1 or later)   A disk based editing algorithm for this would be a lot of additional code(and that takes memory too) and a lot of additional complexity.  it just isn't worth it for this small of a file.

Now there are other ways to actually handle the processing in memory.  For example, I do this sort of thing in C++ and I have a C++ class that processies text files.  When it reads the file it loads each line into a string class.  (a class that supports resizeable strings--this allows lines to be lengthened and shortened with ease)  each string that stores a line is stored in a C++ array class that can also be resized.  This allows lines to be added and deleted, even in the middle.  Now this sort of data stucture (a resizable array of resizable strings) is quite a bit more complex and memory intensive than the simple character array I proposed originally, but it is much easier for the programmer to work with.  If you are working in C++ or an object oriented language, I recommend  this sort of approach.  
0
 
mikechenAuthor Commented:
Thank you for your comment, Nietod.

So each time, WriteProfileString() is called, the whole file is loaded into the memory. A line is inserted into where it supposes to go. Then the whole piece of memory is written back to the file. Then the file is closed.

When GetProfileString() is called, the whole file is loaded into the memory. then the corresponding line is returned in that this function. then the memory is released.

This sounds quite expensive. It looks like there is no reason to keep the file in the memory. Any idea ?

Mike

0
 
nietodCommented:
In the INI fiel days, the OS probably cached the profile files, that is, it probably kept the last X number of files used in memory.  These days the registery has replaced the INI files and the OS is clearly caching parts or all of the registry.  In your cause, If you know that you will be doing repeated accesses to a  particular INI file, provide procedures to lead it and seperate procedures to search/modify it and a seperate procedure to save it.  Then, if you want the convenience, write procedures that do the loading and searching or modifying in one procedure, and impliment them with these seperate procedures.  That way you get the best of both worlds.  If you really wanted to be efficient about this you could build some sort of smart cache for these files, but I reallly doubt it is worth the effort. I can open and save a many thousand line source code file in a fraction of a second, a typical INI file will be far faster.  

Hiw important is run-time efficiency to what you are doing?  is this really likely to be a bottleneck?  (Most programs read INI files very infrequently, is yours different?)
0
 
mikechenAuthor Commented:
The run-time efficency is not that important. Thank you very much for your ideas. Nietod.

Mike

0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now