Insert into a file

I am ready to try adding "edit" to my App. I have done some searching
for ideas and am (a little) surprised not to find quite what I am
looking for.
Is there a way with CFile or some other file class to easily insert
(replace or delete) some data in an open file? What I have read seems
to indicate that I need to copy the file up to the edit point, do the
edit and change pointers and length as necessary and then copy the
rest of the file. This can be done by copying into a new file (and
rename, etc.) or read the whole file into memory and do it in the
buffer (either in-place or to a new buffer), etc.
It seems surprising to me that this is not already canned into an API
call or a MFC library so it would be as simple as:

Myfile.Insert(long EditPos, CString Insertion);
Myfile.Delete(long EditPos, long Size);

and/or:

Myfile.Replace(long EditPos, long Size, CString Insertion);

Don't many people need this kind of capability often?
I can't use CEdit (I don't think) because my use of files is really
very different than a "typical" editor.
What am I missing?
TIA,
Harvey

0
harveyab (125)
3/6/2007 10:09:08 AM
vc.mfc 33608 articles. 0 followers. Follow

5 Replies
303 Views

Similar Articles

[PageSpeed] 23

Hi,

You are simply missing the fact that you can append but not insert into a 
file.
Files are organized in that way; no data shift is possible within a file, 
only is append possible to perform with it.

Perheps it'd be better to refine your design strategy. Either provide more 
info on your problem [you mentioned also CEdit, why especially?]


-- 
======
Arman


"Harvey" wrote:

> I am ready to try adding "edit" to my App. I have done some searching
> for ideas and am (a little) surprised not to find quite what I am
> looking for.
> Is there a way with CFile or some other file class to easily insert
> (replace or delete) some data in an open file? What I have read seems
> to indicate that I need to copy the file up to the edit point, do the
> edit and change pointers and length as necessary and then copy the
> rest of the file. This can be done by copying into a new file (and
> rename, etc.) or read the whole file into memory and do it in the
> buffer (either in-place or to a new buffer), etc.
> It seems surprising to me that this is not already canned into an API
> call or a MFC library so it would be as simple as:
> 
> Myfile.Insert(long EditPos, CString Insertion);
> Myfile.Delete(long EditPos, long Size);
> 
> and/or:
> 
> Myfile.Replace(long EditPos, long Size, CString Insertion);
> 
> Don't many people need this kind of capability often?
> I can't use CEdit (I don't think) because my use of files is really
> very different than a "typical" editor.
> What am I missing?
> TIA,
> Harvey
> 
> 
0
armancho_x1 (249)
3/6/2007 12:06:13 PM
Harvey wrote:
> I am ready to try adding "edit" to my App. I have done some searching
> for ideas and am (a little) surprised not to find quite what I am
> looking for.
> Is there a way with CFile or some other file class to easily insert
> (replace or delete) some data in an open file? What I have read seems
> to indicate that I need to copy the file up to the edit point, do the
> edit and change pointers and length as necessary and then copy the
> rest of the file. This can be done by copying into a new file (and
> rename, etc.) or read the whole file into memory and do it in the
> buffer (either in-place or to a new buffer), etc.
> It seems surprising to me that this is not already canned into an API
> call or a MFC library ...

Disks can't physically insert data, so it must be done using the awkward 
and time consuming copying steps you describe.  So that's probably why 
it is not already provided in an API: Most people would design their 
application to avoid needing to do this.

-- 
Scott McPhillips [VC++ MVP]

0
Scott
3/6/2007 12:21:26 PM
There is no way to "easily" insert or delete contents of an open file.  It is certainly
not part of any existing API mechanism.  While it might be nice, it just isn't there.

CEdit is unrelated to the problem.  It has as little meaning as a pushbutton or combo box
as far as this problem is concerned.

You just have to write the code.

One technique is to use memory-mapped files.  You create a map as large as the existing
file size + the insertion.  Then you map the file into memory, memmove the tail of the
file upwards to create the gap you want, memcpy the new data into the gap, and close the
mapping.  This works fine on files of sizes up to a couple hundred megabytes, but gets
dicey after that because you have to actually map the file in pieces to do the insertion.

The advantage of the mapped file approach is that it is somewhat easier to program,
usually has slightly better performance characteristics than doing raw file I/O, and can
be done "in-place".  The disadvantage is that for large files you get a lot of page faults
(although it is still generally faster than doing raw file I/O) and if the system should
crash in the middle of doing an insertion your file may be hopelessly corrupted.

