Howdy, Stranger!

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

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Writing to a file in C#

hapinderhapinder Posts: 2Member
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

  • weiccoweicco Posts: 950Member
    : 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.
  • hapinderhapinder Posts: 2Member
    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.
  • weiccoweicco Posts: 950Member
    : 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]
  • dvdssalidvdssali Posts: 2Member
    : 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
    :

  • dvdssalidvdssali Posts: 2Member
    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.