2019-03-22 11:26:27

Hey,
I am trying to write something. The program will be controlled by the file loaded to the program.
My question is, how can I detect if the line starts with a given character, how can I load a given set of characters from the file to a variable?

If you want to contact me, do not use the forum PM. I respond once a year or two, when I need to write a PM myself. I apologize for the inconvenience.
Telegram: Nuno69a
E-Mail: nuno69a (at) gmail (dot) com

2019-03-22 21:00:33

If you simply want to give inputs to your program using text file, so you don't need to write them always again and again, then you can just use Console.ReadLine() in case of console app as usual and launch the app using program.exe < inputs.txt.
If you want to do something more complex, like text analysys etc. Then StreamReader might be what you're looking for:
https://docs.microsoft.com/en-us/dotnet … work-4.7.2
A brief example:

using System;
using System.IO;

//...

using (StreamReader sr = new StreamReader(@"PathtoYourFile")) {
string fileContent;
fileContent=sr.readToEnd();
//Do your analysis here
}
//Other code

Or because StreamReader is a stream, you can do also something like this:

using System;
using System.IO;

//...
using (StreamReader sr = new StreamReader(@"PathToYourFile")) {
string currentLine;
while ((currentLine = sr.ReadLine()) != null)
{
//Do your line processing here
}
}
//Other code

Best regards

Rastislav

2019-03-23 02:54:26

To *answer the actual questions of the op*:
To read a file, line by line:

using System.IO;
//...
using (StreamReader reader = new StreamReader(@"file.txt")) {
string line;
while ((line = reader.ReadLine())!=null) {
// ensure that the string isn't null or empty (unless you do want that to do something)
if (!String.IsNullOrEmpty(line)&&line.StartsWith(@"control character")) {
// code...
}
}
}

Note: don't read the file in line-by-line via Console.ReadLine(). That function is not meant for reading files.

"On two occasions I have been asked [by members of Parliament!]: 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out ?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question."    — Charles Babbage.
My Github

2019-03-23 04:03:05

To read files line by line, there is also File.ReadAllLines(path) that I like, it returns all lines of a file, it's a one liner. See https://docs.microsoft.com/en-us/dotnet … work-4.7.2

You might be interested by overloads of that method

Reading is one form of escape. Running for your life is another. ― Lemony Snicket

2019-03-23 10:44:50

Thanks everyone for your answers.
Helpful as always smile.

If you want to contact me, do not use the forum PM. I respond once a year or two, when I need to write a PM myself. I apologize for the inconvenience.
Telegram: Nuno69a
E-Mail: nuno69a (at) gmail (dot) com

2019-03-23 14:28:02

@3:

if (!String.IsNullOrEmpty(line)&&line.StartsWith(@"control character")) 

Is not required, because in case of this while loop:

while ((currentLine = sr.ReadLine()) != null)

The loop will end in case of null value in currentLine.

Checking if string is empty is another thing, it is a part of analysis algorithm which may or may not want it.

Best regards

Rastislav

2019-03-23 16:05:48

Rastislav, how should I then check each line for presence of one character?

If you want to contact me, do not use the forum PM. I respond once a year or two, when I need to write a PM myself. I apologize for the inconvenience.
Telegram: Nuno69a
E-Mail: nuno69a (at) gmail (dot) com

2019-03-23 16:17:28 (edited by Rastislav Kish 2019-03-23 16:19:53)

I was reacting to writing null check two times, onetime in the loop, second in the condition, which was not needed. Of course, if you are looking for some character in beginning of the line, then you can use something like this:

if (currentLine.StartsWith("Hello")) numberOfLinesBeginningWithHello++;

Best regards

Rastislav