Solved

How WriteProfileString() works ?

Posted on 1999-01-20
7
340 Views
Last Modified: 2013-12-03
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
Comment
Question by:mikechen
  • 4
  • 3
7 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 100 total points
ID: 1419068
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
 
LVL 22

Expert Comment

by:nietod
ID: 1419069
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
 

Author Comment

by:mikechen
ID: 1419070
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 22

Expert Comment

by:nietod
ID: 1419071
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
 

Author Comment

by:mikechen
ID: 1419072
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
 
LVL 22

Expert Comment

by:nietod
ID: 1419073
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
 

Author Comment

by:mikechen
ID: 1419074
The run-time efficency is not that important. Thank you very much for your ideas. Nietod.

Mike

0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now