P
P
Pavel2020-11-01 17:58:45
C++ / C#
Pavel, 2020-11-01 17:58:45

How to read a file sequentially from the desired line number?

How to read a file sequentially from the desired line number. I find the first line to read like this

while ((line = text.ReadLine()) != null)
            {
                if (line.Contains("Накладная: " + docNum))
                {
                    System.Console.WriteLine(line);
                    System.Console.WriteLine("There were {0} lines 1.", counter);
                    counter1n = counter;
                    System.Console.WriteLine(line);   
                }
                counter++;
            }

In counter1n - the line with which you need to read further line by line, while I did it through for , but something didn’t work out. Please tell me who did this.
line.Contains("Дата отгрузки")

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vasily Bannikov, 2020-11-01
@electronik777

Personally, I don't like the idea of ​​being hardwired to line numbers and expecting a line with certain content to be followed by lines with the right data.
I don't think you're approaching the question correctly.
What is this file format? Maybe it needs to be parsed differently?
If you yourself came up with the idea of ​​exchanging data in this way, then you should look towards more structured formats, such as JSON, YAML and XML.
If you still need to read line by line, then there are: File.ReadAllLines and StreamReader.ReadLine
On average, it should look something like this:

var lines = EnumerateLines(stream); // Некоторый наш метод, который превращает стрим в поток строк
var expectedLine = $"Накладная: {docNum}";
var dataLines = lines.SkipWhile(line => !line.Contains(expectedLine));
var data = ParseLines(dataLines); // Некоторый наш метод, который парсит данные

PS: Name the variables normally, but not counter1nand give the whole code, otherwise it is not clear from your example where this variable is declared in general and how it is used.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question