Send me private email and I'll send you an example of code that does this.
					joe

On 6 Mar 2007 02:09:08 -0800, "Harvey" <harveyab@juno.com> wrote:

>I am ready to try adding "edit" to my App. I have done some searching
>for ideas and am (a little) surprised not to find quite what I am
>looking for.
>Is there a way with CFile or some other file class to easily insert
>(replace or delete) some data in an open file? What I have read seems
>to indicate that I need to copy the file up to the edit point, do the
>edit and change pointers and length as necessary and then copy the
>rest of the file. This can be done by copying into a new file (and
>rename, etc.) or read the whole file into memory and do it in the
>buffer (either in-place or to a new buffer), etc.
>It seems surprising to me that this is not already canned into an API
>call or a MFC library so it would be as simple as:
>
>Myfile.Insert(long EditPos, CString Insertion);
>Myfile.Delete(long EditPos, long Size);
>
>and/or:
>
>Myfile.Replace(long EditPos, long Size, CString Insertion);
>
>Don't many people need this kind of capability often?
>I can't use CEdit (I don't think) because my use of files is really
>very different than a "typical" editor.
>What am I missing?
>TIA,
>Harvey
Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
0
newcomer (15975)
3/6/2007 1:41:40 PM
It depends on the nature of the app.  For example, in a database, "insertion" is done by
appending records and using the index file to cause the records to be seen in sequential
order.  Another approach for inserting into a file that has a private format is to use a
two-way linked list, where the "links" are file offsets.  The nice thing about this is
that you can treat "deleted" records as "free space" and link them into a free-list.  But
of course such a file can only be read  and written by the app designed to read and write
it.  This is generally the principle of ISAM files (Indexed Sequential Access Method, for
those of you who didn't use IBM mainframes).  There are even factors such as "page buffer"
allowances that try to not fill pages so that insertions can be "nearby", thus reducing
the need to seek all over the file for logically adjacent records.  

Because there are so many alternatives, there is no one way to implement the notion of
"insertion", which is one of the reasons it is not actually a primitive in a byte-oriented
file system (the IBM mainframes had ISAM as a fundamental file type, and the OS handled
the details of inserting records, but it did it by creating these linked-list structures,
managing an in-file storage allocator, etc.  But the ISAM files had a lot of limittions on
what you could do with them.  I once wrote a program that could read any kind of file in
any format and display its contents; it took weeks to handle all the file types the OS
supported)
					joe

On Tue, 06 Mar 2007 07:21:26 -0500, "Scott McPhillips [MVP]" <org-dot-mvps-at-scottmcp>
wrote:

>Harvey wrote:
>> I am ready to try adding "edit" to my App. I have done some searching
>> for ideas and am (a little) surprised not to find quite what I am
>> looking for.
>> Is there a way with CFile or some other file class to easily insert
>> (replace or delete) some data in an open file? What I have read seems
>> to indicate that I need to copy the file up to the edit point, do the
>> edit and change pointers and length as necessary and then copy the
>> rest of the file. This can be done by copying into a new file (and
>> rename, etc.) or read the whole file into memory and do it in the
>> buffer (either in-place or to a new buffer), etc.
>> It seems surprising to me that this is not already canned into an API
>> call or a MFC library ...
>
>Disks can't physically insert data, so it must be done using the awkward 
>and time consuming copying steps you describe.  So that's probably why 
>it is not already provided in an API: Most people would design their 
>application to avoid needing to do this.
Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
0
newcomer (15975)
3/6/2007 1:48:21 PM
Hi Harvey,

In addition to what the others have said, some applications approach this 
problem by keeping a "list" of the changes to the text in memory which they 
use to show the current file on screen as it would appear if saved at any 
particular point.  You could think of these kind of like Actions.  Programs 
like Word (Office) will save the list to the disk periodically in a thread 
so that they won't be lost if something should happen (crash) before the 
file is saved.  You can also modify the Action list in memory if you want to 
implement something like Undo or Redo.

When you actually save the file you have to, as the others have said, 
resolve it so that you can write out a serial file (start to finish).  I 
don't know of any file system that allows you to insert content into a file 
on disk or disc image.

Tom

"Harvey" <harveyab@juno.com> wrote in message 
news:1173175748.702580.35770@c51g2000cwc.googlegroups.com...
>I am ready to try adding "edit" to my App. I have done some searching
> for ideas and am (a little) surprised not to find quite what I am
> looking for.
> Is there a way with CFile or some other file class to easily insert
> (replace or delete) some data in an open file? What I have read seems
> to indicate that I need to copy the file up to the edit point, do the
> edit and change pointers and length as necessary and then copy the
> rest of the file. This can be done by copying into a new file (and
> rename, etc.) or read the whole file into memory and do it in the
> buffer (either in-place or to a new buffer), etc.
> It seems surprising to me that this is not already canned into an API
> call or a MFC library so it would be as simple as:
>
> Myfile.Insert(long EditPos, CString Insertion);
> Myfile.Delete(long EditPos, long Size);
>
> and/or:
>
> Myfile.Replace(long EditPos, long Size, CString Insertion);
>
> Don't many people need this kind of capability often?
> I can't use CEdit (I don't think) because my use of files is really
> very different than a "typical" editor.
> What am I missing?
> TIA,
> Harvey
> 

