I want to do something like this.
I have a file File.c


# include <stdio.h>

-------------------------->here struct B and its parameters should be inserted when i get it on next iteration.

typedef Struct
B b1; --->this struct i will get on next iteration.

I am writing to the file using filestreams.
Please let me know if there is a way to insert data into the file.
Now when i use seek, the struct A is getting overwritten.

7 Years
Discussion Span
Last Post by sknake

You can't insert text like that. If you're only talking about small code files I would load the contents in to memory, insert the text, then rewrite the entire file.

If you're talking about larger files I would create a temp file with System.IO.Path.GetTempFileName() , stream the contents from the source file to the temp file until you hit the insertion point, insert new text in the temp file, then continue copying the source file to the temp file. When that is finished append ".old" on the source file, move the temp file to the source file, then delete the ".old" file


I have huge data to be written.
Hence i was doing this with tempfiles earlier, but in order to reduce the processing time, this needs to be changed.
Now I dont know how to proceed further..


Use asynchronous file I/O to enhance performance.

Normal file copy:
1) Read X bytes from file -- blocks thread, no writing during this period
2) Write X bytes from file -- blocks thread, no reading during this period
3) Read X bytes (again)

As you can see that is inefficient. As soon as you have read data you should send that chunk off to a Write() mechanism and continue reading. There is no reason to suspend reading while you're writing.

public static void CopyStreamToStream(
    Stream source, Stream destination, 
    Action<Stream,Stream,Exception> completed) {
    byte[] buffer = new byte[0x1000];
    AsyncOperation asyncOp = AsyncOperationManager.CreateOperation(null);

    Action<Exception> done = e => {
        if (completed != null) asyncOp.Post(delegate { 
            completed(source, destination, e); }, null);

    AsyncCallback rc = null;
    rc = readResult => {
        try {
            int read = source.EndRead(readResult);
            if (read > 0) {
                destination.BeginWrite(buffer, 0, read, writeResult => {
                    try {
                            buffer, 0, buffer.Length, rc, null);
                    catch (Exception exc) { done(exc); }
                }, null);
            else done(null);
        catch (Exception exc) { done(exc); }

    source.BeginRead(buffer, 0, buffer.Length, rc, null);

Borrowed from (Figure 2):


Actually I fetch my data from the XML file.
So the XMLReader does all the reading for me...
After I am done with reading one set of tags i convert them into .c code and write to the text based on tags.
I have the same tags with different values (structures with diff names) so asynchronous reading and writing is not possible.


Upload your project with sample XML data. Without seeing the guts of your application its hard to say where you could pick up performance.

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.