i want to open an utf-8 file, read it line by line and add, at the end of each line, a particular-per-line string. how should i do this?


>i want to open an utf-8 file.
-- Use System.IO.StreamReader class or System.IO.File class.
>at the end of each line, a particular-per-line string...
-- & - Use this sign to concat strings.

i think i wasn't clear enough:).
i want to read a file line by line, and, if a certain value is found on a line, i want to change that line inside the file by adding a particular string at the end of the line. so i need to change the actual file, not a line taken out of the file.
if i use StreamReader, every time i read a line the cursor will get to the next line.

any other ideas?:)

Post your code. Be sure, the source code must be wrapped with bb code tags.

i haven't got any code since i don't know how to do it. the idea, again, is to parse a simple .txt file, find a line that contains a given searchstring and change the line inside the file. and i don't want to read all the lines in an array of strings, make the changes i need and rewrite the file. i can do this in 2 minutes if i work on a database, but i don't know how to approach the issue when working with a simple txt file.


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace daniweb
  public partial class frmFileStuff : Form
    public frmFileStuff()

    private void button1_Click(object sender, EventArgs e)
      const string inFile = @"C:\file.txt";
      string outFile = Path.GetTempFileName();
      using (FileStream fsOut = new FileStream(outFile, FileMode.Open, FileAccess.Write))
        using (StreamReader srIn = new StreamReader(inFile, Encoding.UTF8))
          string line;
          while ((line = srIn.ReadLine()) != null)
            if (line.Contains("c"))
              line += "myCustomText";
            byte[] buffer = System.Text.ASCIIEncoding.UTF8.GetBytes(line + Environment.NewLine);
            fsOut.Write(buffer, 0, buffer.Length);
      File.Move(outFile, inFile);

thanks, sknake, but that again would imply changing unnecessary data.
from what i read so far, it seems this problem has no solution. u just need to read all the content, make changes, and rewrite the file..:-/

Thats just how files work. Not a big deal, though.