0
tom.nospam (3240)
3/6/2007 3:53:09 PM
Reply:

Similar Artilces:

Opening Lotus password protected file
I'm trying to convert some of my old lotus files that are password protected into Excel files. My old Lotus program got corrupted somehow so I can't remove the password thru Lotus. Any ideas on how I might make the conversion? Would appreciate any help. Thank you. ...

Sending Excel files not saving in Drafts folder
When I go into Excel and click file - send to - Mail Recepient (As attachment), fill out the To, subject, etc, then save it without sending it; it will save a draft copy, but puts the draft in the Inbox instead of the Drafts folder. Can this be changed so that it always saves in the Drafts folder? ...

Cannot Open Any Publisher Files
Don't know what happened but I can no longer open any .pub files regardless of which version created them. Currently I am using Pub 2003 SP2. It is unlikely I have anything older than version 2000. Files saved on network share or local drive will not open. The message is, Publisher cannot open the file. I created a test file and saved it to the local drive and I cannot open that one either. If I save in .rtf format I can open the file, but that loses graphics. I ran the Office repair successfully. I have applied all latest updates.Word and Excel do not seem to be affected. The O/S is X...

How do I insert a horizontal document into a portrait newsletter?
I edit a monthly newsletter with all the pages in portrait format. However, the monthly calendar has to be in horizontal form. I have not been able to find a way to digitally insert it into the newsletter while turning it on its side to fit into the newsletter format, so I have been scanning it and inserting it as a .jpg document which means I lose some sharpness in the process. There must be a better way. Insert a Section Break into the newsletter document before (and after if necessary) the spot where you want to insert the calendar and then via the Page Setup dialog, chan...

#Name? error when opening a file
I'm having a problem opening older files that I used to have no problem with. I have an excel file with links to other workbooks (which I don't have). At first, I could open the file, say no to updating, and continue working. After several months, however, when I open the file, I can see the last #s updated when the prompt to update links appears. As soon as I say no to the update, the links all change to #Name? errors. What is causing the problem? ...

Lookup file with relative pathname
My formulas such as VLOOKUP refer to cells in other workbook files. Excel automatically uses the "hardcoded" absolute pathnames to these files. I need to use a relative pathname so that the set of workbooks are portable. For example, where I currently have vlookup(A1,'N:\Groupname\projectA\workbook.xls'!B1:B10,1) I want vlookup(A1,'..\workbook.xls'!B1:B10,1) Is this possible? Can you use something like the Path property to retrieve the full path of the current workbook then use this to construct the hard-coded pathname? I'm not sure how to create the path...

Reading and writing an XML file
Hello folks! I have to read an XML file, add some elements and write it back. I seem to have no problem doing the first 2 things but then I get The process cannot access the file "c:/file.xml" because it is being used by another process. ... when I try to do the third thing: saving. This is how I am doing it: XmlDocument oXml=new XmlDocument(); oXml.Load(new XmlTextReader(@"c:\file.xml")); // do the element work here oXml.Save(new XmlTextWriter(@"c:\file.xml", System.Text.Encoding.ASCII)); The error occurs right at the Save call. Any of you more experien...

Transfering Money files from XP to Vista
I have MS Money Plus installed on a desktop running on XP and have created a backup of this Money file and saved it to a flash drive. I also have Money installed on a different desktop with Vista and want to restore that same backup, but am unable to do so. I have changed the backup location as well as tried several ways of transfering the data (via email and on a CD) with the same results. I have tried several backups to eliminate the possibility of a damaged file. I am all out of ideas, BUT need to restore this backup on the Vista PC, is there anything else I can do? 1) Don't...

