Writing to a file in C#

Hi,
I am trying to write to file to a particular line number in the file. Is that possible using filestream? Also can you also skip couple of spaces in that line and write it there? Any code would be helpful.

Thanks

Comments

  • : Hi,
    : I am trying to write to file to a particular line number in the file. Is that possible using filestream? Also can you also skip couple of spaces in that line and write it there? Any code would be helpful.
    :
    : Thanks
    :

    That's not possible by directly appending in the middle of the file. If the file is not very big, you could read the whole thing to a buffer, append your text there and finally write the buffer back to file.

    If the file is huge, create a temporary file first. Read stuff from original file to temporary file until you stumble into the position where you want to append your stuff. Then write the additional stuff into temporary file and after that write the rest of the original file to temporary file. Finally destroy the original file and rename temporary file as original file.
  • Hey I am fairly new to C#; I am trying to learn the differences between C++ and C#. But what do you mean by writing it to a buffer? How do I do that? It is a simple .aspx file and I want to change the content of it and make it simple for non programmers. Thank you for your help already and appreciate any more.
  • : Hey I am fairly new to C#; I am trying to learn the differences between C++ and C#. But what do you mean by writing it to a buffer? How do I do that? It is a simple .aspx file and I want to change the content of it and make it simple for non programmers. Thank you for your help already and appreciate any more.
    :

    Well things are not so different in C# than in C/C++ and buffering is basic concept in any programming language or in any computer related stuff. You copy something to buffer and then you write buffer to somewhere in order to gain performance and if this can be done asynchronously even better. But I'll write little synchronous example...

    [code]
    FileStream fIn = new FileStream ...
    FileStream fOut = new FileStream ...
    byte[] buffer = new byte[10 kilobytes should be fine];

    // Read until EOF
    while (fIn.Position < fIn.Length)
    {
    // Read part of the input file to buffer
    int read = fIn.Read(buffer, 0, buffer.Length);

    if (read == 0)
    {
    // Something awful happened
    throw new Exception("KABOOM");
    }

    // Write stuff from buffer to output file
    // Take notice that we use read-variable here ...
    fOut.Write(buffer, 0, read);
    }

    // Close streams
    [/code]
  • : Hi,
    : I am trying to write to file to a particular line number in the file. Is that possible using filestream? Also can you also skip couple of spaces in that line and write it there? Any code would be helpful.
    :
    : Thanks
    :

  • he truth is i've never written a program in c#...so i cant be of any help..I came to this site to see if i could get some notes so that i can begin learning the language....Am completely blick abt c#..The only language i know is C...can you help?
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories