Edit text file on a specific line

Asked

Viewed 810 times

0

I have a txt file that informs me 3 fields: error line, wrong number and number to replace.

However I am not thinking of an instruction to go directly to the line I need, this file follows a layout.

I can put examples here if you like.

https://pastebin.com/5eDHQcgX <- FILE WHERE I WANT TO MAKE THE MODIFICATIONS https://pastebin.com/xKkJjyds <- ARCHIVE WITH LINES AND VALUES TO REPLACE

Way I’m capturing my txt.

private void button1_Click(object sender, EventArgs e)
        {
            selecionaArquivo();
        }
        public void selecionaArquivo()
        {
            try
            {
                if (abrirArquivo.ShowDialog() == DialogResult.OK)
                {
                    speed = new StreamReader(abrirArquivo.FileName);
                    MessageBox.Show("Arquivo importado");
                }
  • Put an example of the file can help in understanding what you want.

  • For example On the 125479 line of this my file has C425|1033395|4,000|CX|11,96|0,20|0,91| And I want to replace the value 11,96 by 12,15 And I already have a file with these error "logs". 125479|11,96|12,15, where, in order is: line where the wrong is, given to be replaced, given to be inserted in place

  • @Wesley the link you posted on the question, there is the line 125479.

  • Oh yes, it was just an example.

1 answer

0


Before following the code, you have to know which line you want to edit, in the example below put in line 1:

 int line_to_edit = 1; //linha 1
 string newText = "|C425|1033395|4,000|CX|12,15|0,20|0,91|";
 string path = "colocar o caminho do arquivo";
 string[] arrLine = File.ReadAllLines(path);
 arrLine[line_to_edit - 1] = newText;
 File.WriteAllLines(path, arrLine);

Original response: Stackoverflow English

Update:

//arquivo logs
string[] logs = File.ReadAllLines(@"C:\logs.txt");

//arquivo para modificar
string path = @"C:\modificar.txt";
string[] arrLine = File.ReadAllLines(path);

foreach (var item in logs)
{
    //split[0] = LINHA
    //split[1] = VALOR ERRADO
    //split[2] = VALOR A SUBSTITUIR
    var split = item.Split('|');
    int line_to_edit = Convert.ToInt32(split[0]);
    var replace = arrLine[line_to_edit - 1].Replace(split[1], split[2]);
    string newText = replace;
    arrLine[line_to_edit - 1] = newText;
    File.WriteAllLines(path, arrLine);
}
  • So, in case I’m making an application pro contabil. To doing with windows form, have any function in C# that le all lines ? Because readallines in c# are not running, and readline reads line by line.

  • @Wesley As well "readallines in c# not running" ?

  • @Wesley ReadAllLines reads all its lines and returns array string in the arrLine variable.

  • Well when I put to call the Readalllines function to my Streamer object, it doesn’t work. NOTE: Using open filedialog to find the file I need

  • @Wesley What are you trying to do ? Explain to me better, so I can help you. I don’t understand.

  • I’m sorry if I’m being unclear. Two txt files for tax report, one where it informs me, line where there is wrong data, wrong value, and the value to be reset. I need to go in with these two files, and line by line search for these wrong data and reset it to the right value. For example On the 125479 line of this my file has C425|1033395|4,000|CX|11,96|0,20|0,91| And I want to replace the value 11,96 by 12,15 And I already have a file with these error "logs". 125479|11,96|12,15, where, in order is: line where the wrong is, given to be replaced, given to be inserted in place (Copy)

  • @Wesley esse link:https://pastebin.com/5eDHQcgX has on line 539 that’s right ?

  • That’s right, Today this is done at hand, I’m creating this routine to optimize the work.

  • No, thank you for your attention.

  • @Wesley edited the answer see if it works.

  • This is working perfectly! However, I am doing with Forms, pro usuario open both files, how to make this modification ?

  • Then you have to ask another question here at stackoverflow

  • 1

    Perfect, tomorrow I’ll do it. Thank you very much!

Show 8 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.