Can't open any files!
In the middle of entering data into Money 2002 (running Windows XP), the program shut down, stating that Money was not properly installed. I've reinstalled Money 2002, even upgraded to Money 2003. I still can't access my data. When I try to open up my Money file, I get this message: "Money cannot locate (file name) or cannot open it, possibly because it is a read-only file or you do not have permission to change it or your disk is write protected. "If you have chosen the correct file and it cannot be accessed, you will need to click OK and open your most recent ba...

Opening files automatically with Import Text File dialog box
I would like to eliminate the requirement to have a user click on the Import button during VBA control of the Import Text File dialog box. This will facilitate the automatic processing of sequentially numbered files. I would appreciate any suggestions. Here's a simplified listing of the code I'm using. Sub ImportOrigData() ' ActiveWindow.ScrollWorkbookTabs Position:=xlFirst Sheets("OrigDataFile").Select Range("A10").Select Dim sText As String For DOY = 147 To 149 ' first and last day of year to be processed sText...

question regarding transaction log files
Does the transaction log file (E000X.log) contain the contents of emails sent or received by a user or it just contains information regarding emails. The reason i am asking this is that incremental backup will only backup the transaction log files, not the databases. My concern is that if i perform a Normal backup on Monday and then perform incremental backup for the rest of the week and then i have a database crash during the week, will I be able to restore emails received to the database after the last Normal backup? Thanks. if you do a weekly fulll, and then a nightly incremental,...

in a excel file, how to make a menu item for the .xls file that when clicked on it runs myform.show? example plz
in a excel file, how to make a menu item for the .xls file that when clicked on it runs myform.show? example plz Try this example, Daniel: http://www.vbaexpress.com/kb/getarticle.php?kb_id=171 ******************* ~Anne Troy www.OfficeArticles.com www.MyExpertsOnline.com "Daniel" <softwareengineer98037@yahoo.com> wrote in message news:uaftfJogFHA.3352@TK2MSFTNGP10.phx.gbl... > in a excel file, how to make a menu item for the .xls file that when clicked > on it runs myform.show? example plz > > ...

Flatten XML file
Hi, I have a large xml file that contains many nested nodes that I want to convert into a flat structure so I can read it into a single database table. I am using VB.Net / SQL Server 2000. A simple example looks something like this (in my xml file there is about 15 levels of nesting): ParentVal1 ChildVal1 ChildVal2 GrandChildVal1 GrandChildVal2 ParentVal2 ChildVal1 GrandChildVal1 GrandChildVal2 I need to convert this to: ParentVal1 ChildVal1 blank ParentVal1 ChildVal2 GrandChildVal1 ParentVal1 ChildVal2 GrandChildVal2 ParentVal2 ChildVal1 GrandC...

Opening Microsoft works files with excel
I recently recieved a microsoft works (.wks) file from a client and could not get it to open using excel. How should I tell him to save the file so that excel will recognize his spread sheet? He can save it as an Excel file, which you'll be able to open. Or, he can save it in Works for Windows 2.0 format. Don wrote: > I recently recieved a microsoft works (.wks) file from a > client and could not get it to open using excel. > How should I tell him to save the file so that excel will > recognize his spread sheet? -- Debra Dalgleish Excel FAQ, Tips & Book List htt...

embedding a macro when generating a CSV file
Greetings! I have a recorded macro for a chart of data in an excel file. Now the question is - how does one generate a CSV file with that macro embedded to generate this chart dynamically. The CSV file will be generated by a user with a series of data (columns/rows). The macro is consistent in that it will generate a chart based upon the SAME column/rows. The difference is that the values will be in flux. Generating the CSV file with the data isn't the problem. How do you then embed the macro code to generate the chart (for each file being created). Regards, Fred You cannot have ...

Problem copying large file on USB storage...
Hello, I'm working on Windows CE 5.0 and I have the following problem : I insert a USB storage device in the USB port and I try to copy a large file (about 5MB) from NAND Flash to USB storage device. The copy started but sometimes it doesn't finish. The OS doesn't show a copy error but I see that the file there isn't on the USB storage. This problem there isn't with small files (about KB). Any idea or suggestion ? Thanks in advance, Paolo -- Paolo Patierno Embedded Software Engineer > Any idea or suggestion ? Hi Paolo, Does the USB host drive...

