C
C#11mo ago
Ewan

Reading lines on a text file help

how would you read the next line in a text fine? like say my text file had 5 lines of text, how would u read the first line and then move on to the next and read that?
14 Replies
Ewan
EwanOP11mo ago
would you be using stream reader? could i see an example of this?
Pobiega
Pobiega11mo ago
Are you reading all the lines, or just some? If reading all, I'd probably use string[] lines = File.ReadAllLines(...)
Ewan
EwanOP11mo ago
i want to read one line at a time so first read the first line, check for a value, then move onto the second, check for another value that i need etc and so on
Pobiega
Pobiega11mo ago
then you'll need a reader
Ewan
EwanOP11mo ago
i’m not sure how to do it could u possible show me how?
Pobiega
Pobiega11mo ago
var reader = File.OpenText("text.txt");

var x = reader.ReadLine();
Console.WriteLine(x);

var y = reader.ReadLine();
Console.WriteLine(y);
var reader = File.OpenText("text.txt");

var x = reader.ReadLine();
Console.WriteLine(x);

var y = reader.ReadLine();
Console.WriteLine(y);
Ewan
EwanOP11mo ago
ooo i see so this reads each line individually?
Pobiega
Pobiega11mo ago
each time you call ReadLine() it reads one line and returns it
Ewan
EwanOP11mo ago
oh i see, why is var reader equal to blank?
Pobiega
Pobiega11mo ago
wdym? its not
Ewan
EwanOP11mo ago
ohh nvm oops ok i see! ok thank you!!
Pobiega
Pobiega11mo ago
No description
Ewan
EwanOP11mo ago
ooo i see ok this’ll help thanks a ton!
Pobiega
Pobiega11mo ago
are you still doing the dog/owner thing?
Want results from more Discord servers?
Add your server