Direct File-write: insertion by stream

I know how to replace a line by loading the file into a vector, and re-writeing a modified vector, but how do I do it directly with the stream?

All purposes considered, I want the program I am writing to take up as little memory as possible. I loads info relating to stuff that will (possibly) be loaded, and it will have to save directly to the file. This is not a project for school, but a personal endeavor. I would appreciate any help you can give me.

string temps = "";
out.open(file.c_str(), ios::[...?, out deletes everything... and app adds it to the end...]);

^
|
another thing i just realized... lol

I would appreciate all the help you give me.

The reason for this algorithm:

A data structure, with a sub-structure represents an object. Since this object member of this object has a data type varying in length, and I want there to be little-to-no limitations on it, so I not want to load it all into memory at any one time.

... I feel that I'm going to hear somthing about iterators for this, lol.

A standard method would be to create a separate output file. Copy the input to the output, line-by-line. At the required point, insert the new line into the output file. Then continue to copy the rest of the input to the output.
If required, at the end, delete the original input file, and rename the output file.

There is good potential for the file to be very long, which would make your solution (one which I am hoping to avoid) extremely in-efficient. Not to mention, I'm also hoping to avoid using a lot of heavy file-writing.

I want to be able to completely replace a single line from a file, without writing a new one(file), or completely loading all of the data. All data from that line will be 'erased' and replaced with the new data.

Ex:

before file:

hi
how
are
you
today?

Replace hi with hello:

hello
how
are
you
today?

remove the first line:

how
are
you
today?

@cire
I do not understand what you mean by 'format the file'? Whould writing the data to it in one way allow me to delete it or somthing?

@ IWishIKnew no problem. I realise my answers have been relatively brief and not fully addressed all your concerns.

I think "format the file" simply means give the file a strictly controlled layout. In the simplest case, each set of data would be of a fixed length, possibly padded with spaces or binary zeros in order to make it a fixed length.

That way, when the new values are written to the file, it will replace the existing data (again, possibly padding with spaces etc which will cover the previous values).

The "\0" is just the C++ way of representing a character code of zero, which is not the same as the digit zero "0".
If you view the file using a hex editor for example, you will see 00 (hex) which is a single character, in the same way that the digit '0' would appear as 30 (hex).

I see the requirements:

Each line has to be able to vary in length, and the number of lines must be able to vary.

An update to an ordinary sequential file with those requirements suggests one of two approaches:

1. Allow extra blank space to accommodate possible changes.advantage - simple and fast to implementdisadvantage - file may be much larger than necessary.
may not be flexible enough.

2. Apply the updates by rewriting the entire file.advantage - file size is kept as small as possible
disadvantage - performance may be slow. Real-world systems often store up a batch of changes in a transaction file, and apply them all at once, avoiding excessive file i/o.

Another possibility exists:
4. Use a file organisation which allows random access - such as a database.advantage - completely flexible as to both the variation in size and the ability to update any part without affecting the rest.
Fast.disadvantage - requires some sort of database to be set up.
also requires some knowledge how to access the database, e.g. using SQL

@naraku
Yes, I know it doesn't. The problem is that there has to be the following two steps for what I want that can not be avoided:

1. Select where to input data
2. replace existing/append new data

Obviously, the easiest way is to load it all into a vector, sort it into a data structure, unsort it into a vector, and write the vector. This means that basically all you're doing for saving/loading is going through a list.

The other way, which I'm currently emplementing right now, is to identify the data loaded in memory, identify what is in the file, and write any unloaded data into the new file, while recognizing when we are to write loaded data that replaces old data we are reading. Then when we have everything saved, we delete the old file, and rename the new file as the old file.

The first way basically takes up a lot of memory (hypothetically, lets just say we have 10Mb...), while the latter writes directly to the disk, while still allowing data to be selected and 'picked' out, at the expense of the hard disk drive.
_____________________________________________________________________

Just an FYI: This is a note taking program. I'm writing it for college (in my spare time, good practice and will be very usfull, and mabey i can sell it) because it would be quite awsome to have all my notes saved for evey year, and be able to pull em up very quickly. That is the goal of this program. The reason I want it to be memory efficient, is because I'm writing it for the long term.

I do supppose I could break up a journal into separate files.... so, say we want note[x], and it isn't in file 1, so we look in file 2 to load it, and when we save it, we don't re-write the whole journal, just the whole file part the note was in... I could emplement a file-size limit, but that would probably be somthing I would want to do after the program is done (somthing for afterward as an improvement mabey).