Lost all the commands in File menu
I would call myself an Access 2003 beginner - I use the software to import canned Excel reports, run some queries to convert tech-speak to normal-speak and calculate a few things, then export it to Excel for others to see & use. I use the software once a week, mostly via some macros and a switchboard I made. I fetch the spreadsheets and drop them in a folder, click a couple times and what once took 4 hours in Excel copy/paste now takes 4 minutes. I wanted to take it another step further and learn to make it grab the canned reports from the server and generate it's reports and a few ...

Formatting problem with linked files
I have 2 worksheets linked. The text changes on the 2nd file when I change the first, but if I change the color of the text, it doesn't update. Any ideas? Hi can't be done with formulas / links as formulas only return values -- Regards Frank Kabel Frankfurt, Germany Cindy wrote: > I have 2 worksheets linked. The text changes on the 2nd > file when I change the first, but if I change the color > of the text, it doesn't update. Any ideas? Thanks, but it's just simple text. Is there any way around it? >-----Original Message----- >Hi >can't be d...

file encrypt
Recently I transfer all my files to a new computer. After the transfer, I tried to open a file on my computer with excel. It comes back that the document maybe read-only or encrypted. I did not make this read only or encrypt it. It was working fine before I move it to my new computer. could someone help me open this file? Thank you. One thought ... Did you transfer the files by copying them via a CD or DVD? If so, that can set the Read-Only flag (because the CD is seen as a read-only medium). If that's the case, go to explorer and right click the file and select Properties. A...

Unable to open .pst file #2
Can some one tell me how to open the outlook archive file? When I try to open the outlook inbox archive file saved as outlook.pst, a message box of outlook is opening with an attachment outlook.pst. When I try to open this, i am once again getting the same message box with the same attachment. However if I try to open the archive file outlook.pst with MS Word, some matter is seen in an unreadable format (encrypted?). I do have a pass word to open outlook. But when I try to open the archive, it is not asking for the pass word. Please let me know how to open the archive file. Thank u. --...

Saving outlook pst file to the I:\ Drive
Good evening I am using Microsoft outlook 2003 and I am trying to save my outlook files (both personal folders and contact list (the PST File?)) to a drive other then the default c:\ Drive. I have had on more than one occasion had the system crash and have lost these files forever when on the C:\ drive. Is there a way I can have these files (file) saved on a drive other than the C:\ Drive. I mean that whenever I use outlook the files is on a drive other then the c:\ drive so that if I have a system crash I can just restore windows and outlook and then go out to the drive and pull these (...

opening .pps files windows 7 with wml
have associated .pps files in default programs with ms ppv & when i get a pps file in wml, it asks for a file association. have googled til i'm blue in the face & found only vista solutions. have gone into the registry & found powerpointviewer, but can't change "show" to "open" because there's already a subsection there titled "open." so how can i get microsoft's powerpoint viewer to open files when i receive them in windows live mail using windows 7? thx :D Did you check that in both places as explained here? Unable to...

Excel-Export a file as a Fixed Length Text file
Can anyone advise on how to export/save an Excel file to a fixed length text file? Here's a thread that offers a couple of alternatives: http://groups.google.com/groups?threadm=40E61859.7071CC5A%40msn.com RJM wrote: > > Can anyone advise on how to export/save an Excel file to a fixed length text file? -- Dave Peterson ec35720@msn.com ...

File won't open anymore
Version: 2008 Operating System: Mac OS X 10.6 (Snow Leopard) Processor: Intel Hi all, <br><br>I have an xls file that I've been working on for the last week, and now all of a sudden it won't open in Excel 2008. I don't get any error messages, the progress bar flashes for a second, then nothing. I don't see it in the recently opened either. <br><br>I can still do a quickview on the file and see everything, as well as open it with Numbers... but not with excel. <br><br>I've tried a repair permissions on the computer, and verified tha...

Sending to multiple addresses in a single contact file
Version: 2008 Operating System: Mac OS X 10.6 (Snow Leopard) Processor: Intel Email Client: Exchange I have a Group of contacts within which there are multiple e-mail addresses for each contact (i.e. Work E-mail and Personal E-Mail). When I add the contacts to the Group it will only send to the primary e-mail address. How do I instruct Entourage to include all of the available e-mail addresses in a contact file when added to a Group? ...