To open the file for reading you can use the method File.OpenText
.
The File.OpenText
returns a StreamReader
, whereas it is a file with many lines, use the method StreamReader.ReadLine
to read line by line on loop:
string linha;
using(var arquivo = File.OpenText("arquivo1.txt")) {
while ((linha = arquivo.ReadLine()) != null) {
// Use "linha" aqui ...
}
}
To get the sequence before the first space, use the method String.IndexOf
:
string linha = "foo bar baz";
int indice = linha.IndexOf(" "); // 3. O número de caracteres antes do primeiro espaço
With the character index you want to remove, use the method String.Substring
to return the sequence from that index:
string linha = "foo bar baz";
int indice = linha.IndexOf(" ");
string outraLinha = "bar" + linha.Substring(indice);
Console.WriteLine(linha); // foo bar baz
Console.WriteLine(outraLinha); // bar bar baz
In your specific case, you can apply like this:
using System.IO;
// ...
string linha;
int contador = 0;
using(var arquivo = File.OpenText("foobar.txt")) { // Abre o arquivo para leitura
while ((linha = arquivo.ReadLine()) != null) { // Lê linha por linha
int espaco = linha.IndexOf(" "); // Quantidade de caracteres antes do espaço
contador++; // Incrementa o número da linha atual
// Retorna os caracteres a partir do primeiro espaço
string linhaIndexada = "N" + contador + linha.Substring(espaco);
// Use "linhaIndexada" aqui ...
}
}
See DEMO
So dude, in this https://www.machsupport.com/forum/index.php?action=dlattach;topic=27728.0;attach=38344 he does exactly what I want.. it removes the lines with N and then adds again, the problem is that I have no idea how to do this understands?
– hrmg
Look, I don’t understand it very well, but if it’s in this pattern, you can make a scheme to remove everything before the space, and then include everything again by incrementing 1:1 in front of the N for each time you pass the loop
– DiegoAugusto
Diegoaugusto.. Yes, that’s right, and how can I identify a space in a string and do that ?
– hrmg
@stderr almost that... sometimes the lines can reach up to 50k understand? so I’m trying to do the following here.. all values before space ' ', I wanted to delete... you have any idea?
– hrmg
thus: N0008 G92 X21.7301 Y88.9657 Thus: G92 X21.7301 Y88.9657..... removing the N0008 only in front of the string
– hrmg
Then.. as soon as I removed the before.. I would go through the array again to add that way understand?
– hrmg
It worked out here. The way I wanted it... Thank you so much for your time!
– hrmg