Public Group

#### Archived

This topic is now archived and is closed to further replies.

This topic is 5387 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

What i want to do is to simulate the ''Insert'' button when i write to a file.. the file is like this ########################### now i want to be able to go over it and do somthing like : f.open(..) f.seekg(5); f.insert("*****"); now the file should look like this: #####*****################# how can i do this ?

##### Share on other sites
Hmmm, the first thing you''re doing wrong is typing ie. f.open() while I believe it should be fopen(), until you are using a different library :|. IMO you should open a file for writing (but DO NOT reset it), then get the number of characters you want to skip (there''s a function which skips a character after reading it, just dunno which one , and then just write to file (I think)... dunno if this would help. Anyway, try to search on Google for "C++ file operations"... I can''t help you more, sorry.

##### Share on other sites
would it not be better if you first create a function to load the file into memory...say array .... then create a function to do the insert stuff to the array ... then a function to write it to a file ??

A bit better than working directly onto a file like that?? Not sure how hard you whan''t it your way....

##### Share on other sites
oh im sorry.. in my example i was using the
std::ofstream;

but i can use the C FILE* functions too.

EDIT:

the file may be HUGE ! so i dunno if thats to smart ..

[edited by - LogouT on September 21, 2003 9:07:10 AM]

##### Share on other sites
You could mmap the file, that way it should appear as a regular memory area and the OSs'' memory management should do stuff like caching for you. Of course, that wouldn''t relieve you from moving around large portions of the file.

If you only need to do very few insertions and do not need random access during that time (would still be possible but probably rather slow), you could build a data structure that remembers the positions of insertions like

typedef struct {  size_t pos; // position of first inserted character  size_t len; // length of inserted data  char* data;} Insertion_t;

and then build a small API that redirects accesses to the raw data through the insertions. A simple linked list that concatenates alternating parts of insertions and original data would be sufficient for writing the result.

AFAIK, editor programs (other than notepad) handle this "efficiently" by splitting the input data in smaller sequential segments nd doing all operations on those easy-to-handle snippets.

##### Share on other sites
any words i can seach for to find some help with that last idea ?

##### Share on other sites
Try planning it out on paper first. That will give you a basic idea of what you are doing. Then, experiment coding it. I am not aware of a link, but search some C/C++ websites and you should be able to find something. CProgramming, GameTutorials, and Google are your best bets. Sorry for having to point you to Google!

Scott Simontis
e-mail:ageofscott@comcast.net
AIM:ssimontis

##### Share on other sites
Well, the simplist thing I can think of is using two files.

in your function, have the file you want data to be inserted into, the offset (int is fine), a temp file, and the data (strings, bits, etc..)

i usually encapsulate file stuff into a class or struct that opens, closes the file, and holds the file pointer and position,
CFile.

heres the step:

1. Create a temporary file
2. Open the temp file using the open command (there is a command/flag to specifically set it as a temp file)
3. Copy all bytes before offset to temp file
(I use fread/fwrite, but there are probably much faster ways)
4. Copy the insert data
5. Copy the rest of the main file to the temp file.
6. Clear the main file
7. Copy the temp file to the main file

One thing to remember: If your storing raw bits/bytes to a file, make sure to but the "b" modifier when opening, or else every 256th number starting at 10 will screw up the entire file. And that can make someone very angry.

• 17
• 11
• 15
• 9
• 49
• ### Forum Statistics

• Total Topics
631393
• Total Posts
2999772
×